-
Notifications
You must be signed in to change notification settings - Fork 749
/
bundle.mjs
77 lines (68 loc) · 2.6 KB
/
bundle.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from "node:fs/promises";
import path from "node:path";
import { worker as workerPath } from "./paths.mjs";
import * as esbuild from "esbuild";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
import fg from "fast-glob";
// Process each of the JS files in the `_worker.js` directory
for (const entry of await fg("**/*.js", { cwd: workerPath, onlyFiles: true })) {
if (entry === "index.js") {
// This is the main bundle and gets special treatment
await bundleMain();
} else {
await bundleLazyModule(entry);
}
}
// Use esbuild to process the main entry-point.
// - shim Node.js APIs
// - convert `global` to `globalThis`
// - convert dynamic `require()` calls to `await import()` calls
// - ensure that the Cloudflare `fetch()` handler is exported
async function bundleMain() {
const result = await esbuild.build({
entryPoints: ["index.js"],
bundle: true,
format: "iife",
write: false,
absWorkingDir: workerPath,
define: {
global: "globalThis",
},
plugins: [
NodeGlobalsPolyfillPlugin({ buffer: true }),
NodeModulesPolyfillPlugin(),
],
});
// Store the original promise (before Angular/Zone.js replaces it) on the global scope.
let main = "globalThis.OGPromise = Promise;\n" + result.outputFiles[0].text;
// Patch any dynamic imports (converting `require()` calls to `import()` calls).
main = main.replace(
'installChunk(__require("./" + __webpack_require__.u(chunkId))',
'promises.push(import("./" + __webpack_require__.u(chunkId)).then((mod) => installChunk(mod.default))'
);
// Export the fetch handler (grabbing it from the global).
// Also Cloudflare expects `fetch()` to return an original Promise (not a ZoneAwarePromise).
main +=
"\nexport default { fetch: (request, env) => globalThis.OGPromise.resolve(globalThis.__workerFetchHandler(request, env)) };";
await fs.writeFile(path.resolve(workerPath, "index.js"), main);
}
// Use esbuild to process the lazy load modules
// In particular we need to convert the CommonJS export syntax to ESM.
async function bundleLazyModule(filePath) {
const result = await esbuild.build({
entryPoints: [filePath],
bundle: true,
format: "cjs",
write: false,
absWorkingDir: workerPath,
define: {
global: "globalThis",
},
plugins: [NodeModulesPolyfillPlugin()],
});
let content = result.outputFiles[0].text;
// Export the fetch handler (grabbing it from the global).
content = "const exports = {};\n" + content + "\nexport default exports";
await fs.writeFile(path.resolve(workerPath, filePath), content);
}