Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

refactor: route single-file bundles through one Rolldown helper (#804)

Casey Gowrie4531a3dbc486
diff --git a/.changeset/shared-single-chunk-builder.md b/.changeset/shared-single-chunk-builder.md
new file mode 100644
index 000000000..28f00b859
--- /dev/null
+++ b/.changeset/shared-single-chunk-builder.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Route all of eve's single-file bundles through a shared Rolldown helper that always disables code splitting. This closes a remaining gap where a dynamic import reachable from a final workflow bundle could still fail with "Expected one bundled ..." during builds.
diff --git a/packages/eve/src/internal/authored-module-loader.ts b/packages/eve/src/internal/authored-module-loader.ts
index 4632e1d4e..d8cd94f5f 100644
--- a/packages/eve/src/internal/authored-module-loader.ts
+++ b/packages/eve/src/internal/authored-module-loader.ts
@@ -18,10 +18,7 @@ import {
type RolldownResolveContext,
} from "#internal/authored-package-boundary.js";
import { expectObjectRecord } from "#internal/authored-module.js";
-import {
- buildWithNitroRolldown,
- getSingleRolldownChunk,
-} from "#internal/bundler/nitro-rolldown.js";
+import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
import { createNodeEsmCompatBannerPlugin } from "#internal/node-esm-compat-banner.js";
const AUTHORED_BUNDLED_MODULE_EXTENSION = /\.[cm]?[jt]sx?$/;
@@ -252,7 +249,7 @@ async function buildAuthoredModuleBundle(
].filter((plugin) => plugin !== null);
try {
- const result = await buildWithNitroRolldown({
+ const chunk = await buildSingleRolldownChunk(`authored module for "${modulePath}"`, {
cwd: packageRoot,
input: modulePath,
platform: "node",
@@ -261,15 +258,13 @@ async function buildAuthoredModuleBundle(
extensions: [...RESOLVE_EXTENSIONS],
},
tsconfig: tsconfigPath,
- write: false,
output: {
- codeSplitting: false,
comments: false,
format: "esm",
sourcemap: configuration.sourcemap,
},
});
- return getSingleRolldownChunk(result, `authored module for "${modulePath}"`).code;
+ return chunk.code;
} catch (error) {
throw createAuthoredModuleBundleError(modulePath, error);
}
diff --git a/packages/eve/src/internal/bundler/extension-scope-plugin.integration.test.ts b/packages/eve/src/internal/bundler/extension-scope-plugin.integration.test.ts
index e5a9c3336..fa2ade260 100644
--- a/packages/eve/src/internal/bundler/extension-scope-plugin.integration.test.ts
+++ b/packages/eve/src/internal/bundler/extension-scope-plugin.integration.test.ts
@@ -4,10 +4,7 @@ import { join } from "node:path";
import { afterAll, describe, expect, it } from "vitest";
-import {
- buildWithNitroRolldown,
- getSingleRolldownChunk,
-} from "#internal/bundler/nitro-rolldown.js";
+import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
import {
createExtensionScopePlugin,
createFixedNamespaceScopePlugin,
@@ -35,15 +32,14 @@ function scratchModule(source: string): { modulePath: string; sourceRoot: string
}
async function bundle(input: string, plugins: unknown[]): Promise<string> {
- const result = await buildWithNitroRolldown({
+ const chunk = await buildSingleRolldownChunk("test module", {
input,
platform: "node",
plugins,
resolve: { extensions: [".ts", ".js", ".mjs"] },
- write: false,
output: { comments: false, format: "esm" },
});
- return getSingleRolldownChunk(result, "test module").code;
+ return chunk.code;
}
const STATE_MODULE = [
diff --git a/packages/eve/src/internal/bundler/nitro-rolldown.integration.test.ts b/packages/eve/src/internal/bundler/nitro-rolldown.integration.test.ts
new file mode 100644
index 000000000..a2af73bb9
--- /dev/null
+++ b/packages/eve/src/internal/bundler/nitro-rolldown.integration.test.ts
@@ -0,0 +1,51 @@
+import { mkdtempSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+import { describe, expect, it } from "vitest";
+
+import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
+
+function scratchModuleWithDynamicImport(): string {
+ const dir = mkdtempSync(join(tmpdir(), "eve-single-chunk-"));
+ writeFileSync(
+ join(dir, "lazy.ts"),
+ 'export const LAZY_MARKER = "eve-lazy-module-marker";\n',
+ "utf8",
+ );
+ const entryPath = join(dir, "entry.ts");
+ writeFileSync(
+ entryPath,
+ 'export async function loadLazy(): Promise<string> {\n const { LAZY_MARKER } = await import("./lazy");\n return LAZY_MARKER;\n}\n',
+ "utf8",
+ );
+ return entryPath;
+}
+
+describe("buildSingleRolldownChunk", () => {
+ it("inlines dynamic imports into one chunk instead of splitting", async () => {
+ const entryPath = scratchModuleWithDynamicImport();
+
+ const chunk = await buildSingleRolldownChunk("test module", {
+ input: entryPath,
+ platform: "node",
+ resolve: { extensions: [".ts", ".js", ".mjs"] },
+ output: { comments: false, format: "esm" },
+ });
+
+ expect(chunk.code).toContain("eve-lazy-module-marker");
+ });
+
+ it("does not let callers re-enable code splitting through output options", async () => {
+ const entryPath = scratchModuleWithDynamicImport();
+
+ const chunk = await buildSingleRolldownChunk("test module", {
+ input: entryPath,
+ platform: "node",
+ resolve: { extensions: [".ts", ".js", ".mjs"] },
+ output: { codeSplitting: true, comments: false, format: "esm" },
+ });
+
+ expect(chunk.code).toContain("eve-lazy-module-marker");
+ });
+});
diff --git a/packages/eve/src/internal/bundler/nitro-rolldown.ts b/packages/eve/src/internal/bundler/nitro-rolldown.ts
index dbe3f1385..adecb28ae 100644
--- a/packages/eve/src/internal/bundler/nitro-rolldown.ts
+++ b/packages/eve/src/internal/bundler/nitro-rolldown.ts
@@ -17,7 +17,7 @@ type RolldownOutput = {
readonly output: readonly [RolldownOutputChunk, ...(RolldownOutputChunk | RolldownOutputAsset)[]];
};
-export type RolldownBuild = (options: Record<string, unknown>) => Promise<RolldownOutput>;
+type RolldownBuild = (options: Record<string, unknown>) => Promise<RolldownOutput>;
type RolldownParseAst = (
sourceText: string,
options?: Record<string, unknown> | null,
@@ -90,6 +90,11 @@ export async function parseWithNitroRolldownAst(
);
}
+/**
+ * Runs a raw Rolldown build. Prefer {@link buildSingleRolldownChunk} for any
+ * bundle whose consumer expects one in-memory file; use this directly only
+ * for multi-file, written-to-disk output.
+ */
export async function buildWithNitroRolldown(
options: Record<string, unknown>,
): Promise<RolldownOutput> {
@@ -97,10 +102,29 @@ export async function buildWithNitroRolldown(
return await build(options);
}
-export function getSingleRolldownChunk(
- output: RolldownOutput,
+/**
+ * Runs a Rolldown build whose contract is exactly one in-memory chunk:
+ * code splitting is disabled and the result is asserted to contain a
+ * single JavaScript chunk, so dynamic imports are inlined rather than
+ * split into lazy chunks. Every eve single-file bundle (the authored-module
+ * evaluator, immutable development generations, and workflow step/function
+ * bundles) goes through this helper so the single-file policy and its
+ * assertion cannot drift apart. The final Nitro production server build
+ * does not use it and keeps code splitting enabled.
+ */
+export async function buildSingleRolldownChunk(
description: string,
-): RolldownOutputChunk {
+ options: Record<string, unknown> & { readonly output?: Record<string, unknown> },
+): Promise<RolldownOutputChunk> {
+ const result = await buildWithNitroRolldown({
+ ...options,
+ write: false,
+ output: { ...options.output, codeSplitting: false },
+ });
+ return getSingleRolldownChunk(result, description);
+}
+
+function getSingleRolldownChunk(output: RolldownOutput, description: string): RolldownOutputChunk {
const chunks = output.output.filter((item) => item.type === "chunk");
const chunk = chunks[0];
diff --git a/packages/eve/src/internal/workflow-bundle/builder-support.ts b/packages/eve/src/internal/workflow-bundle/builder-support.ts
index 7431c71cb..ec1cefd2b 100644
--- a/packages/eve/src/internal/workflow-bundle/builder-support.ts
+++ b/packages/eve/src/internal/workflow-bundle/builder-support.ts
@@ -5,10 +5,7 @@ import { dirname, join, relative, resolve } from "node:path";
import { atomicWriteFile } from "#shared/atomic-write-file.js";
-import {
- buildWithNitroRolldown,
- getSingleRolldownChunk,
-} from "#internal/bundler/nitro-rolldown.js";
+import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
import { resolveWorkflowModulePath } from "#internal/application/package.js";
import {
applyWorkflowTransform,
@@ -322,7 +319,7 @@ export async function bundleWorkflowStepRegistrations(input: {
...serdeOnlyFiles.map((filePath) => createWorkflowImport(filePath, input.workingDir)),
"export const __steps_registered = true;",
].join("\n");
- const output = await buildWithNitroRolldown({
+ const chunk = await buildSingleRolldownChunk(`step registrations bundle for "${input.outfile}"`, {
cwd: input.workingDir,
input: WORKFLOW_VIRTUAL_ENTRY_ID,
// Optional runtime packages (the just-bash sandbox engine and its
@@ -350,15 +347,12 @@ export async function bundleWorkflowStepRegistrations(input: {
mainFields: ["module", "main"],
},
tsconfig: input.tsconfigPath ?? false,
- write: false,
output: {
- codeSplitting: false,
comments: false,
format: "esm",
sourcemap: "inline",
},
});
- const chunk = getSingleRolldownChunk(output, `step registrations bundle for "${input.outfile}"`);
await writeWorkflowBundleAtomically(input.outfile, chunk.code);
}
@@ -413,20 +407,18 @@ export const POST = workflowEntrypoint(workflowCode, { namespace: ${JSON.stringi
return;
}
- const output = await buildWithNitroRolldown({
+ const chunk = await buildSingleRolldownChunk(`final workflow bundle for "${input.outfile}"`, {
cwd: input.workingDir,
input: WORKFLOW_VIRTUAL_ENTRY_ID,
external: (source: string) => source === "@aws-sdk/credential-provider-web-identity",
platform: "node",
plugins: [createWorkflowVirtualEntryPlugin(workflowFunctionCode)],
- write: false,
output: {
comments: false,
format: input.format,
sourcemap: false,
},
});
- const chunk = getSingleRolldownChunk(output, `final workflow bundle for "${input.outfile}"`);
await writeWorkflowBundleAtomically(input.outfile, chunk.code);
}
diff --git a/packages/eve/src/internal/workflow-bundle/builder.ts b/packages/eve/src/internal/workflow-bundle/builder.ts
index 3aac1c5e9..691b9c177 100644
--- a/packages/eve/src/internal/workflow-bundle/builder.ts
+++ b/packages/eve/src/internal/workflow-bundle/builder.ts
@@ -32,10 +32,7 @@ import {
type WorkflowBundleCreateWorkflowsBundleResult,
type WorkflowBundleDiscoveredEntries,
} from "#internal/workflow-bundle/builder-support.js";
-import {
- buildWithNitroRolldown,
- getSingleRolldownChunk,
-} from "#internal/bundler/nitro-rolldown.js";
+import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
import { writeNitroStepEntrypoint } from "#internal/workflow-bundle/nitro-step-entry.js";
import {
copyNitroFunctionDirectory,
@@ -273,42 +270,39 @@ export class WorkflowBundleBuilder {
...workflowFiles.map((filePath) => createWorkflowImport(filePath, this.config.workingDir)),
...serdeOnlyFiles.map((filePath) => createWorkflowImport(filePath, this.config.workingDir)),
].join("\n");
- const output = await buildWithNitroRolldown({
- cwd: this.config.workingDir,
- input: WORKFLOW_VIRTUAL_ENTRY_ID,
- platform: "neutral",
- plugins: [
- createWorkflowVirtualEntryPlugin(virtualEntrySource),
- createWorkflowPseudoPackagePlugin(),
- createEvePackageImportsPlugin(this.config.workingDir, { workflowCondition: true }),
- createWorkflowTransformPlugin({
- manifest: workflowManifest,
- projectRoot: this.transformProjectRoot,
- sideEffectFiles: [...workflowFiles, ...serdeOnlyFiles],
- workingDir: this.config.workingDir,
- }),
- // Must run after the transform so `"use step"` bodies are already
- // stubbed and their node:* imports stripped from this graph.
- createWorkflowNodeBuiltinGuardPlugin(),
- ],
- resolve: {
- conditionNames: ["eve-source", "workflow", "node", "import", "default"],
- extensions: [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"],
- mainFields: ["module", "main"],
- },
- tsconfig: tsconfigPath ?? false,
- write: false,
- output: {
- banner: "globalThis.__private_workflows = new Map();",
- codeSplitting: false,
- comments: false,
- format: "cjs",
- sourcemap: "inline",
- },
- });
- const interimBundle = getSingleRolldownChunk(
- output,
+ const interimBundle = await buildSingleRolldownChunk(
`intermediate workflow bundle for "${outfile}"`,
+ {
+ cwd: this.config.workingDir,
+ input: WORKFLOW_VIRTUAL_ENTRY_ID,
+ platform: "neutral",
+ plugins: [
+ createWorkflowVirtualEntryPlugin(virtualEntrySource),
+ createWorkflowPseudoPackagePlugin(),
+ createEvePackageImportsPlugin(this.config.workingDir, { workflowCondition: true }),
+ createWorkflowTransformPlugin({
+ manifest: workflowManifest,
+ projectRoot: this.transformProjectRoot,
+ sideEffectFiles: [...workflowFiles, ...serdeOnlyFiles],
+ workingDir: this.config.workingDir,
+ }),
+ // Must run after the transform so `"use step"` bodies are already
+ // stubbed and their node:* imports stripped from this graph.
+ createWorkflowNodeBuiltinGuardPlugin(),
+ ],
+ resolve: {
+ conditionNames: ["eve-source", "workflow", "node", "import", "default"],
+ extensions: [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"],
+ mainFields: ["module", "main"],
+ },
+ tsconfig: tsconfigPath ?? false,
+ output: {
+ banner: "globalThis.__private_workflows = new Map();",
+ comments: false,
+ format: "cjs",
+ sourcemap: "inline",
+ },
+ },
);
await bundleFinalWorkflowOutput({
diff --git a/packages/eve/src/internal/workflow-bundle/vercel-workflow-output.ts b/packages/eve/src/internal/workflow-bundle/vercel-workflow-output.ts
index 6a4bd48b3..31f6e3f39 100644
--- a/packages/eve/src/internal/workflow-bundle/vercel-workflow-output.ts
+++ b/packages/eve/src/internal/workflow-bundle/vercel-workflow-output.ts
@@ -12,11 +12,7 @@ import {
import type { Dirent } from "node:fs";
import { basename, dirname, extname, join, relative } from "node:path";
-import {
- buildWithNitroRolldown,
- getSingleRolldownChunk,
- type RolldownBuild,
-} from "#internal/bundler/nitro-rolldown.js";
+import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
import {
EVE_SHARED_SERVER_FUNCTION_PATH,
isEveVercelFunctionPath,
@@ -305,36 +301,32 @@ export async function emitBundledWorkflowFunctionDirectory(input: {
await prepareVercelFunctionDirectory(input.targetPath);
const pluginModulePaths = await emitBundledWorkflowPluginModules({
- build: buildWithNitroRolldown,
pluginPaths: input.pluginPaths ?? [],
targetPath: input.targetPath,
});
const entrypointId = join(dirname(input.bundlePath), "__eve_workflow_function_entry.js");
- const result = await buildWithNitroRolldown({
- cwd: dirname(input.bundlePath),
- input: entrypointId,
- platform: "node",
- plugins: [
- createVirtualModulePlugin({
- id: entrypointId,
- moduleType: "js",
- source: createWorkflowFunctionEntrypointSource({
- bundlePath: input.bundlePath,
- pluginModulePaths,
+ const outputFile = await buildSingleRolldownChunk(
+ `Vercel workflow function for "${input.bundlePath}"`,
+ {
+ cwd: dirname(input.bundlePath),
+ input: entrypointId,
+ platform: "node",
+ plugins: [
+ createVirtualModulePlugin({
+ id: entrypointId,
+ moduleType: "js",
+ source: createWorkflowFunctionEntrypointSource({
+ bundlePath: input.bundlePath,
+ pluginModulePaths,
+ }),
}),
- }),
- ],
- write: false,
- output: {
- codeSplitting: false,
- comments: false,
- format: "cjs",
- sourcemap: false,
+ ],
+ output: {
+ comments: false,
+ format: "cjs",
+ sourcemap: false,
+ },
},
- });
- const outputFile = getSingleRolldownChunk(
- result,
- `Vercel workflow function for "${input.bundlePath}"`,
);
await Promise.all([
@@ -360,24 +352,20 @@ export async function emitBundledWorkflowFunctionDirectory(input: {
}
async function emitBundledWorkflowPluginModules(input: {
- build: RolldownBuild;
pluginPaths: readonly string[];
targetPath: string;
}): Promise<string[]> {
return await Promise.all(
input.pluginPaths.map(async (pluginPath, index) => {
- const result = await input.build({
+ const outputFile = await buildSingleRolldownChunk(`workflow plugin for "${pluginPath}"`, {
input: pluginPath,
platform: "node",
- write: false,
output: {
- codeSplitting: false,
comments: false,
format: "esm",
sourcemap: false,
},
});
- const outputFile = getSingleRolldownChunk(result, `workflow plugin for "${pluginPath}"`);
const emittedPluginFilename = `__eve_workflow_plugin_${index}.mjs`;
await writeFile(join(input.targetPath, emittedPluginFilename), outputFile.code);