question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Rollup's preserveModules

See original GitHub issue

Current 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:open
  • Created 4 years ago
  • Reactions:27
  • Comments:18

github_iconTop GitHub Comments

6reactions
markeriksoncommented, Feb 14, 2020

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

4reactions
BeeeQueuecommented, Apr 18, 2020

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 with preserveModules 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

rollup.js
js. Rollup allows you to write your code using the new module system, and will then compile it back down to existing supported...
Read more >
Rollup Exceptions to preserveModules - Stack Overflow
My rollup config uses preserveModules and output.dir to avoid bundling modules. import resolve from 'rollup-plugin-node-resolve'; import ...
Read more >
Rollup Config for React Component Library With TypeScript + ...
The getFiles method is a utility method that I've written to help me deep retrieve all those files with .js, .jsx, .ts and...
Read more >
@rollup/plugin-typescript - npm
Supported transformer factories: all built-in TypeScript custom transformer factories: import('typescript').TransformerFactory annotated ...
Read more >
Rollup Library Starter - DEV Community ‍ ‍
The preserveModules option will tell Rollup to create separate chunks for all modules, using the original module names as file names.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found