Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle size is bigger than necessary due to ES6 imports #5079

Closed
ruiaraujo opened this issue May 6, 2017 · 13 comments
Closed

Bundle size is bigger than necessary due to ES6 imports #5079

ruiaraujo opened this issue May 6, 2017 · 13 comments

Comments

@ruiaraujo
Copy link
Contributor

I am using react-router-dom and webpack 2 and it seems the tree shaking does not work well enough to remove unused modules.

I managed to fix it locally by changing the import style from

import { Router } from 'react-router' 

to

import Router from 'react-router/Router' 

everywhere.

Version

4.0.0

Is this something that would be interesting to do a PR on?

@pshrmn
Copy link
Contributor

pshrmn commented May 6, 2017

I don't know enough about tree shaking to give any real input on this, but I will mention that this is related to #4432.

@ruiaraujo
Copy link
Contributor Author

The ES build with webpack 2.3 does not solve the issue. Only changing all the imports improved the situation since any import to react-router will bring in all the modules.

@timdorr
Copy link
Member

timdorr commented May 7, 2017

Are you sure you're using the ES build? It leaves import/exports untouched, so Webpack should be able to tree-shake it.

I'm thinking this is either some misconfiguration of webpack or babel on your end. Unfortunately, it's easy to mess up because Webpack doesn't make it obvious when you're actually using ES modules or not.

@ruiaraujo
Copy link
Contributor Author

Yes I am sure, see the attached image. The StaticRouter, Prompt and createMemoryHistory should be removed they stay there.

When I check the reason using the webpack stats analyser, the reason for those files being included is the index file of react-router.

stats analyser

@ruiaraujo
Copy link
Contributor Author

These issues might be relevant.

webpack/webpack#4080
mishoo/UglifyJS#1261

Also the image above was with using a babel plugin (babel-plugin-transform-imports) to do direct import otherwise the bundle looks like this. Nothing gets removed.
no transform imports

@timdorr
Copy link
Member

timdorr commented May 7, 2017

But are you sure it's using the es build that's included in the npm package? That's the key thing. What if you alias react-router to react-router/es?

@ruiaraujo
Copy link
Contributor Author

I am sure (notice the 'es' folder above in the screenshots) and have tried the alias with the same result.

So I am pretty sure that the build is pointing to the ES build.

My question is whether a PR would be accepted with a change to fix this problem.

@timdorr
Copy link
Member

timdorr commented May 8, 2017

Sure, but what would be the appropriate change? AFAIK, we're doing the same thing on Redux and it works fine there.

@ruiaraujo
Copy link
Contributor Author

The change would the react router dom export refer to direct import instead of importing the top level of react-router.

I have tried this locally and it did fixed it.

@chrisdl
Copy link

chrisdl commented Jul 3, 2017

Would fixing this issue solve this you think? @ruiaraujo.

I've recently upgraded to webpack 3 and am trying to optimize my bundle with the new webpack.optimize.ModuleConcatenationPlugin().

# In my build command.
[35] ../node_modules/react-router-dom/es/index.js + 23 modules 41.6 kB {0} [built]
       ModuleConcatenation (inner): module is used with non-harmony imports from ...[list of my components importing { STUFF } from 'react-router'dom']

@ruiaraujo
Copy link
Contributor Author

ruiaraujo commented Jul 7, 2017

@chrisdl That is odd. You can try that branch in your local project and report back.

@chrisdl
Copy link

chrisdl commented Jul 8, 2017

@ruiaraujo im actually having some issues installing the PR locally. Is there a better way than using to your knowledge?

yarn add ReactTraining/react-router#5095/head

@comerc
Copy link

comerc commented Jul 19, 2017

Do you know about babel-plugin-import?

ruiaraujo added a commit to ruiaraujo/react-router that referenced this issue Aug 1, 2017
timdorr pushed a commit that referenced this issue Aug 2, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants