Support Rollup's preserveModules
See original GitHub issueCurrent Behavior
Currently a single bundle is output by rollup and module structure is not preserved.
Desired Behavior
There should be a way to do deep imports from the packaged library like so: import FooModule from "mylib/FooModule"
.
Suggested Solution
Rollup has a preserveModules
option that includes the module structure in the built files.
A caveat is that the library should be published from the build directory to avoid having to include the build directory in the import path (like import FooModule from "mylib/dist/FooModule"
).
Who does this impact? Who is this for?
Here’s a simple benchmark comparing deep vs bare imports from a large library: https://github.com/slikts/deep-vs-bare-import-bench
The performance impact of having to parse unused code is non-negligible for larger libraries, and it’s compounding. Tree shaking only mitigates this for the final consumer but not while developing. Larger component libraries like MUI specifically support deep imports to avoid this problem.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:27
- Comments:18
Top GitHub Comments
Based on a Twitter thread from @mweststrate today, it sounds as if this option may be key to getting proper tree-shaking working:
https://twitter.com/mweststrate/status/1228056670447730688
We don’t need this for “deep imports” - it’s useful exclusively for the better tree-shaking it provides out of the box.
If we do something like
import {A, B} from "pkg"
and the package was built withpreserveModules
it will be able to tree-shake the rest of the code in the package, while if everything is bundled in one file webpack seems to be unable from our tests.