Vite app consuming Vite library with peerDependenices fails to tree-shake library
See original GitHub issueDescribe the bug
The issue is as follows, I have a vite application and a vite components library The application consumes the components library
In the vite-library i have a peer dependency, I’ve configured the library to not bundle that peer dependency.
The vite app is not importing code that is using the peer dependency. When building the vite app, the bundle includes the peer dependency code.
I think the issue is in how the vite library is bundled, it imports dependencies marked as external and for some reason this causes vite to bundle them like they are globally needed
I’ve added a link for reproduction, steps to reproduce:
- npm install the npm workspace
- run npm build in the
vite-lib
folder - run npm build in the
vite-app
folder -> the index.js size will be 184kb~ - change
ComponentC
to not importreact-bootstrap
(component C is not used at all in vite app) - run npm build in the
vite-lib
folder - run npm build in the
vite-app
folder -> the index.js size will be 142kb~ -> 40 kb changed although the app code didn’t change at all (and it does not importComponentC
)
Reproduction
https://stackblitz.com/edit/vitejs-vite-hzzn14?file=vite-app/main.jsx
System Info
System:
OS: Linux 5.0 undefined
CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: Unknown - /bin/jsh
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.10 - /bin/yarn
npm: 7.17.0 - /bin/npm
npmPackages:
@vitejs/plugin-react: ^1.3.0 => 1.3.1
vite: ^2.9.5 => 2.9.6
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- 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/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why is my React component library not tree-shakable?
After building the app bundle and analyzing it I see that the library has either not been tree-shaken at all or that tree-shaking...
Read more >How to write a tree-shakable component library
lets us work in a monorepo where our ui library is one package and our app(s) is another; the ui library must be...
Read more >Dependency Pre-Bundling
Vite automatically detects dependencies that are not resolved from node_modules and treats the linked dep as source code. It will not attempt to...
Read more >rollup.js
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library...
Read more >The Ultimate Introductory Guide for Bundling TypeScript ...
The systems using your library code are addressed as “consumers,” ... the consumer's own application bundler (Webpack, Snowpack, Vite) is ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe related: #6740
At the top level
"sideEffects": false
is declared. https://cdn.jsdelivr.net/npm/react-bootstrap@2.3.1/package.json But it is not declared atreact-bootstrap/Dropdown/package.json
. https://cdn.jsdelivr.net/npm/react-bootstrap@2.3.1/Dropdown/package.json If you add"sideEffects": false
toreact-bootstrap/Dropdown/package.json
it works well.I’ve been able to make it work in rollup using the
preserveModule
option