Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Webpack 4 speed

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Webpack 4 speed

As of July 2018, Webpack is becoming the most popular module bundler tool. It includes a bunch of features tailored for app performance. I have talked about some like Treeshaking, code splitting and support for plugins.

Avatar for Malek Hakim

Malek Hakim

July 10, 2018
Tweet

More Decks by Malek Hakim

Other Decks in Technology

Transcript

  1. What is Webpack? webpack is a static module bundler for

    modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles. - Webpack official docs
  2. Zero config assumptions: • a ./src folder as an entry

    point • will generate ./dist/main.js as an output bundle
  3. Zero config production build comes with • Chunk minimization with

    UglifyJS • Order chunks by occurence: Order the modules and chunks by occurrence. This saves space. • Module concatenation: allows modules to be wrapped into one closure function, which is faster to execute. • Adds a process.env.NODE_ENV === 'production' that would be used by some library to include production code only for a performance boost.
  4. Treeshaking Removes unnecessary, not used code. Caveat: It does that

    only on ES6 code since it relies on import and export syntax.
  5. Code splitting 3 ways: • Entry Points: Manually split code

    using entry configuration. • Prevent Duplication: Use the SplitChunks to dedupe and split chunks. • Dynamic Imports: Split code via inline function calls within modules.
  6. Prevent duplications with SplitChunks Extract common dependencies into an existing

    entry chunk or an entirely new chunk codebase home.js home.js vendors. js vendors .js here will be created with shared code.
  7. When does webpack create a new chunk? webpack will automatically

    split chunks based on these conditions: • New chunk can be shared OR modules are from the node_modules folder • New chunk would be bigger than 30kb (before min+gz) • Maximum number of parallel requests when loading chunks on demand would be lower or equal to 5 • Maximum number of parallel requests at initial page load would be lower or equal to 3 - SplitChunksPlugin
  8. Prefetching • Usually happens in the future after parent chunk

    is loaded. • Best used on user interaction. • Downloaded on browser idle time
  9. Preloading • Initiated by parent chunk and downloaded in parallel

    • It shift the request priority early in the loading • Used for important vendor scripts or font loading • Helps shaving some time for first paint and time to interactive
  10. When to use preload vs prefetch? Preload resources you have

    high-confidence will be used in the current page. Prefetch resources likely to be used for future navigations across multiple navigation boundaries. - Addy Osmani
  11. Best to extract vendor scripts into their own bundle Will

    help the client to cache the unchanged vendor scripts such as “react” or “moment”. Remember SplitChunks ?
  12. Make use of some other interesting tricks: • Use ES

    libraries like lodash-es to take advantage of Treeshaking • Use the IgnorePlugin to ignore unused moment locales. • Make use of the offlinePlugin or other plugins that enable offline experiences and add a service worker to your app. • Use url-loader to transform images into DataUrl base64 files • Extracts CSS into separate files using “mini-css-extract-plugin”. • Look for other optimizations in the wild.
  13. References: • Webpack guides: https://webpack.js.org/guides/ • Resource hints: https://w3c.github.io/resource-hints/ •

    Preload, Prefetch And Priorities in Chrome: https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf • Web performance optimization with Webpack: https://developers.google.com/web/fundamentals/performance/webpack/ • Webpack-libs-optimizations: https://github.com/GoogleChromeLabs/webpack-libs-optimizations