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

experimentalDts generates broken type declarations. #1050

Open
ChristophP opened this issue Nov 27, 2023 · 9 comments
Open

experimentalDts generates broken type declarations. #1050

ChristophP opened this issue Nov 27, 2023 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@ChristophP
Copy link

ChristophP commented Nov 27, 2023

Problem

When using experimentalDts in a with components in a subfolder the generated output points to files that don't exist.

Version info

tsup 8.0.1
@microsoft/api-extractor 7.38.3

Example

// input files
./src
  components/
    ComponentA/
      index.tsx

// tsup config
export default defineConfig({
      target: 'esnext',
      platform: 'neutral',
      format: 'esm',
      experimentalDts: true,
      entry: [ "./src/components/ComponentA.tsx" ]
});

// output files (Note how the components/ folder does not exist in dist)
./dist
  ComponentA/
      index.tsx
  _tsup-dts-rollup.d.ts

Now when looking into the dist/_tsup-dts-rollup.d.ts file it contains references to ./components/ComponentA resulting in broken typings.

Proposed fix

The generated paths in dist/_tsup-dts-rollup.d.ts somehow reference nonexisting files when the entry code is in a subdirectory, so the fix would probably have to check the actual location of the output file and reference that instead.

Other feedback

@ocavue
I originally tried out the experimentalDts option because I am building a number of entries (20+) and the dts build is very slow (~30 seconds). With the new experimentalDts option this time went down to ~7 seconds. Thanks for that great speed improvement. So it would be very nice to use if the generated paths were correct.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@ocavue
Copy link
Collaborator

ocavue commented Nov 27, 2023

I cannot reproduce "the components/ folder does not exist" issue with the following repo. Could you provide me a minimal reproduction so that I can better debug it?

https://stackblitz.com/github/issueset/tsup-1050

The output files are shown below. The fact that index.js and index.d.ts are not in the same folder looks like an issue, but the declaration file itself seems to be all good.

image

@ocavue
Copy link
Collaborator

ocavue commented Nov 27, 2023

By the way, have you tried to remove the .tsup temporary directory and run the build again?

@ChristophP
Copy link
Author

Hi thanks for responding so promptly.
I'll to create a minimal repro and will post back.

@ChristophP
Copy link
Author

@ocavue This is the smallest I could get it to in a short amount of time.
https://github.com/ChristophP/tsup-experimental-dts-bug

To reproduce:

  1. Clone the repo
  2. Run npm ci
  3. Run npx tsup
  4. Take a look at dist/_tsup-dts-rollup.d.ts and notice how it points at paths that do not exist.

@ocavue
Copy link
Collaborator

ocavue commented Nov 27, 2023

Thanks! The generated dist/_tsup-dts-rollup.d.ts in your repo is indeed messed up. I will fix it.

@ocavue ocavue self-assigned this Nov 27, 2023
@ocavue ocavue added the bug Something isn't working label Nov 27, 2023
@ChristophP
Copy link
Author

Awesome, thank you.

@TheAsda
Copy link

TheAsda commented Jan 9, 2024

Facing the same issue with tsup@8.0.1 and @microsoft/api-extractor@7.39.1

@maxpatiiuk
Copy link

Facing the same issue

tsup/8.1.0 darwin-arm64 node-v20.11.0

The bug appears to be on this line:

const outFileName = path.join(outDir, out + dtsExtension)

@maxpatiiuk
Copy link

maxpatiiuk commented Aug 19, 2024

We were able to workaround this by using tsc for types generation:

// tsup.config.ts
import { defineConfig } from "tsup";

export default defineConfig({
  entry: ["src/index.ts"],
  format: ["esm"],
  onSuccess: "yarn tsc -p tsconfig.tsup.json && echo 'Rebuilt'",
});
// tsconfig.tsup.json
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "./tsconfig.json",
  "include": ["src"],
  "compilerOptions": {
    "noEmit": false,
    "emitDeclarationOnly": true,
    "outDir": "dist",
    "rootDir": "src"
  }
}

That also fixes #977 and a few other issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants