Skip to content

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

@vim-daniel

Description

@vim-daniel

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions