-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
62 lines (55 loc) · 1.75 KB
/
build.js
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
import fs from 'fs';
import { minify } from 'minify';
import esbuild from 'esbuild';
import chokidar from 'chokidar';
import debounce from 'debounce';
const isWatching = process.argv[2] === '--watch';
const minifyOptions = {
js: {
type: 'terser',
terser: {
compress: {
keep_fnames: true
},
mangle: {
keep_fnames: true
}
}
}
};
async function build () {
console.log(new Date(), 'rebuilding...');
await fs.promises.rm('server/dist', { recursive: true, force: true });
await fs.promises.mkdir('server/dist');
await esbuild.build({
entryPoints: ['./server/worker.js'],
bundle: true,
outfile: './server/dist/worker.js'
}).then(console.log);
if (!isWatching) {
const minifiedHtml = await minify('server/dist/worker.js', minifyOptions);
await fs.promises.writeFile('./server/dist/worker.js', minifiedHtml);
}
const jsData = await fs.promises.readFile('./server/dist/worker.js', 'utf8');
let htmlData = await fs.promises.readFile('./server/index.html', 'utf8');
htmlData = htmlData.replace('{{WORKERSCRIPT}}', jsData);
await fs.promises.writeFile('./server/dist/index.html', htmlData);
await fs.promises.writeFile(
'./lib/builtinWorker.html.js',
'// built from the ./server/dist/index.html file during npm run build\nconst builtinWorker = atob(`' + Buffer.from(htmlData).toString('base64') + '`); export default builtinWorker;'
);
await fs.promises.rm('./server/dist/worker.js');
}
if (isWatching) {
const debouncedBuild = debounce(build, 300);
chokidar.watch(
['./lib/*', './server'], {
ignored: ['**/dist/**', './lib/builtinWorker.html.js']
}).on('all', (why, what) => {
console.log(new Date(), why, what);
debouncedBuild();
}
);
} else {
build();
}