/
buildWorker.ts
62 lines (60 loc) · 1.47 KB
/
buildWorker.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
import path from "node:path";
import { build } from "esbuild";
type Options = {
routesModule: string;
outfile: string;
minify?: boolean;
sourcemap?: boolean;
fallbackService?: string;
watch?: boolean;
onEnd?: () => void;
};
export function buildWorker({
routesModule,
outfile = "bundle.js",
minify = false,
sourcemap = false,
fallbackService = "ASSETS",
watch = false,
onEnd = () => {},
}: Options) {
return build({
entryPoints: [
path.resolve(__dirname, "../pages/functions/template-worker.ts"),
],
inject: [routesModule],
bundle: true,
format: "esm",
target: "esnext",
outfile,
minify,
sourcemap,
watch,
allowOverwrite: true,
define: {
__FALLBACK_SERVICE__: JSON.stringify(fallbackService),
},
plugins: [
{
name: "wrangler notifier and monitor",
setup(pluginBuild) {
pluginBuild.onEnd((result) => {
if (result.errors.length > 0) {
console.error(
`${result.errors.length} error(s) and ${result.warnings.length} warning(s) when compiling Worker.`
);
} else if (result.warnings.length > 0) {
console.warn(
`${result.warnings.length} warning(s) when compiling Worker.`
);
onEnd();
} else {
console.log("Compiled Worker successfully.");
onEnd();
}
});
},
},
],
});
}