--- url: "https://web.dev/articles/decrease-frontend-size" titulek: "Decrease front-end size | Articles | web.dev" h1: "Decrease front-end size Stay organized with collections Save and categorize content based on your preferences." popis: "How to use webpack to make your app as small as possible" jazyk: "en" kanonicka: "https://web.dev/articles/decrease-frontend-size" drobecky: ["Articles", "Decrease front-end size"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:35:55" znaku: 20121 slov: 2713 kodovani: "iso-8859-2" sha256: "4f67cbacd36e59f937417c96c01c85a1c4e7386dc114e4b739a7d25d6e08b3cb" --- Decrease front-end size Stay organized with collections Save and categorize content based on your preferences. How to use webpack to make your app as small as possible Ivan Akulov One of the first things to do when you’re optimizing an application is to make it as small as possible. Here’s how to do this with webpack. Use the production mode (webpack 4 only) Webpack 4 introduced [the new `mode` flag](https://webpack.js.org/concepts/mode/). You could set this flag to `'development'` or `'production'` to hint webpack that you’re building the application for a specific environment: `// webpack.config.js module.exports = { mode: 'production', };` Make sure to enable the `production` mode when you’re building your app for production. This will make webpack apply optimizations like minification, removal of development-only code in libraries, [and more](https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a). Further reading [What specific things the `mode` flag configures](https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a) Enable minification Minification is when you compress the code by removing extra spaces, shortening variable names and so on. Like this: `// Original code function map(array, iteratee) { let index = -1; const length = array == null ? 0 : array.length; const result = new Array(length); while (++index < length) { result[index] = iteratee(array[index], index, array); } return result; }` ↓ `// Minified code function map(n,r){let t=-1;for(const a=null==n?0:n.length,l=Array(a);++t { return 'Rendered!'; }; export const commentRestEndpoint = '/rest/comments'; // index.js import { render } from './comments.js'; render();` Webpack understands that `commentRestEndpoint` is not used and doesn’t generate a separate export point in the bundle: `// bundle.js (part that corresponds to comments.js) (function(module, __webpack_exports__, __webpack_require__) { "use strict"; const render = () => { return 'Rendered!'; }; /* harmony export (immutable) */ __webpack_exports__["a"] = render; const commentRestEndpoint = '/rest/comments'; /* unused harmony export commentRestEndpoint */ })` The minifier removes the unused variable: `// bundle.js (part that corresponds to comments.js) (function(n,e){"use strict";var r=function(){return"Rendered!"};e.b=r})` This works even with libraries if they are written with ES modules. You aren’t required to use precisely webpack’s built-in minifier (`UglifyJsPlugin`) though. Any minifier that supports dead code removal (e.g. [Babel Minify plugin](https://github.com/webpack-contrib/babel-minify-webpack-plugin) or [Google Closure Compiler plugin](https://github.com/roman01la/webpack-closure-compiler)) will do the trick. Further reading [“ES6 Modules in depth”](https://ponyfoo.com/articles/es6-modules-in-depth) Webpack docs [about tree shaking](https://webpack.js.org/guides/tree-shaking/) Optimize images Images account for [more than a half](http://httparchive.org/interesting.php?a=All&l=Oct%2016%202017) of the page size. While they are not as critical as JavaScript (e.g., they don’t block rendering), they still eat a large part of the bandwidth. Use `url-loader`, `svg-url-loader` and `image-webpack-loader` to optimize them in webpack. [`url-loader`](https://github.com/webpack-contrib/url-loader) inlines small static files into the app. Without configuration, it takes a passed file, puts it next to the compiled bundle and returns an url of that file. However, if we specify the `limit` option, it will encode files smaller than this limit as [a Base64 data url](https://css-tricks.com/data-uris/) and return this url. This inlines the image into the JavaScript code and saves an HTTP request: `// webpack.config.js module.exports = { module: { rules: [ { test: /\.(jpe?g|png|gif)$/, loader: 'url-loader', options: { // Inline files smaller than 10 kB (10240 bytes) limit: 10 * 1024, }, }, ], } };``// index.js import imageUrl from './image.png'; // → If image.png is smaller than 10 kB, `imageUrl` will include // the encoded image: 'data:image/png;base64,iVBORw0KGg…' // → If image.png is larger than 10 kB, the loader will create a new file, // and `imageUrl` will include its url: `/2fcd56a1920be.png`` [`svg-url-loader`](https://github.com/bhovhannes/svg-url-loader) works just like `url-loader` – except that it encodes files with the [URL encoding](https://developer.mozilla.org/docs/Glossary/percent-encoding) instead of the Base64 one. This is useful for SVG images – because SVG files are just a plain text, this encoding is more size-effective. `module.exports = { module: { rules: [ { test: /\.svg$/, loader: "svg-url-loader", options: { limit: 10 * 1024, noquotes: true } } ] } };` [`image-webpack-loader`](https://github.com/tcoopman/image-webpack-loader) compresses images that go through it. It supports JPG, PNG, GIF and SVG images, so we’re going to use it for all these types. This loader doesn’t embed images into the app, so it must work in pair with `url-loader` and `svg-url-loader`. To avoid copy-pasting it into both rules (one for JPG/PNG/GIF images, and another one for SVG ones), we’ll include this loader as a separate rule with [`enforce: 'pre'`](https://webpack.js.org/configuration/module/#rule-enforce): `// webpack.config.js module.exports = { module: { rules: [ { test: /\.(jpe?g|png|gif|svg)$/, loader: 'image-webpack-loader', // This will apply the loader before the other ones enforce: 'pre' } ] } };` The default settings of the loader are already good to go – but if you want to configure it further, see [the plugin options](https://github.com/tcoopman/image-webpack-loader#options). To choose what options to specify, check out Addy Osmani’s excellent [guide on image optimization](https://images.guide/). Further reading ["What is base64 encoding used for?"](https://stackoverflow.com/questions/201479/what-is-base-64-encoding-used-for) Addy Osmani’s [guide on image optimization](https://images.guide/) Optimize dependencies More than a half of average JavaScript size comes from dependencies, and a part of that size might be just unnecessary. For example, Lodash (as of v4.17.4) adds 72 KB of minified code to the bundle. But if you use only, like, 20 of its methods, then approximately 65 KB of minified code does just nothing. Another example is Moment.js. Its 2.19.1 version takes 223 KB of minified code, which is huge – the average size of JavaScript on a page [was 452 KB in October 2017](http://httparchive.org/interesting.php?a=All&l=Oct%2016%202017). However, 170 KB of that size is [localization files](https://github.com/moment/moment/tree/4caa268356434f3ae9b5041985d62a0e8c246c78/locale). If you don’t use Moment.js with multiple languages, these files will bloat the bundle without a purpose. All these dependencies can be easily optimized. We’ve collected optimization approaches in a GitHub repo – [check it out](https://github.com/GoogleChromeLabs/webpack-libs-optimizations)! Enable module concatenation for ES modules (aka scope hoisting) When you are building a bundle, webpack is wrapping each module into a function: `// index.js import {render} from './comments.js'; render(); // comments.js export function render(data, target) { console.log('Rendered!'); }` ↓ `// bundle.js (part of) /* 0 */ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); var __WEBPACK_IMPORTED_MODULE_0__comments_js__ = __webpack_require__(1); Object(__WEBPACK_IMPORTED_MODULE_0__comments_js__["a" /* render */])(); }), /* 1 */ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_exports__["a"] = render; function render(data, target) { console.log('Rendered!'); } })` In the past, this was required to isolate CommonJS/AMD modules from each other. However, this added a size and performance overhead for each module. Webpack 2 introduced support for ES modules which, unlike CommonJS and AMD modules, can be bundled without wrapping each with a function. And webpack 3 made such bundling possible – with [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/). Here’s what module concatenation does: `// index.js import {render} from './comments.js'; render(); // comments.js export function render(data, target) { console.log('Rendered!'); }` ↓ `// Unlike the previous snippet, this bundle has only one module // which includes the code from both files // bundle.js (part of; compiled with ModuleConcatenationPlugin) /* 0 */ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); // CONCATENATED MODULE: ./comments.js function render(data, target) { console.log('Rendered!'); } // CONCATENATED MODULE: ./index.js render(); })` See the difference? In the plain bundle, module 0 was requiring `render` from module 1. With module concatenation, `require` is simply replaced with required function, and module 1 is removed. The bundle has fewer modules – and less module overhead! To turn on this behavior, **in webpack 4**, enable the `optimization.concatenateModules` option: `// webpack.config.js (for webpack 4) module.exports = { optimization: { concatenateModules: true } };` **In webpack 3,** use the `ModuleConcatenationPlugin`: `// webpack.config.js (for webpack 3) const webpack = require('webpack'); module.exports = { plugins: [ new webpack.optimize.ModuleConcatenationPlugin() ] };` Further reading Webpack docs [for the ModuleConcatenationPlugin](https://webpack.js.org/plugins/module-concatenation-plugin/) [“Brief introduction to scope hoisting”](https://medium.com/webpack/brief-introduction-to-scope-hoisting-in-webpack-8435084c171f) Detailed description of [what this plugin does](https://medium.com/webpack/webpack-freelancing-log-book-week-5-7-4764be3266f5) Use `externals` if you have both webpack and non-webpack code You might have a large project where some code is compiled with webpack, and some code is not. Like a video hosting site, where the player widget might be built with webpack, and the surrounding page might be not: (A completely random video hosting site) If both pieces of code have common dependencies, you can share them to avoid downloading their code multiple times. This is done with [the webpack’s `externals` option](https://webpack.js.org/configuration/externals/) – it replaces modules with variables or other external imports. If dependencies are available in `window` If your non-webpack code relies on dependencies that are available as variables in `window`, alias dependency names to variable names: `// webpack.config.js module.exports = { externals: { 'react': 'React', 'react-dom': 'ReactDOM' } };` With this config, webpack won’t bundle `react` and `react-dom` packages. Instead, they will be replaced with something like this: `// bundle.js (part of) (function(module, exports) { // A module that exports `window.React`. Without `externals`, // this module would include the whole React bundle module.exports = React; }), (function(module, exports) { // A module that exports `window.ReactDOM`. Without `externals`, // this module would include the whole ReactDOM bundle module.exports = ReactDOM; })` If dependencies are loaded as AMD packages If your non-webpack code doesn’t expose dependencies into `window`, things are more complicated. However, you can still avoid loading the same code twice if the non-webpack code consumes these dependencies as [AMD packages](http://requirejs.org/docs/whyamd.html#amd). To do this, compile the webpack code as an AMD bundle and alias modules to library URLs: `// webpack.config.js module.exports = { output: { libraryTarget: 'amd' }, externals: { 'react': { amd: '/libraries/react.min.js' }, 'react-dom': { amd: '/libraries/react-dom.min.js' } } };` Webpack will wrap the bundle into `define()` and make it depend on these URLs: `// bundle.js (beginning) define(["/libraries/react.min.js", "/libraries/react-dom.min.js"], function () { … });` If non-webpack code uses the same URLs to load its dependencies, then these files will be loaded only once – additional requests will use the loader cache. Further reading Webpack docs [on `externals`](https://webpack.js.org/configuration/externals/) Summing up Enable the production mode if you use webpack 4 Minimize your code with the bundle-level minifier and loader options Remove the development-only code by replacing `NODE_ENV` with `production` Use ES modules to enable tree shaking Compress images Apply dependency-specific optimizations Enable module concatenation Use `externals` if this makes sense for you Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2017-12-18 UTC.