-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
48 lines (40 loc) · 1.28 KB
/
Copy pathbuild.js
File metadata and controls
48 lines (40 loc) · 1.28 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
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const isWatch = process.argv.includes('--watch');
async function buildOnce() {
if (!fs.existsSync('dist')) fs.mkdirSync('dist');
await esbuild.build({
entryPoints: ['src/plugin/code.ts'],
bundle: true,
outfile: 'dist/code.js',
platform: 'browser',
target: 'es6',
});
await esbuild.build({
entryPoints: ['src/ui/index.ts'],
bundle: true,
outfile: 'dist/_ui-bundle.js',
platform: 'browser',
target: 'es6',
});
const html = fs.readFileSync('src/ui/index.html', 'utf8');
const css = fs.readFileSync('src/ui/styles.css', 'utf8');
const js = fs.readFileSync('dist/_ui-bundle.js', 'utf8');
fs.unlinkSync('dist/_ui-bundle.js');
const final = html
.replace('<!-- inject:css -->', `<style>\n${css}\n</style>`)
.replace('<!-- inject:js -->', `<script>\n${js}\n</script>`);
fs.writeFileSync('dist/ui.html', final);
console.log('[build] done');
}
if (isWatch) {
console.log('[watch] watching for changes...');
const chokidar = require('chokidar');
buildOnce().catch(console.error);
chokidar.watch('src').on('change', () => {
buildOnce().catch(console.error);
});
} else {
buildOnce().catch((e) => { console.error(e); process.exit(1); });
}