-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.mjs
More file actions
96 lines (86 loc) · 3.22 KB
/
esbuild.mjs
File metadata and controls
96 lines (86 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as esbuild from 'esbuild';
import * as fs from 'node:fs';
import { minifyCSS } from "./minifyCSS.js";
/**
* @param {Record<string, (original: string, ...args: string[]) => string>} options
* @returns {esbuild.Plugin}
*/
const bookmarkletPlugin = options => ({
name: 'bookmarkletPlugin',
setup(build) {
build.onLoad({ filter: /\.(tsx|js)$/ }, async args => {
let code = await fs.promises.readFile(args.path, 'utf8');
const cssRegex = /css`([^`]+)`/g;
let match;
let newCode = code;
while ((match = cssRegex.exec(code)) !== null) {
const originalCSS = match[1];
const minifiedCSS = minifyCSS(originalCSS);
newCode = newCode.replace(originalCSS, minifiedCSS);
}
return {
loader: 'tsx',
contents: newCode,
};
});
build.onEnd(async (result) => {
const { outputFiles } = result;
if (!outputFiles) return;
for (const file of outputFiles) {
const utf8Decoder = new TextDecoder();
const outputContent = utf8Decoder.decode(file.contents);
const compactCode = outputContent
.replace(/\s+/g, ' ')
.replace(/\s*([{}();,:])\s*/g, '$1');
const bookmarklet = 'javascript:(function(){' + compactCode + '})();';
file.contents = Buffer.from(bookmarklet, 'utf8');
}
});
}
});
const distDir = 'dist';
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir);
}
await esbuild.build({
entryPoints: ['src/bookmarklet.js'],
bundle: true,
minify: true,
outfile: 'dist/bookmarklet.txt',
write: false,
plugins: [
bookmarkletPlugin()
]
}).then(result => {
let bookmarkletCode = '';
for (const file of result.outputFiles) {
fs.writeFileSync(file.path, file.contents);
bookmarkletCode = file.contents.toString();
}
const encodedBookmarklet = 'javascript:' + encodeURI(
bookmarkletCode.replace(/^javascript:/, '')
);
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CAAT Color Contrast Checker Bookmarklet</title>
</head>
<body>
<main class="row">
<h1>CAAT-Color-Contrast-Checker</h1>
<h2>Bookmark Color Contrast Calculation Tool</h2>
<ol>
<li>Activate the bookmark toolbar in your browser (if not active yet). Most Browsers allow this by pressing Ctrl+Shift+B.</li>
<li>Drag & drop the link <a href="${encodedBookmarklet}">CAAT Contrast Checker</a> to the bookmark toolbar.</li>
<li>Activate the bookmarklet on any page.</li>
<li>Use the pipette to transfer color values or enter them manually into the fields to determine the color contrast.</li>
</ol>
</main>
</body>
</html>
`;
fs.writeFileSync(`${distDir}/ccc-bookmarklet.html`, htmlContent);
});