fix(coding-agent): prevented puppeteer startup crashes

- Avoided value-imports of `puppeteer-core` in main startup files to prevent eager loading of the browser-data dependency graph.
- Used `import type` and literal string checks to defer `puppeteer-core` initialization until browser tools are actually invoked.
- Added a test to verify that `puppeteer-core` and `@puppeteer/browsers` remain off the eager startup import graph.
- Externalized additional heavy dependencies to reduce bundle size and improved minification settings in the build script.
This commit is contained in:
can1357
2026-06-18 17:21:09 +02:00
parent f0a383332b
commit 8427e69e1e
5 changed files with 54 additions and 15 deletions
+29 -12
View File
@@ -9,6 +9,31 @@ const outDir = path.join(packageDir, "dist");
const cliPath = path.join(outDir, "cli.js");
const shebang = "#!/usr/bin/env bun\n";
// Native / optional / platform-specific deps that are never bundled — installed on
// demand (transformers/fastembed/onnxruntime) or shipped as their own artifact
// (native addon, mupdf).
const ALWAYS_EXTERNAL = ["mupdf", "@oh-my-pi/pi-natives", "@huggingface/transformers", "fastembed", "onnxruntime-node"];
// Heavy, lazily-used third-party leaf deps. Each is a declared `dependency`, so the
// published package resolves it from node_modules at runtime; bundling only embeds a
// redundant copy that bloats dist/cli.js. NEVER add a patched dependency here — the
// bundle is where a root `patchedDependencies` patch is baked in, so an externalized
// import would load the unpatched npm package in users' installs (currently
// beautiful-mermaid and @ark/schema are patched, so they — and arktype, which pulls
// @ark/schema — stay bundled).
const RUNTIME_EXTERNAL = [
"puppeteer-core",
"@puppeteer/browsers",
"@babel/parser",
"@xterm/headless",
"turndown",
"turndown-plugin-gfm",
"@mozilla/readability",
"linkedom",
"markit-ai",
"@agentclientprotocol/sdk",
];
async function runCommand(command: string[]): Promise<void> {
const proc = Bun.spawn(command, {
cwd: packageDir,
@@ -63,19 +88,11 @@ async function main(): Promise<void> {
"--target=bun",
"--outdir",
"dist",
"--minify-whitespace",
"--minify-syntax",
// Full minify (whitespace + syntax + identifiers); --keep-names retains
// fn/class .name where code depends on it.
"--minify",
"--keep-names",
"--external",
"mupdf",
"--external",
"@oh-my-pi/pi-natives",
"--external",
"@huggingface/transformers",
"--external",
"fastembed",
"--external",
"onnxruntime-node",
...[...ALWAYS_EXTERNAL, ...RUNTIME_EXTERNAL].flatMap(dep => ["--external", dep]),
"--define",
'process.env.PI_BUNDLED="true"',
"./src/cli.ts",