Skip to content

Commit

Permalink
feat: split worker bundle with esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone committed Oct 2, 2024
1 parent 917ef60 commit 75e90ea
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
46 changes: 35 additions & 11 deletions packages/wrangler/src/deployment-bundle/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
rewriteNodeCompatBuildFailure,
} from "./build-failures";
import { dedupeModulesByName } from "./dedupe-modules";
import { getEntryPointFromMetafile } from "./entry-point-from-metafile";
import { getEntryPointFromSplitBundleMetafile } from "./entry-point-from-metafile";
import { asyncLocalStoragePlugin } from "./esbuild-plugins/als-external";
import { cloudflareInternalPlugin } from "./esbuild-plugins/cloudflare-internal";
import { configProviderPlugin } from "./esbuild-plugins/config-provider";
Expand Down Expand Up @@ -44,7 +44,13 @@ const escapeRegex = (str: string) => {
export const COMMON_ESBUILD_OPTIONS = {
// Our workerd runtime uses the same V8 version as recent Chrome, which is highly ES2022 compliant: https://kangax.github.io/compat-table/es2016plus/
target: "es2022",
loader: { ".js": "jsx", ".mjs": "jsx", ".cjs": "jsx" },
loader: {
".js": "jsx",
".mjs": "jsx",
".cjs": "jsx",
".wasm": "binary",
".DS_Store": "text",
},
} as const;

/**
Expand Down Expand Up @@ -383,15 +389,15 @@ export async function bundleWorker(
entryPoints: [entry.file],
bundle,
absWorkingDir: entry.directory,
outdir: destination,
outdir: isOutfile ? path.dirname(destination) : destination,
entryNames: entryName || path.parse(entryFile).name,
...(isOutfile
? {
outdir: undefined,
outfile: destination,
entryNames: undefined,
}
: {}),
// ...(isOutfile
// ? {
// outdir: undefined,
// outfile: destination,
// entryNames: undefined,
// }
// : {}),
inject,
external: bundle
? ["__STATIC_CONTENT_MANIFEST", ...(external ? external : [])]
Expand All @@ -402,6 +408,7 @@ export async function bundleWorker(
// Include a reference to the output folder in the sourcemap.
// This is omitted by default, but we need it to properly resolve source paths in error output.
sourceRoot: destination,
splitting: entry.format === "modules" ? true : false,
minify,
metafile: true,
conditions: getBuildConditions(),
Expand Down Expand Up @@ -463,6 +470,7 @@ export async function bundleWorker(
let stop: BundleResult["stop"];
try {
if (watch) {
// CHECK
const ctx = await esbuild.context(buildOptions);
await ctx.watch();
result = await initialBuildResultPromise;
Expand All @@ -475,6 +483,7 @@ export async function bundleWorker(
await ctx.dispose();
};
} else {
// CHECK
result = await esbuild.build(buildOptions);
// Even when we're not watching, we still want some way of cleaning up the
// temporary directory when we don't need it anymore
Expand All @@ -489,7 +498,11 @@ export async function bundleWorker(
throw e;
}

const entryPoint = getEntryPointFromMetafile(entryFile, result.metafile);
const entryPoint = getEntryPointFromSplitBundleMetafile(
entry.file,
result.metafile
);

const notExportedDOs = doBindings
.filter((x) => !x.script_name && !entryPoint.exports.includes(x.class_name))
.map((x) => x.class_name);
Expand Down Expand Up @@ -518,6 +531,17 @@ export async function bundleWorker(
const modules = dedupeModulesByName([
...moduleCollector.modules,
...additionalModules,
...Object.entries(result.metafile.outputs)
.filter(([name]) => {
return !name.endsWith(".map") && name !== entryPoint.relativePath;
})
// TODO: this is a hack to see what CI says
.map(([name, _output]) => ({
name: path.relative(path.dirname(entryPoint.relativePath), name),
content: fs.readFileSync(name),
type: "esm" as const,
filePath: path.resolve(name),
})),
]);

await writeAdditionalModules(modules, path.dirname(resolvedEntryPointPath));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import assert from "node:assert";
import path from "node:path";
import type { Metafile } from "esbuild";

/**
* Compute entry-point information such as path, exports and dependencies
* from the output of esbuild.
*/
export function getEntryPointFromMetafile(
export function getEntryPointFromSimpleMetafile(
entryFile: string,
metafile: Metafile
) {
Expand Down Expand Up @@ -34,3 +35,35 @@ export function getEntryPointFromMetafile(
dependencies: entryPoint.inputs,
};
}

/**
* Compute entry-point information such as path, exports and dependencies
* from the output of esbuild.
*/
export function getEntryPointFromSplitBundleMetafile(
entryFile: string,
metafile: Metafile
) {
const relativeEntryFile = path.relative(process.cwd(), entryFile);
const entryPointDetails = Object.entries(metafile.outputs).find(
([_output, { entryPoint }]) => {
if (entryPoint) {
return entryPoint === relativeEntryFile;
}
return false;
}
);

if (!entryPointDetails) {
throw new Error(
`Cannot find entry-point "${entryFile}" in generated bundle.`
);
}

// console.log("entryPointDetails", entryPointDetails);
return {
relativePath: entryPointDetails[0],
exports: entryPointDetails[1].exports,
dependencies: entryPointDetails[1].inputs,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as esbuild from "esbuild";
import { UserError } from "../errors";
import { logger } from "../logger";
import { COMMON_ESBUILD_OPTIONS } from "./bundle";
import { getEntryPointFromMetafile } from "./entry-point-from-metafile";
import { getEntryPointFromSimpleMetafile } from "./entry-point-from-metafile";
import type { CfScriptFormat } from "./worker";

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ export default async function guessWorkerFormat(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const metafile = result.metafile!;

const { exports } = getEntryPointFromMetafile(entryFile, metafile);
const { exports } = getEntryPointFromSimpleMetafile(entryFile, metafile);
let guessedWorkerFormat: CfScriptFormat;
if (exports.length > 0) {
if (exports.includes("default")) {
Expand Down

0 comments on commit 75e90ea

Please sign in to comment.