Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vite 6 potential bundle size regression (CJS, ESM) #18861

Closed
7 tasks done
skovhus opened this issue Dec 2, 2024 · 3 comments
Closed
7 tasks done

vite 6 potential bundle size regression (CJS, ESM) #18861

skovhus opened this issue Dec 2, 2024 · 3 comments

Comments

@skovhus
Copy link
Contributor

skovhus commented Dec 2, 2024

Describe the bug

When using the prosemirror-utils dependency the vite@6 bundle ends up 20% bigger than vite@5 – due to ESM and CJS ending up in the same bundle (see stats-vite5.html and stats-vite6.html).

Removing the prosemirror-utils dependency fixes the problem, but question is, can this be considered a vite 6 bundle size regression – or just a bug in the library definition?

Reproduction

https://github.com/skovhus/vite6-bundle-size-regression

Steps to reproduce

No response

System Info

System:
    OS: macOS 15.0.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 239.70 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.11.1/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
    pnpm: 9.1.1 - ~/.nvm/versions/node/v20.11.1/bin/pnpm
    bun: 1.1.2 - ~/.bun/bin/bun
    Watchman: 2024.10.21.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 131.0.6778.86
    Safari: 18.0.1
  npmPackages:
    vite: ^6.0.1 => 6.0.1

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red
Copy link
Member

sapphi-red commented Dec 2, 2024

Summary: It's not a bug, and is actually a bug fix. The library should add module condition pointing to the same file with import condition.


In Vite 5.x, there was a bug that mixed the cache of require resolution and import resolution and Vite 6 fixed that. While this is a more correct behavior, dual package hazard happens a bit more frequently (it won't happen if the package author writes a proper exports field). This causes the bundle size to increase.

For example, in this project, in Vite 6 prosemirror-transform@1.7.3 gets bundled twice. One for prosemirror-transform@1.7.3/dist/index.js and one for prosemirror-transform@1.7.3/dist/index.cjs. This is happening because prosemirror-transform has import condition pointing to index.js and require condition pointing to index.cjs. That export field means if the package was required, index.cjs should be used, and index.js when imported.

The two import / require paths are:

  • src/main.ts --(import)--> prosemirror-transform@1.7.3/dist/index.js
  • src/main.ts --(import)--> prosemirror-utils@0.9.6/dist/index.js --(require)--> prosemirror-state@1.4.3/dist/index.cjs --(require)--> prosemirror-transform@1.7.3/dist/index.cjs

In Vite 5, the resolution cache was mixed so prosemirror-state@1.4.3/dist/index.cjs --(require)--> prosemirror-transform@1.7.3/dist/index.cjs was prosemirror-state@1.4.3/dist/index.cjs --(require)--> prosemirror-transform@1.7.3/dist/index.js. This is incorrect, but reduced the bundle size.

The way to fix this is to add module condition to the exports field pointing to the same file with import condition for all prosemirror dependencies. Because prosemirror already has module field, it should be safe to add them.
If the module conditions are set, I guess the bundle size would be smaller than the result you had with Vite 5 as prosemirror-tables, prosemirror-model, prosemirror-state will be deduped.

@sapphi-red
Copy link
Member

Some issues related to the module condition: webpack/webpack#11014, rollup/rollup#3514, evanw/esbuild#2417

@skovhus skovhus closed this as completed Dec 2, 2024
@skovhus
Copy link
Contributor Author

skovhus commented Dec 2, 2024

Thanks for looking into this and for the careful explanation. 👏🏼

NOTE: upgrading the library also solved the issue, just wanted to flag if this was in fact a regression in Vite 6

@github-actions github-actions bot locked and limited conversation to collaborators Dec 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants