-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.worker.mjs
56 lines (50 loc) · 1.34 KB
/
build.worker.mjs
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
import * as esbuild from 'esbuild';
const argv = new Set(process.argv.slice(2));
const identifier = (length = 9) =>
`i${Math.random().toString(36).substring(2, length)}`;
const createRequireAlias = identifier();
/**
* @type {import('esbuild').BuildOptions}
*/
const options = {
entryPoints: ['./src/worker/index.ts'],
outfile: './dist/worker.mjs',
format: 'esm',
platform: 'node',
bundle: true,
sourcemap: true,
minify: argv.has('--minify'),
banner: {
js: `
import { createRequire as ${createRequireAlias} } from 'module';
const require = ${createRequireAlias}(import.meta.url);
`,
},
};
if (argv.has('--watch')) {
const ctx = await esbuild.context({
...options,
plugins: [
{
name: 'rebuild',
setup(build) {
build.onEnd((result) => {
console.log('[watch] build started');
if (result.errors.length > 0) {
result.errors.forEach((error) =>
console.error(
`> ${error.location.file}:${error.location.line}:${error.location.column}: error: ${error.text}`,
),
);
} else {
console.log('[watch] build finished, watching for changes...');
}
});
},
},
],
});
await ctx.watch();
} else {
await esbuild.build(options);
}