Files
hyperframes/scripts/check-cli-lazy-deps.test.mjs
Miguel Ángel 5583ae4293 perf(cli): commands run without esbuild, puppeteer-core or sharp until they need them (#5930)
* perf(cli): commands run without esbuild, puppeteer-core or sharp until they need them

A host that bundles the CLI can now leave these packages out: each command
loads them only where it uses them, and sharp installs once on first use when
it is missing. The build check fails on any static import of them again.

* fix(cli): use a host's sharp from NODE_PATH and let hosts turn first-use installs off

* fix(cli): capture fails when sharp is missing, and one install serves every caller

A missing sharp now stops the asset download up front instead of counting every
image as unavailable. Concurrent first uses share one install, a failed install
is not retried in the same run, and preview thumbnails report why. Hosts can turn
off installs for a list of packages, e.g. HYPERFRAMES_NO_OPTIONAL_INSTALL=sharp.

* fix(cli): snapshot --describe still works without sharp, resizing only when it is installed

* style(cli): format the sharp loader and its tests

* build(cli): sharp is an optional dependency, so a host can ship the CLI without it

* build(studio-server): keep sharp out of the bundle now that it is optional
2026-10-20 09:12:47 +00:00

49 lines
1.9 KiB
JavaScript

import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { checkCliLazyDeps, listStaticLazyImports } from "./check-cli-lazy-deps.mjs";
test("flags static imports and re-exports of a lazy package, across lines", () => {
const source = [
'import sharp from "sharp";',
"import {",
" CDPSession",
'} from "puppeteer-core";',
'export { transform } from "esbuild";',
'import "esbuild/lib/main.js";',
].join("\n");
assert.deepEqual(listStaticLazyImports(source, "chunk.js"), [
"chunk.js:1 imports sharp statically",
"chunk.js:2 imports puppeteer-core statically",
"chunk.js:5 imports esbuild statically",
"chunk.js:6 imports esbuild/lib/main.js statically",
]);
});
test("allows dynamic imports, type-only imports, look-alike names and quoted code", () => {
const source = [
'const { transform } = await import("esbuild");',
'import type { Page } from "puppeteer-core";',
'import sharpish from "sharp-ish";',
'const doc = `import sharp from "sharp";`;',
].join("\n");
assert.deepEqual(listStaticLazyImports(source), []);
});
test("scans every built file, nested ones too, and refuses a missing build", () => {
const root = mkdtempSync(join(tmpdir(), "hf-lazy-deps-"));
try {
assert.match(checkCliLazyDeps(root)[0], /missing; build the CLI first/);
mkdirSync(join(root, "packages/cli/dist/commands"), { recursive: true });
writeFileSync(join(root, "packages/cli/dist/cli.js"), 'await import("sharp");');
writeFileSync(join(root, "packages/cli/dist/commands/x.js"), 'import sharp from "sharp";');
assert.deepEqual(checkCliLazyDeps(root), [
"packages/cli/dist/commands/x.js:1 imports sharp statically",
]);
} finally {
rmSync(root, { recursive: true, force: true });
}
});