GoogleChromeLabs / webpack-libs-optimizations

Using a library in your webpack project? Here’s how to optimize it
Apache License 2.0
3.37k stars 111 forks source link

Babel optimization: require helpers instead of inlining them #12

Open iamakulov opened 6 years ago

iamakulov commented 6 years ago

By installing babel-plugin-transform-runtime and babel-runtime, developers might remove duplicated Babel helpers across their bundle: https://github.com/babel/babel-loader#babel-is-injecting-helpers-into-each-file-and-bloating-my-code

We should add this to the list of optimizations.

kurtextrem commented 6 years ago

In my case adding those actually increases the bundle size, so ymmv: Before: http://prntscr.com/jofzbf After: http://prntscr.com/jofz6g (however this is one bundle, it might only apply to multiple-bundle configs)

iamakulov commented 6 years ago

Oh wow. Looks like the bundle got increased because of core-js – and I think this might be the reason:

image

(From https://babeljs.io/docs/plugins/transform-runtime/#why)

I’ll check how it works with polyfills and regenerator disabled as soon as I get time:

{
  "plugins": [
    ["transform-runtime", {
      "polyfill": false,
      "regenerator": true
    }]
  ]
}
kurtextrem commented 6 years ago

Using that config (I also tried regenerator: false) it results in a 3 kB larger bundle for me - as parts of core-js are added.

iamakulov commented 6 years ago

Hm. Finally tested this with a production Next.js app and @babel/plugin-transform-runtime@7.0.0-beta.42, and it looks like this consistently makes all bundles several kBs smaller:

(Left is with the plugin; right is without it) image

Do you, by chance, remember the project/bundle you were testing this on?

kurtextrem commented 6 years ago

I don't really know what happened, but using 7.0.0 it indeed reduces my bundle size (3 kB). Using the exact same setup, just upgraded deps.

side note: polyfill got removed