// pnpm hooks for packing the public package train, and nothing else: passed to `pnpm pack` with // `--pnpmfile` by scripts/package-release.ts, never installed as the workspace's .pnpmfile.cjs, so // installs and the lockfile are unaffected. // // `beforePacking` runs after pnpm has rewritten `workspace:` ranges into exact versions, and does two // things to the packed package.json only: // // - It drops `devDependencies`. A consumer never installs them, and a tooling package's test-only // development dependencies name private workspace packages (Core, the module template) that are // not part of the public package train. // - It puts every remaining dependency map back in the source package.json's own key order. pnpm // rewrites `workspace:` ranges concurrently and inserts each entry as its lookup resolves, so two // packs of the same commit could order those keys differently, the one difference that made // otherwise byte-identical tarballs differ. No name or range changes. "use strict"; const { readFileSync } = require("node:fs"); const path = require("node:path"); const DEPENDENCY_FIELDS = [ "dependencies", "optionalDependencies", "peerDependencies", "peerDependenciesMeta", ]; function inSourceOrder(packed, source) { if ( packed === null || typeof packed !== "object" || source === null || typeof source !== "object" ) { return packed; } const ordered = {}; for (const key of Object.keys(source)) { if (Object.hasOwn(packed, key)) ordered[key] = packed[key]; } for (const key of Object.keys(packed)) { if (!Object.hasOwn(ordered, key)) ordered[key] = packed[key]; } return ordered; } function beforePacking(manifest, directory) { const source = JSON.parse(readFileSync(path.join(directory, "package.json"), "utf8")); const packed = { ...manifest }; delete packed.devDependencies; for (const field of DEPENDENCY_FIELDS) { if (packed[field] !== undefined) packed[field] = inSourceOrder(packed[field], source[field]); } return packed; } module.exports = { hooks: { beforePacking }, inSourceOrder };