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: experimentalDts for Node16/NodeNext module resolution #1225

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

Conversation

aryaemami59
Copy link
Contributor

@aryaemami59 aryaemami59 commented Sep 27, 2024

Copy link

codesandbox bot commented Sep 27, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

vercel bot commented Sep 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tsup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 19, 2024 5:19am

Copy link

pkg-pr-new bot commented Sep 27, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/egoist/tsup@1225

commit: ba6eb7d

src/utils.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@ocavue ocavue left a comment

Choose a reason for hiding this comment

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

To matches the behavior of tsc's output, .js should only be added for Node16/NodeNext, not for Bundler etc.

@aryaemami59
Copy link
Contributor Author

@ocavue tsc's Bundler module resolution can still handle (and output) relative .js\.mjs\.cjs file extensions.

@ocavue
Copy link
Collaborator

ocavue commented Oct 15, 2024

tsc's Bundler module resolution can still handle (and output) relative .js\.mjs\.cjs file extensions.

Thanks. TIL.

Does other moduleResolution options like Node can handle relative file extensions too?

@aryaemami59
Copy link
Contributor Author

aryaemami59 commented Oct 15, 2024

Does other moduleResolution options like Node can handle relative file extensions too?

@ocavue I think so, but just to be safe let me double check.

@aryaemami59
Copy link
Contributor Author

Does other moduleResolution options like Node can handle relative file extensions too?

@ocavue Answer is yes!

@ocavue
Copy link
Collaborator

ocavue commented Oct 15, 2024

I still believe it’s safer to match tsc’s behavior (i.e., whether to include the .js extension in the generated .d.ts files). The output of tsc is the de facto standard, and there may be other tools besides TypeScript that consume .d.ts files.

In my opinion, a better approach would be to use moduleResolution to determine whether to add the .js extension.

@aryaemami59
Copy link
Contributor Author

aryaemami59 commented Oct 15, 2024

@ocavue Can't say I agree with that but if we wanted to offer some more flexibility we could put this functionality behind an option or at least provide an option to disable it? Something like addRelativePathExtension? The issue is as it stands right now (prior to this PR) the resulting type definitions for both ESM and CJS end up importing from the same _tsup-dts-rollup file. Maybe we could somehow merge the final type definition file with _tsup-dts-rollup and remove _tsup-dts-rollup from the final outDir?

@ocavue
Copy link
Collaborator

ocavue commented Oct 15, 2024

addRelativePathExtension seems too complex to me.

@aryaemami59
Copy link
Contributor Author

aryaemami59 commented Oct 15, 2024

@ocavue What do you think of removing _tsup-dts-rollup files from the final outDir?

addRelativePathExtension seems too complex to me.

I was going for something that was explicit and self-explanatory. While it may sound complicated, it sounds fairly obvious what it does. But fair enough, what would you have in mind?

@ocavue
Copy link
Collaborator

ocavue commented Oct 15, 2024

What do you think of removing _tsup-dts-rollup files from the final outDir?

Most packages only have one entrypoint, and removing _tsup-dts-rollup is a good idea for them. _tsup-dts-rollup.d.ts is a silly workaround for the fact that @microsoft/api-extractor doesn't support multi-entrypoints packages. Maybe a better solution is only output _tsup-dts-rollup.d.ts if there are more than one entrypoints.


I just don't think it's necessary to config addRelativePathExtension manually. Other .d.ts generator (i.e. tsc) don't have such an option.

@aryaemami59
Copy link
Contributor Author

aryaemami59 commented Oct 18, 2024

@ocavue I tried recreating the issue that you said this would cause, but I can't. I'm going to try some more, but is there anyway you could provide a more concrete example so I can make sure this PR doesn't cause the issues you're describing?

Edit: I added some tests for it, please let me know if it needs anything else.

@ocavue
Copy link
Collaborator

ocavue commented Oct 19, 2024

is there anyway you could provide a more concrete example

Here you are:

  1. Open https://stackblitz.com/github/issueset/tsup-multi-dts or clone https://github.com/issueset/tsup-multi-dts
  2. Run pnpm install && pnpm run build
  3. You will get the following error

src/bad.ts:5:7 - error TS2322: Type 'import("/private/tmp/tsup-multi-dts/packages/my-lib-bad/dist/foo").SharedClass' is not assignable to type 'import("/private/tmp/tsup-multi-dts/packages/my-lib-bad/dist/bar").SharedClass'.
  Types of property 'symbol' are incompatible.
    Type 'typeof import("/private/tmp/tsup-multi-dts/packages/my-lib-bad/dist/foo").sharedSymbol' is not assignable to type 'typeof import("/private/tmp/tsup-multi-dts/packages/my-lib-bad/dist/bar").sharedSymbol'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants