Skip to content

Publish valid ESM types for dual output packages#120

Merged
cuzzlor merged 5 commits intomainfrom
fix-esm-dual-output-typess
Apr 24, 2026
Merged

Publish valid ESM types for dual output packages#120
cuzzlor merged 5 commits intomainfrom
fix-esm-dual-output-typess

Conversation

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates ts-toolkit’s packaging workflow to publish valid TypeScript declarations for dual ESM/CJS outputs and adds an arethetypeswrong export/types validation step to generated templates and this repo’s build.

Changes:

  • Emit conditional exports entries with per-condition types for dual-output packages.
  • Add a build step to validate published artifacts with @arethetypeswrong/cli (attw --pack dist), including wiring it into templates and devDependencies.
  • Add utilities + tests to generate .d.mts/.d.cts siblings and rewrite relative specifiers for ESM declaration resolution.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/util/copy-package-json-from-config.ts Builds new exports shapes and emits dual declaration siblings with ESM-specifier rewriting.
src/util/copy-package-json-from-config.spec.ts Adds tests for conditional exports shapes and declaration emission/rewriting.
src/init/check-packages.ts Ensures @arethetypeswrong/cli is installed in initialized Node projects.
src/init/add-scripts.ts Adds build:* script to run attw against the packed dist output in templates.
package.json Bumps version and adds attw build script + devDependency.
package-lock.json Locks new @arethetypeswrong/cli dependency tree and updates version metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/util/copy-package-json-from-config.ts Outdated
Comment thread src/util/copy-package-json-from-config.ts Outdated
Comment thread src/util/copy-package-json-from-config.spec.ts
Comment thread src/util/copy-package-json-from-config.spec.ts Outdated
Comment thread src/util/copy-package-json-from-config.ts
@cuzzlor cuzzlor merged commit 27e4b4c into main Apr 24, 2026
1 check passed
@cuzzlor cuzzlor deleted the fix-esm-dual-output-typess branch April 24, 2026 05:17
@cuzzlor
Copy link
Copy Markdown
Contributor Author

cuzzlor commented Apr 24, 2026

This worked well

image

cuzzlor added a commit that referenced this pull request Apr 28, 2026
* fix(types): rewrite .d.mts relative specifiers to .mjs

`emitDualDeclarations` was producing `.d.mts` files whose relative
specifiers ended in `.js`. Under TS's node16+ ESM resolver, a `.js`
specifier in a `.d.mts` is paired with the adjacent `.d.ts`, which —
in a dual-published package whose root `package.json` has
`"type": "commonjs"` — is treated as CJS-flavoured. Strict ESM
consumers then surface type-resolution errors against an otherwise
valid package.

`rewriteSpecifier` is only ever called for the `.d.mts` branch, so the
extension it emits should be `.mjs`, which pairs with the `.d.mts`
twin and keeps the resolution chain in ESM throughout.

The `.d.cts` branch is unaffected — those remain content-identical
copies of the source `.d.ts`, which CJS resolution handles via
extensionless specifiers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test+docs: cover .mjs rewrite cases and document dual-decl emission

Tests
- Add focused unit test for the file case (was only exercised via the
  integration test): `from './foo'` → `from './foo.mjs'` when `./foo.d.ts`
  exists.
- Add test for resolving when only the `.d.mts` twin is present alongside
  the source, exercising the second branch of `rewriteSpecifier`.
- Add test that runs all three module-specifier shapes through one pass
  (`from '...'`, bare `import '...'`, dynamic `import('...')`).

Docs
- Add a `## copy-package-json` subsection covering dual-declaration
  emission, the per-condition `exports` shape, and the `.mjs` rewrite
  rules with the rationale for `.mjs` over `.js`.
- The original feature (PR #120) shipped without README coverage; this
  fills that gap alongside the specifier-extension fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(release): 5.0.0-beta.1

Publishes the .d.mts specifier-extension fix so downstream packages can
drop their local workarounds (e.g. graphql-apollo-server's
build:4a-fix-dmts post-step).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(audit): bump postcss to 8.5.10 to fix GHSA-qx2v-qp2m-jg93

Resolves the moderate XSS advisory (PostCSS Stringify Output unescaped
`</style>`) reaching us via vitest > vite > postcss in dev. Within the
existing semver range; no package.json change, lockfile-only.

Surfaced by the PR check on #121.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs+test: cover the .d.mts adjacency case for directory specifiers

Addresses Copilot review comments on #121. `rewriteSpecifier` already
resolves both `.d.ts` and `.d.mts` adjacents in the directory branch,
but the README bullet and the source-comment example only mentioned
`.d.ts`. Updated both to spell out either form.

Also adds a sibling unit test covering the case where only an
`index.d.mts` is present in a directory (matching the existing
"only a .d.mts twin alongside the source" test for the file case).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* use interop: 'auto' as default for rollup CJS output

* fix default rollup external function to correctly switch between local and imported module code

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

3 participants