Skip to content

Honour package sideEffects when tree-shaking production bundles - #74

Open
kosciak9 wants to merge 1 commit into
elixir-volt:masterfrom
kosciak9:fix/package-side-effects
Open

kosciak9 wants to merge 1 commit into
elixir-volt:masterfrom
kosciak9:fix/package-side-effects

Conversation

@kosciak9

Copy link
Copy Markdown

Production bundles are built from a virtual project (OXC.bundle/2 writes the compiled modules into a temp directory) in which a package's files sit under the package name, but the package's own package.json is not written. Rolldown reads sideEffects from the nearest package.json, so it could not see it and kept every unused module that runs code at the top level.

The effect is large for icon libraries: each icon in iconoir-react and lucide-react is a module like export default forwardRef(Icon), so importing ~100 icons kept all ~3,500 of them. In our app that was ~2.7 MB of a 6.9 MB minified bundle.

This writes a minimal package.json holding only sideEffects at the root of each bundled package that declares it, for every bundling path (single entry, shared entries, code-split chunks). Packages that don't declare sideEffects are unchanged.

Test: drops unused modules of packages declaring no side effects in resolution_test.exs builds two packages whose modules call an imported function at the top level; the unused module of the sideEffects: false package is dropped, while the sideEffects: true package keeps it. It fails without the change.

Locally mix test passes except CaseBrowserTest (no Playwright here) and one BundleTest case that fails with {:not_found, "lit-html"} only in the full run and passes on its own.

Production bundles are built from a virtual project without the packages' package.json files, so Rolldown could not see sideEffects and kept every module that runs code at the top level, such as icons wrapped in forwardRef. Write a minimal package.json carrying sideEffects at each bundled package's root.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant