import fs from 'node:fs'; import path from 'node:path'; import type { UserConfig } from 'tsdown'; import { bundledNotices } from './bundle-in-cjs.mjs'; import { isOneOf, ownSourceOnly } from './own-source-only.mjs'; /** * The one library preset (see tooling/build/package.json for scope). * * The package.json `exports` map is the source of truth: it lists the curated * public entries as src/ paths (the repo's source-first pattern — workspace * consumers resolve TS source directly, no watch builds). epdf-build derives * the tsdown entries from that map, and tsdown regenerates both sides on every * build: dev `exports` (src) stay normalized in place, publish `exports` * (dist, import + require conditions) land in `publishConfig`. Internal * modules simply aren't listed, wherever they live in src/. */ export function presetConfig(overrides: UserConfig = {}): UserConfig { const pkg = JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')); const raw: string[] = pkg.epdf?.rawExports ?? []; const rawEntries = Object.fromEntries( raw.map((subpath: string) => [subpath, pkg.exports?.[subpath]]), ); // epdf.conditions (package.json): extra export CONDITIONS per subpath, // written first (conditions match in order) into BOTH maps, always as dist // paths. For resolvers that must land on a different build of an entry — // e.g. `es2020` (Angular's application builder) → the portable engine. const conditions: Record> = pkg.epdf?.conditions ?? {}; // epdf.neverBundle (package.json): specifiers left as they are in dist, for // the consumer's resolver: a `package.json` `imports` entry whose target // depends on where the code runs (the engine's `#inline-transport`). Each // target is a file of the package: Turbopack doesn't follow one that names // another package. const neverBundle: string[] = pkg.epdf?.neverBundle ?? []; // epdf.bundleInCjs (package.json): dependencies the CommonJS output holds inside it, code and // declarations, while the ES module output goes on importing them (see bundle-in-cjs.mjs). // Every package that ends up inside is listed, the ones a listed package uses too: the build // fails on one that isn't. const bundleInCjs: string[] = pkg.epdf?.bundleInCjs ?? []; const heldInCjs = (id: string) => isOneOf(bundleInCjs, id); if (bundleInCjs.length > 0 && neverBundle.length > 0) { throw new Error( `${pkg.name}: "epdf.bundleInCjs" and "epdf.neverBundle" can't be used together`, ); } const withConditions = (generated: Record) => { const out: Record = { ...generated }; for (const [subpath, extra] of Object.entries(conditions)) { const entry = out[subpath]; out[subpath] = entry && typeof entry === 'object' ? { ...extra, ...(entry as Record) } : { ...extra, default: entry }; } return out; }; return { entry: entriesFromExports(pkg, raw), format: bundleInCjs.length > 0 ? { esm: {}, // A format's settings replace the build's, so this is the whole of `deps` for the // CommonJS output, in the shape tsdown resolves it to. cjs: { deps: { alwaysBundle: heldInCjs, onlyBundle: bundleInCjs, skipNodeModulesBundle: false, resolveDepSubpath: true, dts: { alwaysBundle: heldInCjs }, }, }, } : ['esm', 'cjs'], platform: 'neutral', target: 'es2020', dts: true, sourcemap: true, exports: { // Default: pure source-first (dev exports -> src, publish -> dist). // 'development' (epdf.devExports): condition-split for boundary packages // whose source needs wider libs (DOM/ES2022) than their consumers allow — // TS resolves dist .d.ts (skipLibCheck shields consumers' stricter or // narrower tsconfigs), while Vite's dev server still reads src for HMR // via the `development` condition. devExports: pkg.epdf?.devExports ?? true, // epdf.rawExports (package.json): subpaths shipped verbatim in BOTH the // dev and publish maps — e.g. a worker entry published as TS source for // the consumer's bundler to compile. Never built, never rewritten. customExports: (generated) => withConditions({ ...generated, ...rawEntries }), }, ...(neverBundle.length > 0 ? { deps: { neverBundle } } : {}), // dist holds the package's own files and nothing from another package; epdf.bundle // (package.json) lists the packages meant to ship inside it (see own-source-only.mjs). plugins: [ ownSourceOnly(pkg, fs.realpathSync(process.cwd())), ...(bundleInCjs.length > 0 ? [bundledNotices(pkg, fs.realpathSync(process.cwd()))] : []), ], publint: true, // node16 profile: subpath exports are invisible to legacy node10 module // resolution; v3's documented floor is TS >= 4.7 with node16/bundler. // rawExports are bundler-only entries (shipped TS source) — Node never // resolves them, so they're excluded from the check. attw: { profile: 'node16', level: 'error', excludeEntrypoints: raw }, // Deterministic builds: the preset is the only config source. config: false, name: pkg.name, ...overrides, }; } interface PackageJson { name?: string; exports?: Record; epdf?: { rawExports?: string[]; devExports?: true | string; conditions?: Record>; neverBundle?: string[]; bundle?: string[]; bundleInCjs?: string[]; }; } /** * './runtime' -> { runtime: './src/runtime.tsx' }, '.' -> { index: ... }. * Accepts both the normalized string form tsdown writes back and the * handwritten { types, import } object form found on first conversion. */ function entriesFromExports(pkg: PackageJson, skip: string[] = []): Record { const entries: Record = {}; for (const [subpath, value] of Object.entries(pkg.exports ?? {})) { if (subpath === './package.json' || skip.includes(subpath)) continue; const target = typeof value === 'string' ? value : ((value as Record)?.development ?? (value as Record)?.import ?? (value as Record)?.default ?? (value as Record)?.types); if (!target?.startsWith('./src/')) continue; entries[subpath === '.' ? 'index' : subpath.slice(2)] = target; } if (Object.keys(entries).length === 0) { throw new Error( `${pkg.name}: no src/ entries found in package.json "exports" — ` + `the preset derives its entry list from the exports map (see tooling/build).`, ); } return entries; }