perf(cli): bundle runtime entries to reduce Windows startup I/O (#1996)

Bundle ordinary CLI and output entries to reduce Windows module filesystem resolution while preserving source URLs, lazy loading and worker resources. Keep release identity in its shared owning module.

Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
Ysopwnable
2026-10-12 09:25:27 +08:00
committed by GitHub
co-authored by Kaoru0822
parent 6611bcf6e8
commit d93fbda390
13 changed files with 243 additions and 139 deletions
@@ -4,15 +4,16 @@ import { pathToFileURL } from "node:url";
import { describe, expect } from "vitest";
import { bundleMcpRuntime } from "../../../scripts/bundle-mcp-runtime.mjs";
import { bundleRuntime } from "../../../scripts/bundle-runtime.mjs";
import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js";
describe("MCP runtime bundle", () => {
describe("CLI and MCP runtime bundles", () => {
workspaceCliTest(
"preserves module locations, lazy side effects, package resolution and worker entrypoints",
async ({ processes, workspace }) => {
await workspace.mkdir("dist/provider");
await workspace.mkdir("dist/composition");
await workspace.mkdir("dist/cli");
await workspace.mkdir("bridge");
await workspace.mkdir("node_modules/external-probe");
const files = {
@@ -45,8 +46,17 @@ globalThis.effects.push('lazy');
export const lazyUrl = import.meta.url;`,
"dist/composition/javascriptAnalysisWorkerProcess.js":
'process.stdout.write("worker-entry");',
"dist/generatedPackageMetadata.js":
'export const generatedPackageMetadata = { version: "1.0.0" };',
"dist/cli.js": 'export { facts, run } from "./main.js";',
"dist/cliOutput.js": 'export { facts } from "./provider/probe λ.js";',
"dist/cli/streamedJsonOutput.js":
'export { facts } from "../provider/probe λ.js";',
"dist/mcpDoctor.js": 'export { run } from "./main.js";',
"dist/main.js": `
import { facts } from './provider/probe λ.js';
import { generatedPackageMetadata } from './generatedPackageMetadata.js';
export { facts };
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
globalThis.effects.push('main');
@@ -54,7 +64,7 @@ export const run = async () => {
const beforeLazy = [...globalThis.effects];
const { lazyUrl } = await import('./lazy.js');
return {
...facts, beforeLazy, afterLazy: globalThis.effects, lazyUrl,
...facts, version: generatedPackageMetadata.version, beforeLazy, afterLazy: globalThis.effects, lazyUrl,
workerOutput: execFileSync(process.execPath, [fileURLToPath(facts.worker)], { encoding: 'utf8' })
};
};`,
@@ -76,9 +86,23 @@ export const run = async () => {
return JSON.parse(result.stdout) as unknown;
};
const original = await invoke("dist/main.js");
await bundleMcpRuntime(workspace.root);
await bundleRuntime(workspace.root);
const bundled = await invoke("dist/mcp/main.js");
expect(bundled).toEqual(original);
expect(await invoke("dist/cli-runtime/cli.js")).toEqual(
await invoke("dist/cli.js"),
);
const cliDirectory = join(workspace.root, "dist/cli-runtime");
const identity = await processes.run(
process.execPath,
[
"--input-type=module",
"--eval",
`const { facts } = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cli.js")).href)}); const output = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cliOutput.js")).href)}); const streamed = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "streamedJsonOutput.js")).href)}); process.stdout.write(JSON.stringify(facts === output.facts && facts === streamed.facts));`,
],
{ cwd: workspace.root },
);
expect(identity).toMatchObject({ exitCode: 0, stdout: "true" });
expect(bundled).toMatchObject({
resource: "bridge resource\n",
external: "external",
@@ -93,6 +117,14 @@ export const run = async () => {
expect(
(await readdir(join(workspace.root, "dist/mcp"))).length,
).toBeGreaterThan(1);
await workspace.write(
"dist/generatedPackageMetadata.js",
'export const generatedPackageMetadata = { version: "0.0.0" };',
);
const previousRelease = await invoke("dist/main.js");
expect(previousRelease).toMatchObject({ version: "0.0.0" });
expect(await invoke("dist/mcp/main.js")).toEqual(previousRelease);
expect(await invoke("dist/cli-runtime/cli.js")).toEqual(previousRelease);
},
);
@@ -100,18 +132,26 @@ export const run = async () => {
"rejects module-relative resolution and unknown import metadata during the build",
async ({ workspace }) => {
await workspace.mkdir("dist");
await workspace.mkdir("dist/cli");
await workspace.write("package.json", '{"type":"module"}');
for (const expression of [
"import.meta.resolve('./worker.js')",
"import.meta.env",
]) {
await workspace.write(
"dist/main.js",
`export const value = ${expression};`,
);
await expect(bundleMcpRuntime(workspace.root)).rejects.toThrow(
"Unsupported import.meta use in MCP bundle",
);
for (const path of [
"dist/cliOutput.js",
"dist/cli/streamedJsonOutput.js",
"dist/mcpDoctor.js",
])
await workspace.write(path, "export {};\n");
for (const entry of ["dist/main.js", "dist/cli.js"]) {
for (const expression of [
"import.meta.resolve('./worker.js')",
"import.meta.env",
]) {
await workspace.write("dist/main.js", "export {};\n");
await workspace.write("dist/cli.js", "export {};\n");
await workspace.write(entry, `export const value = ${expression};`);
await expect(bundleRuntime(workspace.root)).rejects.toThrow(
"Unsupported import.meta use in runtime bundle",
);
}
}
},
);