Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct default CJS exports #475

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

userquin
Copy link
Contributor

@userquin userquin commented Dec 29, 2024

We should add exports to ESMExport interface in mlly, missing in dts, that's why I added the regex to extract the exports (upps, it is on NamedExport type):

{
  type: 'default',
  exports: ' type X, sum as default',
  specifier: undefined,
  code: 'export { type X, sum as default }',
  start: 87,
  end: 120,
  names: [ 'default' ],
  name: 'default',
  _type: 'named'
}

resolves #474

image

@userquin userquin changed the title fix: correct default CJS exports dts fix: correct default CJS exports Dec 29, 2024
@pi0 pi0 requested a review from danielroe January 7, 2025 23:03
@pi0
Copy link
Member

pi0 commented Jan 12, 2025

Sorry for late review dear @userquin.

Do you think you can add some unit tests for fixCJSExportTypePlugin? 🙏🏼 Since we are forking away from tsup implementation, it would be nice to have them in place to both prevent regressions and clearly test desired behaviors.

@userquin
Copy link
Contributor Author

userquin commented Jan 13, 2025

Do you think you can add some unit tests for fixCJSExportTypePlugin? 🙏🏼 Since we are forking away from tsup implementation, it would be nice to have them in place to both prevent regressions and clearly test desired behaviors.

To test the cjs types we can:

  • add attw (will check the new fixtures) after the test: maybe using pnpm exec attw --pack
  • add some small parsing via mlly and/or use d.ts and d.cts file snapshots (rn on my local using file snapshots)

I'm going to create a new repo to test the same fixtures using tsup (will fail for mixed declarations) and pkgroll (using import + require package exports here to allow re-use them in pkgroll).

@userquin
Copy link
Contributor Author

userquin commented Jan 13, 2025

@pi0 @danielroe

When all remaining exports (from the default export) only contains types (export { type A, type B }) the export contining the default is replaced by export type { A, B }. Check test/cjs-types-fixture/mixed-declarations/index.ts in this PR and the dist folder.

When re-exporting types from another import, there is no an easy way to know if the export is a type, and so when re-exporting exports, the export contining the default will not be changed. Check test/cjs-types-fixture/reexport-types/index.ts in this PR and the dist folder.

screenshot with generated d.ts files

imagen

@userquin
Copy link
Contributor Author

With last commit in the re-export fixture:

imagen

@pi0
Copy link
Member

pi0 commented Jan 13, 2025

Nice work @userquin. I'm wondering if you would be interested in maintaining it as an external/shared library now. (that converts .d.mts to .d.cts) -- it might be usable to unbuild and wider ecosystem.

@userquin
Copy link
Contributor Author

Good idea, maybe we can add it here to the unjs ecosystem... I need to find some free time, too busy at work.

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.

dts for node10 and node16 cjs still wrong with type module
3 participants