Skip to content

Build dual ESM/CJS output with two tsc runs - #252

Open
matejos wants to merge 1 commit into
mainfrom
esm-dual-tsc
Open

Build dual ESM/CJS output with two tsc runs#252
matejos wants to merge 1 commit into
mainfrom
esm-dual-tsc

Conversation

@matejos

@matejos matejos commented Aug 28, 2026

Copy link
Copy Markdown

Closes #243.

The package was CommonJS only. It now ships both, selected through the exports map. The public API does not change.

pnpm run build runs tsc twice, into dist/cjs and dist/esm. Each output directory gets a small package.json that declares its module type. That is what lets one set of .d.ts files serve both, so the repo root stays CommonJS.

Every relative import in src now carries a .js extension, and so do the lodash deep imports. ES modules do not resolve an extensionless path, and tsc emits the specifier as written. Jest maps the extension back through a new moduleNameMapper. scripts/verify-dist.ts guards it in CI by loading both builds in a real Node.js process.

The built files moved, so main is now dist/cjs/node-index.js. Any consumer that deep imports @api3/commons/dist/..., or that uses the paths override from the README, needs a path update. Named imports from the package root are unaffected.

Tested against oev-searcher, purinta-lending and airnodehub by packing the build into each: all typecheck, build and test clean, with no source changes needed. publint and @arethetypeswrong/cli are both green.

@matejos
matejos requested review from Siegrift and matejfalat August 28, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support to ESM/CJS

1 participant