"exports" gains "import" and "types" next to "require": import "alex2node" failed with ERR_PACKAGE_PATH_NOT_EXPORTED. scripts/build.mjs compiles src/ to dist/cjs, dist/esm and dist/types in a staging directory and swaps them in only when both compiler runs passed. "main" and "types" follow for resolvers that do not read "exports". Target ES6 -> ES2020, "engines": node >= 18, relative imports carry ".js" for Node's ESM loader. uuid gives way to crypto.randomUUID(); mqtt is the only runtime dependency left. Tests load the package by name. New: both entry points, the modules the builds import, dist/ equal to a fresh build, a round trip on the ES module build, and two compiled type fixtures in place of the .d.ts regexes. 22 tests pass in 5.7 s. dist/: 16 files, 79,711 B -> 25 files, 132,291 B; the npm tarball is 25,125 B. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
49 lines
2.2 KiB
JavaScript
49 lines
2.2 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 the declarations (cjs/, types/), then ES modules (esm/). Both write to a
|
|
// staging directory that replaces the three directories only after both runs 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, mkdtempSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import { 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));
|
|
mkdirSync(out, { recursive: true });
|
|
staging = mkdtempSync(join(out, ".build-"));
|
|
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");
|
|
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 });
|
|
}
|