-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-cfworker.ts
63 lines (53 loc) · 1.36 KB
/
build-cfworker.ts
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
import 'dotenv/config';
import { Plugin, context as createContext } from 'esbuild';
import mri from 'mri';
import { DEFAULT_BUILD_ARGS, buildParams } from './build';
const argv = mri(process.argv.slice(2));
const resolvePlugin = {
name: 'resolvePlugin',
setup(build) {
build.onResolve(
{ filter: /^decode-named-character-reference$/ },
async () => {
const result = {
path: require.resolve('decode-named-character-reference'),
};
return result;
},
);
},
} as Plugin;
function wrapNodeExternal(list: string[]) {
const external = new Set<string>();
list.forEach((name) => {
external.add(`node:${name}`);
external.add(name);
});
return Array.from(external);
}
const externalNodeBuiltins = wrapNodeExternal(['async_hooks']);
async function buildWorker() {
const context = await createContext({
...buildParams,
entryPoints: ['./src/runtime/cfworker/index.ts'],
platform: 'browser',
target: 'es2020',
format: 'esm',
treeShaking: true,
plugins: [resolvePlugin],
define: DEFAULT_BUILD_ARGS,
external: externalNodeBuiltins,
});
if (argv['watch']) {
await context.watch();
} else {
await context.rebuild().then((v) => {
console.log(`build ~ result`, v);
context.dispose();
});
}
}
async function main() {
await buildWorker();
}
main();