Skip to content

library-build

The build pipeline of a TypeScript LIBRARY package: one dist/lib/{cjs,esm} tree of unbundled modules beside its .d.cts / .d.mts declarations, the generated index.ts barrels, and the generated package.json exports map.

Each step works on the current working folder, which is the package being built. A repository’s scripts/build-*.ts entry points are thin wrapCliTask wrappers around these functions, and its build script runs them in order through runBuildSteps.

Interface Description
BuildLibraryOptions Options for buildLibrary.
GenerateLibraryExportsOptions Options for generateLibraryExports.
GenerateLibraryIndexOptions Options for generateLibraryIndex.
Function Description
buildCleanLibrary Removes everything an earlier build left: dist/, the generated index.ts barrels, the .d.ts files tsc wrote beside the sources (hand-authored ones under @types stay), and the per-subpath package.json stubs.
buildLibrary Builds every source module, unbundled, into dist/lib/cjs (.cjs) and dist/lib/esm (.mjs). The output folders must already exist; buildLibraryTypes creates them.
buildLibraryTypes Emits the declarations with tsc --project ./tsconfig.types.json, then copies each one into dist/lib twice: as a .d.cts whose relative imports name .cjs files and whose bare type imports carry resolution-mode: import, and as a .d.mts whose relative imports name .mjs files. The library’s root declaration (library.ts where the package has one, otherwise the generated index.ts) carries every lib and types reference the package needs. Every other declaration references a library-references declaration beside it by path, never the root: the root barrel re-exports every module, so referencing it made each subpath load the whole package, and a consumer type-checking with skipLibCheck: false then needed every optional peer installed. library-references carries only the libs that describe the language (es2022, esnext.disposable), which no consumer conflicts with. The ones that describe a host (dom, dom.iterable, webworker) and types="node" go only into the declarations that name one of their globals, found by resolving every name in the emitted declarations with the type checker. Otherwise a browser consumer of a pure subpath got all of @types/node, and its setTimeout returned NodeJS.Timeout.
generateLibraryExports Regenerates the exports map of the package’s package.json from its source folders, and writes a package.json stub per subpath for resolvers that do not read exports.
generateLibraryIndex Generates an index.ts barrel in every source folder that has something to export, re-exporting each module and each sub-barrel under its own name.
runBuildSteps Runs each named npm script the package defines, in order, skipping the ones it does not define.
Variable Description
LIBRARY_BANNER The banner every emitted library module opens with.
LIBRARY_BUILD_STEPS The steps of a library build, in the order runBuildSteps runs them.
SOURCE_CONDITION The export condition a repository holding several packages maps every subpath onto its TypeScript source with, so a package there can import a sibling package’s source before anything is built. Pass it to generateLibraryExports through GenerateLibraryExportsOptions.sourceCondition.