vite: Lib mode doesn't export types

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.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26 (9 by maintainers)

Most upvoted comments

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).

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.

@samwightt As I already wrote here: https://github.com/vitejs/vite/issues/2989#issuecomment-825697272

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 🤷

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.

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.