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

Make the vendor bundle hold vendor code #307

Merged
merged 1 commit into from
Jul 21, 2016

Conversation

justin808
Copy link
Member

@justin808 justin808 commented Jul 20, 2016

By setting the vendor libs as entry points, we force the vendor code
into the vendor-bundle.js. This has the advantage in that app code
should be changing less often, so client browsers will have the vendor
bundle cached longer, and the smaller app bundle can be refreshed.


This change is Reviewable

@justin808
Copy link
Member Author

Review status: 0 of 3 files reviewed at latest revision, 2 unresolved discussions.


client/package.json, line 83 [r1] (raw file):

    "react-router-redux": "^4.0.5",
    "redux": "^3.5.2",
    "redux-promise": "^0.5.3",

This wasn't used.


client/webpack.client.base.config.js, line 37 [r1] (raw file):

      'react-on-rails',
      'react-router-redux',
      'immutable',

@robwise @alexfedoseev Does this look right? Worth committing this? without this change, the vendor-bundle.js is much smaller than the app-bundle.js, because libraries like react are in the app-bundle.js.


Comments from Reviewable

@justin808 justin808 force-pushed the move-vendor-libs-to-vendor-bundle branch from a7690e4 to fada45b Compare July 20, 2016 03:13
@robwise
Copy link
Contributor

robwise commented Jul 20, 2016

Reviewed 3 of 3 files at r1.
Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


client/webpack.client.base.config.js, line 37 [r1] (raw file):

Previously, justin808 (Justin Gordon) wrote…

@robwise @alexfedoseev Does this look right? Worth committing this? without this change, the vendor-bundle.js is much smaller than the app-bundle.js, because libraries like react are in the app-bundle.js.

@justin808 yes, very important for the reasons you describe! This is an important thing to point out to people IMO if you are going to be chunking bundles so I agree with you doing this.

Should we add:

  • jquery-ujs
  • react-router
  • redux
  • react-router-redux (Alex and I aren't fans of this one though)
  • classnames

Comments from Reviewable

@alex35mil
Copy link
Member

Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


client/webpack.client.base.config.js, line 37 [r1] (raw file):

Previously, robwise (Rob Wise) wrote…

@justin808 yes, very important for the reasons you describe! This is an important thing to point out to people IMO if you are going to be chunking bundles so I agree with you doing this.

Should we add:

  • jquery-ujs
  • react-router
  • redux
  • react-router-redux (Alex and I aren't fans of this one though)
  • classnames
Also keep in mind: if in your vendor array you have `react-router` and somewhere in the codebase you import `react-router/es6` — this import won't get to `vendor` bundle.

Comments from Reviewable

@alex35mil
Copy link
Member

Reviewed 3 of 3 files at r1.
Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


Comments from Reviewable

@robwise
Copy link
Contributor

robwise commented Jul 20, 2016

Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


client/webpack.client.base.config.js, line 37 [r1] (raw file):

Previously, alexfedoseev (Alex Fedoseev) wrote…

Also keep in mind: if in your vendor array you have react-router and somewhere in the codebase you import react-router/es6 — this import won't get to vendor bundle.

oh I didn't know that, that's a good gotcha

What about import _ from 'lodash/fp';?


Comments from Reviewable

@alex35mil
Copy link
Member

Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


client/webpack.client.base.config.js, line 37 [r1] (raw file):

Previously, robwise (Rob Wise) wrote…

oh I didn't know that, that's a good gotcha

What about import _ from 'lodash/fp';?

Shouldn't get unless `lodash/fp` is explicitly included in vendor array.

Comments from Reviewable

@justin808
Copy link
Member Author

Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


client/webpack.client.base.config.js, line 37 [r1] (raw file):

jquery-ujs
Imported elsewhere as an entry point: https://github.com/shakacode/react-webpack-rails-tutorial/blob/move-vendor-libs-to-vendor-bundle/client/webpack.client.rails.build.config.js

config.entry.vendor.unshift(
  'es5-shim/es5-shim',
  'es5-shim/es5-sham',
  'jquery-ujs',

  // Configures extractStyles to be true if NODE_ENV is production
  'bootstrap-loader/extractStyles'
);

react-router
redux
I figured those will get picked up by the other libs as dependencies. So why include those.

react-router-redux (Alex and I aren't fans of this one though)
classnames
I'll add those!


Comments from Reviewable

By setting the vendor libs as entry points, we force the vendor code
into the vendor-bundle.js. This has the advantage in that app code
should be changing less often, so client browsers will have the vendor
bundle cached longer, and the smaller app bundle can be refreshed.

Warning on what gets included:

If in your vendor array you have react-router and
somewhere in the codebase you import react-router/es6 — this import
won't get to vendor bundle.
@justin808 justin808 force-pushed the move-vendor-libs-to-vendor-bundle branch from fada45b to e058da3 Compare July 21, 2016 03:15
@justin808 justin808 merged commit 5c44c3d into master Jul 21, 2016
@justin808 justin808 deleted the move-vendor-libs-to-vendor-bundle branch July 21, 2016 03:47
@alex35mil
Copy link
Member

@justin808 @robwise It's not the end of the story tho. This change does bust the cache, but there is another issue: it busts it on every rebuild, even if vendor modules bundle wasn't changed at all. This happens b/c of the way webpack identifies dependencies — it uses integer ids, so when any new import is added to the app bundle, ids are changed and hashes of all bundles are invalidated.

It's an old ongoing issue, there are a several workarounds, but non of them is perfect. Here is the long read on topic: webpack/webpack#1315

@justin808
Copy link
Member Author

Wow @alexfedoseev! great reference! So basically, this is sort of pointless, except, if you want to open up the small app-bundle file and see what Webpack is doing.

@alex35mil
Copy link
Member

alex35mil commented Jul 21, 2016

@justin808 Sometimes yes, sometimes no. It's still useful when you use code splitting w/ dynamic chunks loading. Also it can be solved for some cases. Previously I used chunk-manifest-webpack-plugin, but it broke at some point. Other way I haven't tried yet is to use constant string ids for deps w/ NamedModulesPlugin, it will add some fat to the bundle (altho gzip will cut off most of it), but this way ids of the deps won't be changed in time. TBH I'm waiting for stable @2 release before digging into this issue again, b/c it might be solved there.

@robwise
Copy link
Contributor

robwise commented Jul 21, 2016

So basically, this is sort of pointless, except, if you want to open up the small app-bundle file and see what Webpack is doing.

Absolutely not. Even with the cache busting issue, that's only an issue when re-deploying. This is still useful because it's not requiring you to keep re-downloading the same JS over and over again as you visit pages with different bundles on them. If you don't do this, and you have 8 bundles, then you're going to have 8 instances of jquery, one in each bundle, that you must keep re-downloading over and over. Same for all the other ones.

I did this on our F&G in the first place because it was taught here: https://egghead.io/lessons/tools-grouping-vendor-files-with-the-webpack-commonschunkplugin. Without this, our bundles were averaging 5MB each, with this change, we got the vendor bundle to 2MB and the other bundles to ~120KB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants