pi-mono/packages/chord/test/node/workers/child.test.ts
Mario Zechner 7ac1a9e3a0 feat(chord): split the worker base at dynamic imports; workers/child entry
buildWorkerBase bundles with esbuild code splitting into one private directory: the entry
and its static chunks load at each fork, a lazily imported module and the packages it
imports only when imported. @earendil-works/chord/workers/child exports the worker's side
(workerMain, processLock) without esbuild.
2026-11-05 08:49:17 +01:00

28 lines
1.3 KiB
TypeScript

// Tests of the worker's side as its own entry point (`@earendil-works/chord/workers/child`): importing it loads no
// esbuild, so a forked worker does not pay for the server's worker base build.
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
describe("the child entry's imports", () => {
it("loads no esbuild: a worker importing workerMain and processLock never imports it", () => {
const child = fileURLToPath(new URL("../../../src/node/workers/child.ts", import.meta.url));
// A hook refusing esbuild, then the child entry imported.
const script = [
'import { registerHooks } from "node:module";',
"registerHooks({ resolve(specifier, context, next) {",
' if (specifier === "esbuild") throw new Error("esbuild imported");',
" return next(specifier, context);",
"} });",
`const { processLock, workerMain } = await import(${JSON.stringify(child)});`,
"console.log(typeof processLock, typeof workerMain);",
].join("\n");
const output = execFileSync(
process.execPath,
["--experimental-strip-types", "--no-warnings", "--input-type=module", "--eval", script],
{ encoding: "utf8" },
);
expect(output.trim()).toBe("function function");
});
});