vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.65k stars 6.2k forks source link

Double module import of external library #9251

Closed Kolobok12309 closed 2 years ago

Kolobok12309 commented 2 years ago

Describe the bug

We write some library with .vue components and utils (for this components and other). This utils can have some state, and methods for change it(keys is symbols). In real project with vite(Nuxt3 setup) we found duplication of imported modules(by console.log)

This problem from vite or vue-plugin, can't say for sure, sometimes module load 2 times, sometimes 3, and this critical because we use symbols

Problem is similar to #7323

In repro duplicates is /node_modules/.vite/deps/chunk-VQ4VIJMT.js?v=3a551489 and /node_modules/shared/shared.js .vue file is initiator of shared.js, and simple .js is initiator of chunk-VQ4VIJMT.js?v=3a551489 file

Reproduction

https://github.com/Kolobok12309/vitejs-double-file-load

System Info

System:
    OS: macOS 12.4
    CPU: (10) x64 Apple M1 Max
    Memory: 1.02 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 14.19.3 - ~/.nvm/versions/node/v14.19.3/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 6.14.17 - ~/.nvm/versions/node/v14.19.3/bin/npm
  Browsers:
    Chrome: 103.0.5060.114
    Firefox: 95.0.2
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-vue: ^2.3.3 => 2.3.3 
    vite: ^3.0.1 => 3.0.2

Used Package Manager

yarn

Logs

No response

Validations

Kolobok12309 commented 2 years ago

Connected repo with minimal library https://github.com/kolobok12309/vite-library-bug-double-load

bluwy commented 2 years ago

I think this is a duplicate of #3910. The workaround for now is to add that library, e.g. vite-library-bug-double-load, to optimizeDeps.exclude, and if there's any CJS deps used by that library, it needs to be re-added to optimizeDeps.include, e.g. vite-library-bug-double-load > cjs-dep.

Kolobok12309 commented 2 years ago

@bluwy, you are right, is duplicate, i didn't see this issue. But optimizeDeps.exclude: ['shared'] don't work for this minimal repro