The Open Framework for Building Agents
fix(eve): keep Nitro preset named exports behind the instrumentation entry (#4318)
e0-gh-vercel-connect[bot]106693284601
diff --git a/.changeset/keep-nitro-preset-named-exports.md b/.changeset/keep-nitro-preset-named-exports.mdnew file mode 100644index 000000000..8be295268--- /dev/null+++ b/.changeset/keep-nitro-preset-named-exports.md@@ -0,0 +1,5 @@+---+"eve": patch+---++`eve build` keeps the named exports of the Nitro preset entry. Builds with presets such as `NITRO_PRESET=node-middleware` export `middleware` and `handleUpgrade` again instead of only `default`.diff --git a/packages/eve/src/internal/nitro/host/instrumentation-entry.scenario.test.ts b/packages/eve/src/internal/nitro/host/instrumentation-entry.scenario.test.tsindex 125d273ea..610fa2656 100644--- a/packages/eve/src/internal/nitro/host/instrumentation-entry.scenario.test.ts+++ b/packages/eve/src/internal/nitro/host/instrumentation-entry.scenario.test.ts@@ -21,11 +21,19 @@ vi.mock("#internal/nitro/host/vercel-build-prewarm.js", () => ({const execFileAsync = promisify(execFile);const createDirectory = useTemporaryDirectories();const createAppRoot = useTemporaryAppRoots();+const ENTRY_TARGETS = [+ "vercel-web",+ "vercel-node",+ "node-server",+ "node-middleware",+ "development",+ "setup-failure",+];afterEach(() => vi.unstubAllEnvs());describe("instrumentation before the Nitro entry", () => {- it.each(["vercel-web", "vercel-node", "node-server", "development", "setup-failure"])(+ it.each(ENTRY_TARGETS)("awaits asynchronous setup before application imports with %s",async (target) => {const root = await createDirectory("eve-instrumentation-entry-");@@ -61,7 +69,7 @@ describe("instrumentation before the Nitro entry", () => {const nitro = await createNitro({rootDir: root,dev: target === "development",- preset: target === "node-server" ? "node-server" : "vercel",+ preset: target.startsWith("node-") ? target : "vercel",vercel: { entryFormat: target === "vercel-node" ? "node" : "web" },logLevel: 0,plugins: [applicationPath, instrumentationPath],@@ -85,10 +93,11 @@ describe("instrumentation before the Nitro entry", () => {"-e",`globalThis.startup = [];try {- const { default: server } = await import(${JSON.stringify(entry)});+ const { default: server, middleware } = await import(${JSON.stringify(entry)});await import(${JSON.stringify(entry)});if (${JSON.stringify(target)} === 'vercel-web' && typeof server.fetch !== 'function') throw new Error('Missing Vercel fetch handler');if (${JSON.stringify(target)} === 'vercel-node' && typeof server !== 'function') throw new Error('Missing Vercel Node handler');+ if (${JSON.stringify(target)} === 'node-middleware' && typeof middleware !== 'function') throw new Error('Missing Node middleware handler');if (${JSON.stringify(target)} === 'development' && typeof server.ipc.onClose !== 'function') throw new Error('Missing development close handler');await globalThis.closeNitro();} catch (error) {diff --git a/packages/eve/src/internal/nitro/host/instrumentation-entry.ts b/packages/eve/src/internal/nitro/host/instrumentation-entry.tsindex 6d4baefc8..f87b899d5 100644--- a/packages/eve/src/internal/nitro/host/instrumentation-entry.ts+++ b/packages/eve/src/internal/nitro/host/instrumentation-entry.ts@@ -4,19 +4,41 @@ import { stringifyEsmImportSpecifier } from "#internal/application/import-specifconst INSTRUMENTATION_ENTRY = "#eve/instrumentation-entry";+interface EntryPluginContext {+ resolve(source: string): Promise<{ id: string } | null>;+ load(options: { id: string }): Promise<{ exports: readonly string[] | null }>;+}+/** Awaits instrumentation before importing the preset's server dependency graph. */export function configureInstrumentationEntry(nitro: Nitro, instrumentationPath: string): void {// Nitro's development preset otherwise collapses both startup phases into one module.nitro.options.inlineDynamicImports = false;- nitro.options.virtual[INSTRUMENTATION_ENTRY] = () =>- [- `await import(${stringifyEsmImportSpecifier(instrumentationPath)});`,- `const { default: server } = await import(${stringifyEsmImportSpecifier(nitro.options.entry)});`,- "export default server;",- ].join("\n");// Presets finish selecting their runtime entry in build:before.nitro.hooks.hook("rollup:before", (_nitro, config) => {config.input = INSTRUMENTATION_ENTRY;+ config.plugins = [+ {+ name: "eve:instrumentation-entry",+ resolveId: (id: string) => (id === INSTRUMENTATION_ENTRY ? id : null),+ async load(this: EntryPluginContext, id: string) {+ if (id !== INSTRUMENTATION_ENTRY) return null;+ const entry = await this.resolve(nitro.options.entry);+ if (entry === null) throw new Error(`Cannot resolve Nitro entry ${nitro.options.entry}`);+ // Re-export every preset binding, e.g. node-middleware exposes only named exports.+ const { exports } = await this.load(entry);+ return [+ `await import(${stringifyEsmImportSpecifier(instrumentationPath)});`,+ `const server = await import(${stringifyEsmImportSpecifier(nitro.options.entry)});`,+ ...(exports ?? []).map((name) =>+ name === "default"+ ? "export default server.default;"+ : `export const ${name} = server.${name};`,+ ),+ ].join("\n");+ },+ },+ ...(config.plugins ?? []),+ ];});}