| @@ -1,6 +1,7 @@ |
| 1 | | −import { cpSync, existsSync, mkdirSync, rmSync } from "node:fs"; |
| 1 | +import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | 2 | import { dirname, join } from "node:path"; |
| 3 | 3 | import { createRequire } from "node:module"; |
| 4 | +import { pathToFileURL } from "node:url"; |
| 4 | 5 | |
| 5 | 6 | const root = process.cwd(); |
| 6 | 7 | const out = join(root, ".selfhost-runtime"); |
| @@ -36,6 +37,7 @@ const libsqlNativePackage = (): string => { · 29 unmodified lines |
| 36 | 37 | }; |
| 37 | 38 | |
| 38 | 39 | const externalPackages = [ |
| 40 | + "@cloudflare/worker-bundler", |
| 39 | 41 | "quickjs-emscripten", |
| 40 | 42 | "quickjs-emscripten-core", |
| 41 | 43 | "@jitl/quickjs-ffi-types", |
| @@ -64,12 +66,39 @@ const copyPackage = (name: string): void => { · 22 unmodified lines |
| 64 | 66 | cpSync(packageDir(name), destination, { recursive: true, dereference: true }); |
| 65 | 67 | }; |
| 66 | 68 | |
| 69 | +// The worker-bundler backend (packages/plugins/apps/src/pipeline/ |
| 70 | +// worker-bundler-artifact.ts) loads `dist/index.bundled.js` from the resolved |
| 71 | +// package, falling back to bundling `dist/index.js` with esbuild at runtime. |
| 72 | +// The npm package doesn't ship the bundled entry and the runtime image has no |
| 73 | +// esbuild, so produce it here, the same way apps/cli/src/build.ts does for the |
| 74 | +// packed CLI. |
| 75 | +const writeBundledWorkerBundler = async (): Promise<void> => { |
| 76 | + const distPath = join(packageDir("@cloudflare/worker-bundler"), "dist"); |
| 77 | + const esbuildEntry = requireFromSelfHost.resolve("esbuild", { |
| 78 | + paths: [join(root, "node_modules/.bun/node_modules")], |
| 79 | + }); |
| 80 | + const { build } = await import(pathToFileURL(esbuildEntry).href); |
| 81 | + const result = await build({ |
| 82 | + entryPoints: [join(distPath, "index.js")], |
| 83 | + bundle: true, |
| 84 | + format: "esm", |
| 85 | + platform: "browser", |
| 86 | + external: ["./esbuild.wasm"], |
| 87 | + logLevel: "silent", |
| 88 | + write: false, |
| 89 | + }); |
| 90 | + const source = result.outputFiles[0]?.text; |
| 91 | + if (source === undefined) throw new Error("failed to bundle @cloudflare/worker-bundler"); |
| 92 | + writeFileSync(join(out, "node_modules/@cloudflare/worker-bundler/dist/index.bundled.js"), source); |
| 93 | +}; |
| 94 | + |
| 67 | 95 | rmSync(out, { recursive: true, force: true }); |
| 68 | 96 | mkdirSync(serverOut, { recursive: true }); |
| 69 | 97 | |
| 70 | 98 | await Bun.$`bun build apps/host-selfhost/src/serve.ts --target=bun --format=esm --outdir=${serverOut} ${quickJsExternals.map((name) => `--external=${name}`)}`; |
| 71 | 99 | |
| 72 | 100 | for (const name of externalPackages) copyPackage(name); |
| 101 | +await writeBundledWorkerBundler(); |
| 73 | 102 | |
| 74 | 103 | if (!existsSync(join(serverOut, "serve.js"))) { |
| 75 | 104 | throw new Error( |