question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vite app consuming Vite library with peerDependenices fails to tree-shake library

See original GitHub issue

Describe 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:

  1. npm install the npm workspace
  2. run npm build in the vite-lib folder
  3. run npm build in the vite-app folder -> the index.js size will be 184kb~
  4. change ComponentC to not import react-bootstrap (component C is not used at all in vite app)
  5. run npm build in the vite-lib folder
  6. 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 import ComponentC)

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, May 4, 2022

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 at react-bootstrap/Dropdown/package.json. https://cdn.jsdelivr.net/npm/react-bootstrap@2.3.1/Dropdown/package.json If you add "sideEffects": false to react-bootstrap/Dropdown/package.json it works well.

0reactions
vim-danielcommented, May 17, 2022

It might be. Does this work with webpack or rollup?

I’ve been able to make it work in rollup using the preserveModule option

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found