Files
dev/scripts/embed-dist-check.ts
zeekay 41f8ea456f @hanzo/dev-core 0.1.13: the library keeps use-sync-external-store and the router the host's, and the release is gated on it
dist/utils-*.js, presentation-*.js and useSettings-*.js carried bundled CommonJS copies of
use-sync-external-store (behind @tanstack/react-router and react-store) whose require("react") a
Vite 8 / rolldown host cannot answer: /dev threw 'Calling require for "react"' and drew 'Something
broke'. They are external now and listed as dependencies. scripts/embed-dist-check.ts fails the publish
when a file of dist calls require("react") (the minified form is n("react")) or imports a package
package.json does not list.

Verified in a Vite 8.3.8 app (React 19, the packed tarball; build+preview and dev, Chromium): 0.1.11 throws
the require error and draws nothing; this draws the workspace with no console error.

Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV
2026-11-21 02:31:00 -08:00

38 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
// @effect-diagnostics nodeBuiltinImport:off globalConsole:off - A one-shot release gate; synchronous Node APIs keep it small.
// The embedded Dev core is bundled by its host. A host's ES-module bundler cannot answer a CommonJS
// `require("react")`, so no file of the library may call one (the minified helper reads `n("react")`),
// and every package the library imports bare must be one the host installs: a dependency or a peer.
import * as NodeFS from "node:fs";
import * as NodePath from "node:path";
const root = NodePath.resolve(import.meta.dirname, "..", "packages", "dev");
const dist = NodePath.join(root, "dist");
const manifest = JSON.parse(NodeFS.readFileSync(NodePath.join(root, "package.json"), "utf8"));
const installed = new Set([
...Object.keys(manifest.dependencies ?? {}),
...Object.keys(manifest.peerDependencies ?? {}),
]);
// A statement that starts a line: `import "x"`, `import { a } from "x"`, `export * from "x"`, and `import("x")`.
const IMPORTED = /^(?:import|export)\s*(?:[^'";]*?\bfrom\s*|\s*)["']([A-Za-z@][^"']*)["']|\bimport\(\s*["']([A-Za-z@][^"']*)["']\s*\)/gm;
const packageOf = (specifier: string) => specifier.split("/").slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
const files = NodeFS.readdirSync(dist, { recursive: true, encoding: "utf8" }).filter((name) => name.endsWith(".js"));
const problems: string[] = [];
for (const name of files) {
const code = NodeFS.readFileSync(NodePath.join(dist, name), "utf8");
if (/\(\s*["']react["']\s*\)/.test(code)) problems.push(`${name} calls require("react")`);
for (const match of code.matchAll(IMPORTED)) {
const specifier = (match[1] ?? match[2])!;
if (!installed.has(packageOf(specifier))) {
problems.push(`${name} imports ${specifier}, which package.json does not list as a dependency or a peer`);
}
}
}
if (files.length === 0) problems.push("dist holds no files: the library was not built");
if (problems.length > 0) {
console.error([...new Set(problems)].join("\n"));
process.exit(1);
}
console.log(`embed dist: ${files.length} files, no require of react, every bare import is installed by the host`);