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.

[build] Bundling with Rollup

See original GitHub issue

Expected Behavior

Bundling with Rollup should work as long as MUI provides ES modules (https://github.com/rollup/rollup/wiki/ES6-modules#es6-friendly-libraries).

Current Behavior

Currently, there is an index.es.js file which is an ES module itself, but it points at CommonJS modules (https://unpkg.com/material-ui@1.0.0-beta.32/index.es.js). This then causes Rollup to fail to build:

[!] Error: 'BottomNavigationAction' is not exported by node_modules/material-ui/BottomNavigation/index.js
https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
node_modules/material-ui/index.es.js (9:38)
 7: export { default as Avatar } from './Avatar';
 8: export { default as Badge } from './Badge';
 9: export { default as BottomNavigation, BottomNavigationAction } from './BottomNavigation';
                                          ^
10: export { default as Button } from './Button';
11: export { default as ButtonBase } from './ButtonBase';

Presumably, this is because https://github.com/rollup/rollup-plugin-commonjs cannot fully understand the CommonJS exports (unless you manually specify them via namedExports option).

Steps to Reproduce (for bugs)

I have a simple repro available here: https://github.com/NMinhNguyen/material-ui-rollup-example

It also contains a branch where I modified yarn.lock to point at a temporary fork of MUI that publishes ES modules (source), and bundling with Rollup works without any other changes. I am willing to raise a PR with this change (I’ll probably also replace the current build:es2015modules script as I believe my change supersedes it, but not sure if removing index.es.js could be considered a breaking change?), but I thought I’d raise an issue first (this is also what the contributing guide suggests) 🙂

I have also created this branch where I committed the current build output and how it changes with my changes so you can more easily review them as well as spot any possible regressions. And here you can find the actual ES5 modules.

By the way, the reason I would rather not use the es folder is because that folder is ESNext (2015+), so I’d have to transpile it via Babel myself. This would mean that I’d additionally have to include these plugins to ensure I get the same Babel transformations applied: https://github.com/mui-org/material-ui/blob/8497cd976d74cfd102c6e8afb932348b366dd5b3/.babelrc#L70-L83

Context

We are using Rollup to bundle our code.

Your Environment

Tech Version
Material-UI 1.0.0-beta.32
React 16.2.0
Rollup 0.55.3
rollup-plugin-commonjs 8.3.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
oliviertassinaricommented, Feb 11, 2018

I still have a feeling that if a library ensures that its ES output only imports ES modules, then the risk of module duplication is significantly reduced?

@NMinhNguyen Yes, I agree with this point at the condition all the third party libraries you want to use follow this pattern. Hard to enforce.

would you be open to me raising a smaller PR that does just that?

Yes, please! I thought it was already done. It can be considered a bug on our end.

0reactions
estaubcommented, Apr 10, 2018

fwiw, here’s a different experience and motivation:

When using dynamic bundle loading, initial page-load gains can be very much larger than those @dantman’s showing above. My initial load mui stat size for about half of my pages is about 300K (30K gzipped). The other half need another bundle, with another 300K (22K gzipped) of mui. A few rarely-used components are sprinkled elsewhere in bundles where they are used. I’m just telling webpack 4 to split things up as it deems best; I’m not hand-tweaking all of this.

I’m trying to keep the app viable for use on slow devices with low bandwidth.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
Step 1: Install Rollup and create a configuration file. ... Next, create a new file called rollup.config.js in the learn-rollup folder. Inside, add...
Read more >
rollup.js
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library...
Read more >
The Ultimate Guide to Getting Started with the Rollup.js ...
Rollup.js is a Node.js module bundler most often used for client-side JavaScript running in the browser. (You can bundle Node.js scripts but ...
Read more >
A Beginners Guide to Rollup Module Bundler - Section.io
Step 2: Creating your first Rollup bundle. Start by creating a src folder. This folder will house several JavaScript files and in that...
Read more >
An Introduction to the Rollup.js JavaScript Bundler - SitePoint
Rollup.js can use a JavaScript configuration file to define bundling options. The default name is rollup.config.js and it should be placed in ...
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