Skip to content

Lib mode doesn't export types #2989

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

Closed
darklight9811 opened this issue Apr 14, 2021 · 26 comments
Closed

Lib mode doesn't export types #2989

darklight9811 opened this issue Apr 14, 2021 · 26 comments
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@darklight9811
Copy link

Describe the bug

I'm using vitejs to build a react typescript library, I started building it with a plain rollup, but decided to migrate, so I have put everything accordingly. Fixed some bugs, added some packages, but when I export it, it doesn't export the typings (.d.ts) along with the es and umd files, even if I set declarations to true inside of the tsconfig. But it does export successfully both files.

I tried tweaking rollup but to no avail, and also couldn't find anything related to this in the issues or google at all.

Reproduction

Install vite with the "react-ts" template, follow the documentation steps to use lib mode, make a simple component and export it from the output file, and build it.

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: Windows 10 10.0.18363
    CPU: (8) x64 Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
    Memory: 10.24 GB / 15.96 GB
  Binaries:
    Node: 14.3.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.14.0 - D:\programas\laragon\bin\yarn\bin\yarn.CMD
    npm: 5.7.1 - ~\node_modules\.bin\npm.CMD
  Browsers:
    Chrome: 89.0.4389.128
    Edge: Spartan (44.18362.1474.0)
    Internet Explorer: 11.0.18362.1

Used package manager: yarn


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

  • [ x ] Read the Contributing Guidelines.
  • [ x ] Read the docs.
  • [ x ] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • [ x ] Provide a description in this issue that describes the bug.
  • [ x ] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • [ x ] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
@Shinigami92
Copy link
Member

Have you checked noEmit in tsconfig.json?

This doesn't work together with declaration: true
https://www.typescriptlang.org/tsconfig#noEmit

@darklight9811
Copy link
Author

Just checked, same thing, exported files:
image

@keelii
Copy link

keelii commented Apr 16, 2021

You can use tsc command to generate a single bundled declaration file, like this:

{
    "build:types": "tsc -m amd -t ESNext -d --isolatedModules false  --moduleResolution node --esModuleInterop --jsx react --outFile dist/index.js src/main.ts && rm -rf dist/index.js ",

}

@darklight9811
Copy link
Author

@keelii, but shouldn't vite provide this solution by default since it natively supports typescript?

@keelii
Copy link

keelii commented Apr 16, 2021

When vite do this for you, it's not cheap, and if you do it by using tsc, It is truly more natively.

Because even if you use tsc, it's not so fluently. you will need concat to a single d.ts file which is not directly supported will in typescript, you need some tricks to let it work.

@Shinigami92 Shinigami92 added bug p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels Apr 16, 2021
@Shinigami92
Copy link
Member

Mh seems that it was already requested some time ago #2049

IMO it may make sense to generate declaration files in library mode, cause a published library should serve declarations. That just make the ecosystem a better place.

@darklight9811
Copy link
Author

@Shinigami92, any dates or updates on when this would go to vitejs? That's the only thing that is preventing me from using on my company.

@Shinigami92
Copy link
Member

@darklight9811 I don't know if there is currently an open PR, if not please feel free to jump in and create one 🙂

@patak-dev
Copy link
Member

But even without this in core, this shouldn't prevent you to use it as you can generate the types on the side as was shown in the comments in this issue.

@darklight9811
Copy link
Author

@patak-js, I don't know if I'm doing something wrong, but that command is not working and tweaking it also didn't work for me.

@Shinigami92
Copy link
Member

@darklight9811 also tried this? #2989 (comment)

@samwightt
Copy link

Just tried to use Vite for a serious project at my company and this is preventing me from using it as well. Generating type definitions is something that should be supported by default, especially when the Typescript support in Vite is as good as it is already. Really disappointed that this hasn't been considered as a standard feature so that devs don't have to deal with low-quality workarounds.

@Shinigami92
Copy link
Member

@samwightt As I already wrote here: #2989 (comment)

I'm sorry for you that you may have a misunderstanding about how open source works 🙁
But we give you everything you need to do a PR and nothing stops you from providing this much sought-after feature/fix
Do the world a favor and establish it for the entire community
And in the end, we're grateful for you resolving an issue the community had with Vite
Just assuming something should be supported right away and then being frustrated that it isn't and then doing nothing doesn't help anyone 🤷

@axelthat
Copy link

axelthat commented Apr 24, 2021

You can use https://github.com/egoist/tsup.

Example usage,

{
    "scripts": {
        "build": "tsup index.ts --format esm,cjs,iife --dts --minify"
    }
}

The --dts flag generates the types automatically.

@moljac024
Copy link

moljac024 commented May 3, 2021

I understand the reasoning behind not doing this but I think it should be reconsidered. If Vite did this by default it would be a sort of pit of success that would benefit the broader JS/TS community. If it's not the default and people start using Vite to build libraries I imagine we will have lots of them published without types. That in turn will lead to a lot of churn and lots of github issues being created (in the respective libraries).

@darklight9811

This comment has been minimized.

@Shinigami92
Copy link
Member

@Sanjade, I can't get it to work with sass files, any idea how?

What does sass have to do with TypeScript declarations?! Could you explain a bit?

@darklight9811
Copy link
Author

@Shinigami92 , sass files are generating css modules that can be exported and are dynamically typed.....

@samwightt
Copy link

If it's not the default and people start using Vite to build libraries I imagine we will have lots of them published without types.

This. Currently I'm actively discouraging people in my network from using Vite for libraries. Not having this on by default, especially when everything else in library mode just works, will objectively make the JS ecosystem worse. Usage of Typescript is only growing, so it would be good for Vite to be a good citizen and support this by default.

I'm not angry or annoyed that this is not the default, and I'm not expecting any work to be done to get this feature working. I just want to make sure that maintainers understand how big of a deal this feature is, and that not having it is a really glaring oversight when the rest of library mode is as polished as it is.

@darklight9811

This comment has been minimized.

@axelthat

This comment has been minimized.

@darklight9811

This comment has been minimized.

@axelthat

This comment has been minimized.

@darklight9811

This comment has been minimized.

@Shinigami92
Copy link
Member

@Sanjade Do you like to create a feature request issue for this? Then we have a fresh new issue and we can better track it 🙂

@nihalgonsalves
Copy link
Member

Moved to #3461

@vitejs vitejs locked as too heated and limited conversation to collaborators May 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

No branches or pull requests

8 participants