fix: emit CommonJS type declarations for require consumers - #24
Open
erossignon wants to merge 1 commit into
Open
erossignon wants to merge 1 commit into
erossignon wants to merge 1 commit into
Conversation
The package root declares "type": "module", so the single declaration set under build/types was classified as ESM regardless of which export condition resolved it. CommonJS consumers on moduleResolution node16 got TS1479 even though build/cjs loads fine via require. Emit a second declaration set to build/types-cjs and mark it with a "type": "commonjs" package.json, reusing the marker approach already used for build/cjs. The exports map now selects declarations per condition. Fixes PeculiarVentures#23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #23.
Problem
The package root declares
"type": "module", and everyexportsentry pointed at a single declaration set underbuild/types. TypeScript derives a declaration file's module format from the"type"field of its nearestpackage.json, not from the export condition that resolved it -- so those.d.tsfiles were classified as ESM even when reached throughrequire. Any CommonJS consumer onmoduleResolution: node16got:The runtime was never broken --
build/cjsloads correctly viarequire. Only the declarations were misclassified.Change
tsconfig.build.types.cjs.jsonemits a second declaration set tobuild/types-cjs.scripts/write-cjs-package-json.mjsnow writes the{"type": "commonjs"}marker tobuild/types-cjsas well asbuild/cjs, reusing the approach already in place for the JavaScript output.exportsselects declarations per condition (import.types/require.types).filespublishes the new directory and its marker.types-cjsalongsidetypes.No source changes, and the ESM declarations are untouched.
Verification
npm run checkpasses (lint,tsc --noEmit, 31 tests).Beyond that I packed the tarball and type-checked a real consumer against it with
skipLibCheck: false, on TypeScript 5.9.3:/bytessubpath/pemsubpath/bytessubpath/bytessubpathThe last row is unchanged from baseline -- subpath exports have never been reachable under classic
noderesolution, which is why asn1-schema#130 was fixed by moving to root imports. I mention it only so the matrix is complete; this PR neither fixes nor worsens it.Runtime smoke test on both formats:
Context
Reached this through
node-opcua-crypto, which uses the@peculiarASN.1/X.509 stack for certificate handling. Downstream CJS projects currently needskipLibCheck: trueto build. This is thenode16half of the compatibility problem reported in PeculiarVentures/asn1-schema#130.