qmhc / vite-plugin-dts

A Vite plugin for generating `.d.ts` files.
MIT License
1.22k stars 84 forks source link

DTS not compatible with latest React Vite tsconfig structure #344

Closed mochizin closed 2 weeks ago

mochizin commented 2 months ago

Describe the bug

I'm trying to use DTS with Vite for React 5.3.1 In this version Vite introduced tsconfig.app.json and reference it in tsconfig.json:

{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.node.json"
    }
  ]
}

For some reason, the tsconfig.app.json is not being recognized by DTS. I get the following error:

image or Module './App.tsx' was resolved to 'libray-ui/src/App.tsx', but '--jsx' is not set. image

The solution that I'm using is add tsconfigPath: "./tsconfig.app.json", Is this a good solution? Edit: This solution doesn't work properly

plugins: [
    react(),
    dts({
      tsconfigPath: "./tsconfig.app.json"
    }),
  ],

Reproduction

https://stackblitz.com/edit/vitejs-vite-yxnnz2?file=vite.config.ts

Steps to reproduce

export default defineConfig({ plugins: [ react(), dts(), ], build: { lib: { entry: resolve(__dirname, "src/main.ts"), name: "trigger-ui", formats: ["es"], fileName: "trigger-ui", }, rollupOptions: { external: ["react", "react/jsx-runtime"], }, }, });

- main.tsx file

export const Card = ({ children, ...props }: CardTypes) => { return <button {...props}>{children}; };

type CardTypes = { children: string; } & React.ComponentPropsWithoutRef<"button">;


- Run 
`npm run build`

### System Info

```shell
System:
    OS: Windows 11
  Binaries:
    Node: 20.11.1
    npm: 10.2.4
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1
    vite: ^5.3.1 => 5.3.1
    vite-plugin-dts: ^3.9.1 => 3.9.1

Validations

yestay90 commented 1 month ago

@mochizin , hello. Is there any updates on this subject? Should we recreate react and vite app with older version? I want to install older version of vite. Which is doesnt have this problem?

yestay90 commented 1 month ago

@mochizin , I removed tsconfig.app.json and moved all its content to tsconfig.json. And it started to work properly. I could import UI component to my other project.

mochizin commented 1 month ago

@mochizin , I removed tsconfig.app.json and moved all its content to tsconfig.json. And it started to work properly. I could import UI component to my other project.

Yeah, this was my solution as well, but it is how Vite do in previous versions. I was looking for some solution/fix that works with latest Vite

willian commented 1 month ago

@yestay90, @mochizin, I think this fix must come from this plugin in a future update. Until it happens, this is what I did to fix it on my side:

    dts({
      tsconfigPath: resolve(__dirname, "tsconfig.app.json"),
      rollupTypes: true,
    }),

So, I didn't need to change the "Vite way" of organizing the tsconfig files.

mochizin commented 1 month ago

@willian, I tried to do that previously as well but when I run npm run build that contains tsc -b, it generates a vite.config.js file in my root folder. I'm using vite.config.ts.

So this solution doesn't work for me 😕

phuongthuan commented 1 month ago

@mochizin I got exactly the same your issue Here is the fix

  plugins: [
    react(),
    dts({
      rollupTypes: true,
      tsconfigPath: "./tsconfig.app.json",
    }),
  ],
fahrikurniawan99 commented 1 month ago

@phuongthuan wow amazing how this works, why does it happen?

mochizin commented 1 month ago

@phuongthuan can't see the file, can you post the code here?

lizyChy0329 commented 1 month ago

@mochizin I got exactly the same your issue @mochizin我得到了与你完全相同的问题 Here is the fix这是修复方法

  plugins: [
    react(),
    dts({
      rollupTypes: true,
      tsconfigPath: "./tsconfig.app.json",
    }),
  ],

It work! @qmhc

albertdugba commented 3 weeks ago

This also ended up working for me

elkofy commented 2 weeks ago

Me too after 4 days of searching

qmhc commented 2 weeks ago

It has been updated in FAQs.

LuciKritZ commented 1 week ago

@yestay90, @mochizin, I think this fix must come from this plugin in a future update. Until it happens, this is what I did to fix it on my side:

    dts({
      tsconfigPath: resolve(__dirname, "tsconfig.app.json"),
      rollupTypes: true,
    }),

So, I didn't need to change the "Vite way" of organizing the tsconfig files.

Thanks for the fix, appreciate it! @willian @phuongthuan

luizfm commented 4 days ago

Any updated on this? Some of these suggested config didn't work for me...

error during build:
[vite:dts] Internal Error: getResolvedModule() could not resolve module name "./stories/button/button.component"
/Users/luizfernandodemorais/Desktop/projetos/personal-projects/prolog-component-js/dist/index.d.ts:1:1

I'm using the same build command as mentioned by @mochizin "build": "tsc -b && vite build"