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.

This library is not tree-shaking friendly

See original GitHub issue

Environment

Tech Version
@material-ui/pickers 3.1.1
material-ui 4.1.1
React 16.8.6
Browser any
Peer library date-fns 2.0.0-alpha.34

Steps to reproduce

Tree-shaking with code splitting is problematic due to unnecessary bundling by rollup for es and cjs targets

Expected behavior

Pickers should be packaged like material-ui core, especially pickers are under mui organisation. I am referring to

{
  "main": "./index.js",
  "module": "./esm/index.js",
}

they have in package.json. Notice, they give path to not bundled index.js files. However, for pickers paths target bundled library by rollup.

Rollup is nice for UMD target, but for cjs and es this is really redundant, people using bundlers on app level will bundle vendor files anyway. Prebundling hurts tree-shaking and does not give any advantage what so ever. Webpack tree shaking is based on unused reexports and prebundling library won’t have those but a flat structure.

Also, we cannot even do import like import DatePicker from @material-ui/pickers/DatePicker which would target a component with es module, because the only es file is bundled. This is problematic because of Webpack issue https://github.com/webpack/webpack/issues/7782 , so you cannot tree shake a given library across multiple chunks. The remedy is to import components like import DatePicker from @material-ui/pickers/DatePicker, but because this is not of es type, it leads to disaster because material-ui/core components are not imported as es modules, so actually they will be duplicated in date picker chunk.

So please, do not bundle for es and common js targets. And if you really want to bundle for those, do it additionally, but don’t link those in main and module in package.json

Actual behavior

As above

Live example

n/a

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
klis87commented, Jun 21, 2019

@TrySound here u are https://github.com/klis87/webpack-tree-shaking

Proof that tree-shaking works for core components and does not work for pickers

1reaction
klis87commented, Jun 19, 2019

But then you make peoples life miserable. Please read arguments I made above. If you don’t wanna provide alternative builds, just don’t bundle for es/cjs targets. Not bundled esm is tree-shakeable and not bundled cjs files can also be run on server (why to bundle for server usage anyway?)

Please give me at least one argument for bundling for es/cjs with rollup. I gave you tons of serious disadvantages. Plus, you are under @material-ui org and they allow such imports, thanks to which they are friendly library to make optimisations, and I believe you should conform to their way, as pickers are now somehow official mui components.

From maintanance point of view not bundling is simpler than bundling, you just need to bundle for UMD and thats it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

This library is not tree-shaking friendly · Issue #1132 - GitHub
Webpack tree shaking is based on unused reexports and prebundling library won't have those but a flat structure. Also, we cannot even do...
Read more >
How To Make Tree Shakeable Libraries - Theodo blog
... a tree shaking friendly configuration. Unfortunately, this does not automatically imply that your library is in fact tree shakeable!
Read more >
Tree-Shaking Problems with Component Libraries - Medium
* Ensure no compilers transform your ES2015 module syntax into CommonJS modules (this is the default behavior of the popular Babel preset @babel ......
Read more >
Why is my React component library not tree-shakable?
I found one possible solution to my problem. It has nothing to do with tree-shaking though. I'm simply splitting the library into several ......
Read more >
Building a Tree Shaking Friendly JavaScript Package
Building a Tree Shaking friendly JavaScript Package ... We only imported ”isArray” from “lodash” and not the whole library, and why did 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