The staging directory was dist/.build-XXXXXX. A build that is killed cannot remove it, the next build removed only its own, and "files": ["dist"] put the leftover into the npm tarball. .gitignore hid it from git status and the freshness test skipped every name starting with ".build-", so nothing reported it. The staging directory is now .dist-staging beside the output (.<name>-staging for --out <dir>) and is cleared at the start of each build. The freshness test no longer skips anything: a stray file under dist/ fails it. Checked in a scratch copy: a build stopped with SIGKILL after 1.5 s leaves .dist-staging/ and an unchanged dist/; npm pack lists 36 files, none from the staging directory; the next build removes it. New test for the same on a temporary package. npm test: 28 pass in 8.2-9.5 s. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
55 lines
2.7 KiB
JavaScript
55 lines
2.7 KiB
JavaScript
// npm run build - the only writer of dist/.
|
|
//
|
|
// node scripts/build.mjs [--out <dir>] <dir> defaults to dist/ in the package
|
|
//
|
|
// Two compiler runs over src/: CommonJS with its declarations (cjs/, types/), then ES modules with theirs (esm/).
|
|
// TypeScript reads a .d.ts as CommonJS or as an ES module by the package.json above it, so each build needs its own
|
|
// set: the one in types/ would tell an ES module consumer that a default import works.
|
|
//
|
|
// Both runs write to a staging directory that replaces the three directories only after both passed, so a failed
|
|
// build leaves the tracked dist/ as it was. Files of sources that no longer exist go with the old directories.
|
|
import { spawnSync } from "node:child_process";
|
|
import { mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import { basename, dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const tsc = createRequire(import.meta.url).resolve("typescript/bin/tsc");
|
|
|
|
function outputDirectory(args) {
|
|
const at = args.indexOf("--out");
|
|
if (at === -1) return join(root, "dist");
|
|
if (!args[at + 1]) throw new Error("--out needs a directory");
|
|
return resolve(args[at + 1]);
|
|
}
|
|
|
|
function compile(project, options) {
|
|
const run = spawnSync(process.execPath, [tsc, "-p", join(root, project), ...options], { cwd: root, stdio: "inherit" });
|
|
if (run.error) throw run.error;
|
|
if (run.status !== 0) throw new Error(`tsc -p ${project} failed, ${out} is unchanged`);
|
|
}
|
|
|
|
let out;
|
|
let staging;
|
|
try {
|
|
out = outputDirectory(process.argv.slice(2));
|
|
// Beside the output, not in it: the package ships all of dist/, and a build that is killed cannot remove its
|
|
// staging directory - the next build does. Same parent directory, so the renames below stay on one filesystem.
|
|
staging = join(dirname(out), `.${basename(out)}-staging`);
|
|
rmSync(staging, { recursive: true, force: true });
|
|
compile("tsconfig.cjs.json", ["--outDir", join(staging, "cjs"), "--declarationDir", join(staging, "types")]);
|
|
compile("tsconfig.esm.json", ["--outDir", join(staging, "esm")]);
|
|
// The package is "type": "commonjs"; this marks the .js files under esm/ as ES modules.
|
|
writeFileSync(join(staging, "esm", "package.json"), JSON.stringify({ type: "module" }, null, 2) + "\n");
|
|
mkdirSync(out, { recursive: true });
|
|
for (const part of ["cjs", "esm", "types"]) {
|
|
rmSync(join(out, part), { recursive: true, force: true });
|
|
renameSync(join(staging, part), join(out, part));
|
|
}
|
|
} catch (err) {
|
|
console.error(`build: ${err.message}`);
|
|
process.exitCode = 1;
|
|
} finally {
|
|
if (staging) rmSync(staging, { recursive: true, force: true });
|
|
}
|