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.

Issue when sharing a treeshaked library

See original GitHub issue

I have the following setup: Application A loads a button from react-bootstrap like this: import Button from "react-bootstrap/Button" In shared I have: "shared": {"react-bootstrap" : {}} I can see in the bundle that only the code for Button is bundled and shared and not the entire library which is ok.

I have application B which loads a modal from react-bootstrap like this: import { Modal } from "react-bootsrap" In shared I have: "shared": {"react-bootstrap" : {}} I can see that the entire “react-bootstrap” library is bundled because of the unoptimized import which is ok.

I run application A and dynamically import application B and render it inside it. Expectation: Application A doesn’t provide the Modal so application B will load its own chunk which contains it and take it from there. Reality: Application B cannot find Modal and is not rendered correctly.

If i change the import in application A to import { Button } from "react-bootstrap" then the sharing happens correctly. Does that mean that only entire libraries can be shared and not pieces of them?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
alex-vukovcommented, Jan 31, 2021

Thank you @jacob-ebey for the suggestion! Sharing single components works, however it brings the inconvenience that you have to track each component you have used in the code in order to add it to “shared”. Another option that I found useful is to use "shared": {"react-bootstrap/": {}}. Adding the trailing slash at the end shares all the single components in the folder at once but if you use only 3-4 of them the other (probably around 80) unused chunks take extra space on the server. So I guess I am looking for something in the middle between the two options i.e. “share only those components from a library which are used by my application without having to specify them one by one”. Maybe this is more of a feature request but I believe it would be helpful

0reactions
mjhenkescommented, May 18, 2021

Thanks @ScriptedAlchemy ! What book are you referring to?

edit: Derp, never mind i found it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree shaking not working for ES module library #9337 - GitHub
js TextContainer is exported as a function. Steps I've tried to resolve this issue: Setting sidEffects: false in package.json [1] and ...
Read more >
Tree-Shaking Problems with Component Libraries - Medium
If you're publishing a component library, offer an ES Module build — and avoid rolling all your modules into a single file. PropTypes...
Read more >
How to write a tree-shakable component library
The Problem​​ and you would import only a single component from your library into your app your whole library would get loaded and...
Read more >
Why doesn't Rollup.js treeshake my library - Stack Overflow
It works fine but when I use Rollup to bundle and treeshake it, it always includes the whole goodcore library, even when I...
Read more >
How to Make Your React Component Library Tree Shakeable
Test that third-party libraries are tree shakeable before you leverage them. Did you find this post useful? Let me know by sharing it...
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