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(plugin-legacy): ensure correct typing for node esm #13892

Merged
merged 1 commit into from Sep 20, 2023

Conversation

xiaoxiangmoe
Copy link
Contributor

@xiaoxiangmoe xiaoxiangmoe commented Jul 19, 2023

Description

According to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing , the only way to correct provide types for both Node ESM and CJS is to have two separate declaration files, so we need to generate index.d.cts and index.d.mts.

unbuild@2 could generate index.d.cts and index.d.mts standing for types of index.cjs and index.mjs, and could generate index.d.ts for legacy typescript usage.

releated commit:
vuejs/core@d621d4c

releated PRs:
unjs/unbuild#273
vitejs/vite-plugin-vue#179

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Jul 19, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

antfu
antfu previously approved these changes Jul 19, 2023
@@ -17,7 +17,6 @@
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
Copy link
Member

Choose a reason for hiding this comment

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

What is the reasoning behind removing this entry? We have the same setup on all other plugins: https://github.com/vitejs/vite-plugin-vue/blob/93c444cb99d64c65b71050d0f6a5e3016f7046a1/packages/plugin-vue/package.json#L14

Copy link
Member

Choose a reason for hiding this comment

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

TypeScript is able to pick up the adjacent .ts by removing this, .e.g ./dist/index.mjs -> ./dist/index.d.mts. What this PR does is that it emits two dts files for the two formats :(

Copy link
Member

Choose a reason for hiding this comment

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

unjs/unbuild#273

Because of .d.mts and .d.cts, removing it will allow TS to find correct one by itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing , the only way to correct provide types for both Node ESM and CJS is to have two separate declaration files, so we need to generate index.d.cts and index.d.mts.

unbuild@2 could generate index.d.cts and index.d.mts standing for types of index.cjs and index.mjs, and could generate index.d.ts for legacy typescript usage.

releated commit:
vuejs/core@d621d4c

releated PRs:
unjs/unbuild#273
vitejs/vite-plugin-vue#179

Copy link
Member

Choose a reason for hiding this comment

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

I think this PR is good for plugins given our current setup. We could release it in a minor for the plugin. I added it to the 5.0 milestone (that we're going to start working on soon) because there is a parallel discussion about moving vite core and the plugins to ESM only. I still don't know if Vite 5 is the right major to do this, so if we don't move forward with that, we should merge this PR instead.
Polls that @bluwy started for reference:

@patak-dev patak-dev added this to the 5.0 milestone Jul 21, 2023
@bluwy
Copy link
Member

bluwy commented Sep 19, 2023

As much as I dislike this TS shenanigans, I'm ok to push this for the next plugin-legacy major for now, and drop CJS in the next-next major. I'm not sure if there's any migration paths we need to implement but I think we can cut right clean then.

@xiaoxiangmoe would you be able to upgrade unbuild to v2 stable and rebase the PR?

@bluwy bluwy added plugin: legacy p2-nice-to-have Not breaking anything but nice to have (priority) labels Sep 19, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Thanks!

@bluwy bluwy merged commit d914a9d into vitejs:main Sep 20, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority) plugin: legacy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants