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

Use direct import to react-router. #5095

Merged
merged 1 commit into from
Aug 2, 2017

Conversation

ruiaraujo
Copy link
Contributor

Fix #5079.

@ruiaraujo ruiaraujo force-pushed the smallerBundle branch 2 times, most recently from 987945e to cf17680 Compare May 10, 2017 22:23
@@ -1 +1 @@
export { Router as default } from 'react-router'
export { default } from 'react-router/Router'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Just picking one of these at random, but this applies to all the re-exports).

Unfortunately, in the packaged form, the ES module versions of these files actually live in /es/. So to get tree shaking benefits beyond these exports, you would actually have to link to react-router/es/Router. But that would break non-ES-aware bundlers.

I'm not sure if there's a good option here. If you import directly from react-router/es/Router, that is the best way to get tree-shaking benefits. Perhaps we should remove the re-exports entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing re-exports would be a major change so maybe it is a bit drastic.

Also what tree shaking benefits would you hope to get that direct imports to not give you?

Since react-router mainly exports a single module from each file, tree shaking actually does not provide any value beyond removing unused modules/file which we achieve here through direct imports.

Linking to the CommonJS build by default is just the safer option but actually I think I can improve this.

Copy link
Contributor Author

@ruiaraujo ruiaraujo May 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually to improve on this, my initial idea was a bit of overkill.

I was thinking of a custom Babel plugin that will rewrite the export to the right direct import depending on the build.

So I decided to go the lazy route. Check it out.

@ruiaraujo
Copy link
Contributor Author

@timdorr Any chance of taking a look at this again?

@timdorr
Copy link
Member

timdorr commented May 18, 2017

Yeah, I will soon, but I have limited time available for the rest of the month. Big product release (2 years of work!) is coming up on the 30th and I need to focus on that.

@ruiaraujo
Copy link
Contributor Author

Good luck then! 🤞

@ruiaraujo
Copy link
Contributor Author

@timdorr Hope your release went fine. Any time available to take a look at this?

@mjackson
Copy link
Member

Just to be clear, @ruiaraujo, this change is for people who are using babel-plugin-transform-imports with the router, right? Is that plugin not able to understand the export { Something as default } from '...' yet?

@mjackson
Copy link
Member

Also, isn't this the kind of thing that tree-shaking was designed to help out with?

@timdorr
Copy link
Member

timdorr commented Jul 13, 2017

Just to be clear, @ruiaraujo, this change is for people who are using babel-plugin-transform-imports with the router, right?

Nope, this is for webpack 2/3 support for ES modules. The direct re-exporting (export { Router as default } from 'react-router') is apparently not tree-shake-able by webpack. This will reduce bundle sizes significantly for those that are using the es build with webpack 2/3.

@timdorr
Copy link
Member

timdorr commented Jul 13, 2017

BTW, this is basically our exact issue: webpack/webpack#2867 (comment) It re-exports from a file, not a npm module, but it's the same use case.

@mjackson
Copy link
Member

Seems like a webpack bug, not ours.

@pshrmn pshrmn mentioned this pull request Jul 14, 2017
@timdorr
Copy link
Member

timdorr commented Jul 14, 2017

More like a misunderstand of what webpack means by "tree shaking". It's different from Rollup's. Hopefully the module concatenation stuff helps it out too, since it's somewhat related.

@mjackson
Copy link
Member

So you're saying there's a blessed way to declare your imports if you want to take advantage of webpack's tree shaking? Because if you declare it like export { Something as default } from '...' webpack doesn't understand that?

@ruiaraujo
Copy link
Contributor Author

With this import style we do not rely on tree shaking at all because the dependency is declared directly.
It does not affect consumers at all besides the fact they get smaller bundles compared to before.

@mjackson
Copy link
Member

@ruiaraujo What do you mean by "the dependency is declared directly"? Can you please be a little more specific?

@ruiaraujo
Copy link
Contributor Author

@mjackson If this PR is merged, react-router-dom will import MemoryRouter from react-router in practice as
import MemoryRouter from 'react-router/es/MemoryRouter' instead of
import {MemoryRouter} from 'react-router'

So this is what I mean by a directly imported dependency. Sorry for not being more explicit before.

@mjackson
Copy link
Member

Ah, so we're using our own poor-man's tree shaking so webpack doesn't have to implement it for real? Bah. That's lame.

@ruiaraujo
Copy link
Contributor Author

Apologies for being pragmatic in my approach. ;)

@mjackson
Copy link
Member

Oh, I don't blame you @ruiaraujo ;) I think we can probably go ahead and merge this for now. Thanks for the PR.

@ruiaraujo
Copy link
Contributor Author

I have rebased and fixed the conflicts.

@avocadowastaken
Copy link

Take a look to babel-plugin-direct-import, it works with RR3 and RR4.

@timdorr
Copy link
Member

timdorr commented Aug 2, 2017

Thanks, @ruiaraujo. Based on Michael's comments, it looks like we're good to go here.

@timdorr timdorr merged commit 9662a48 into remix-run:master Aug 2, 2017
@ruiaraujo ruiaraujo deleted the smallerBundle branch August 2, 2017 14:50
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 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

Successfully merging this pull request may close these issues.

4 participants