<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nimesh on Medium]]></title>
        <description><![CDATA[Stories by Nimesh on Medium]]></description>
        <link>https://medium.com/@nimgrg?source=rss-371556390e38------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sfK0OWpHmtczZ2va.jpg</url>
            <title>Stories by Nimesh on Medium</title>
            <link>https://medium.com/@nimgrg?source=rss-371556390e38------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 18:35:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nimgrg/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Analysing and minimising the size of client side bundle with webpack and source-map-explorer]]></title>
            <link>https://medium.com/netscape/analysing-and-minimising-the-size-of-client-side-bundle-with-webpack-and-source-map-explorer-41096559beca?source=rss-371556390e38------2</link>
            <guid isPermaLink="false">https://medium.com/p/41096559beca</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Nimesh]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 06:01:02 GMT</pubDate>
            <atom:updated>2017-09-19T16:14:04.107Z</atom:updated>
            <content:encoded><![CDATA[<p>TLDR;</p><ol><li>Use <strong><em>source-map-explorer</em></strong> to analyse your bundle dependencies.</li><li>Watch out for server side modules bleeding into the client side causing unneeded polyfills to be bundled when building an isomorphic app.</li><li>You may not need all of <strong><em>babel-polyfill</em></strong>, pick and chose the polyfills you need.</li><li>Don’t install the whole library if it allows for modular installs.</li><li><strong><em>webpack </em></strong>alias is a powerful feature that can be used to minimise the bundle size.</li></ol><p>Recently I have had the opportunity to take a <strong><em>browserify</em></strong> and <strong><em>gulp</em></strong> based project and convert it to <strong><em>webpack</em></strong>. And along with it also came the opportunity to look at and analyse the size of the client side bundle and see what extra fat could be cut off. What followed was interesting and the same time a bit extreme approach to cutting off the extra fat from the client side bundle. Some of the situations I ran into were also due to the fact that we were building an isomorphic app, so may not apply to everyone.</p><h3>Analysing the client side bundle</h3><p>To analyse the client side bundle, I initially started off with <a href="https://www.npmjs.com/package/webpack-bundle-analyzer"><strong><em>webpack-bundle-analyzer</em></strong></a>, although very visual and very easy to get setup, I found the UI hard to navigate around [although this might not be the case with the latest release]. At the same time I came across this tweet about <a href="https://www.npmjs.com/package/source-map-explorer"><strong><em>source-map-explorer</em></strong></a>.</p><h3>Addy Osmani on Twitter</h3><p>Why is my JS bundle so big? source-map-explorer helps answer this q &amp;amp; works with any source map. Saved 100s of KB 🔥 https://t.co/XFy65zjlhO</p><p>I was sold on <strong><em>source-map-explorer</em></strong> as soon as I saw how easy it was to navigate around the UI. One of the other benefit of <strong><em>source-map-explorer </em></strong>is also the fact that you don’t really need <strong><em>webpack</em></strong> to analyse your bundle. As long as you have a source map for your bundled file you are good to go.</p><p>I ran my analysis on the minified bundle of the app, as at the end of the day that is what get’s deployed to the end users browsers. With that in mind you can simply run an analysis on your minified file using the following commands</p><pre>//Install source-map-explorer globally</pre><pre>$ npm install -g source-map-explorer </pre><pre>$ source-map-explorer /path/to/bundle.js /path/to/bundle.js.map</pre><p>This will automatically open up a browser for you with a chart showing you your client side bundles module dependencies. From then you can easily navigate around the UI and see the dependencies that are being pulled in.</p><h3>Modular install of library</h3><p>When starting with this process one of the most obvious bloat on our client site bundle was <strong><em>lodash</em></strong>, although we were careful of not pulling in the whole of <strong><em>lodash </em></strong>into the project, somehow it had made it to the codebase.</p><pre>import { debounce } from &#39;lodash&#39;</pre><p>It was obvious that we only needed <strong><em>debounce</em></strong> from <strong><em>lodash,</em></strong> so I uninstalled <strong><em>lodash </em></strong>and installed <strong><em>lodash.debouce. </em></strong>Changed the import to:</p><pre>import debouce from &#39;lodash.debounce&#39;</pre><p>This automatically brought the client side bundle size down by <strong><em>50kB</em></strong>, which is quite significant drop in the bundle size for tiny bit of effort.</p><h3>Using IgnorePlugin to ignore modules</h3><p>After the obvious fix, I was seeing modules,<em> </em>which of were of no use on the client side. Modules like <strong><em>mv, rim-raf </em></strong>and <strong><em>source-map-support, </em></strong>for our web app just did not make any sense. Fortunately <strong>w<em>ebpack </em></strong>comes with a handy <strong><em>IgnorePlugin </em></strong>which I used for this scenario to exclude these modules.</p><pre>//webpack.config.js</pre><pre>{<br>  ...<br>  plugins: [<br>      new webpack.IgnorePlugin(/^(mv|rim-raf|source-map-support)$/)<br>  ]<br>  ...<br>}</pre><p>This managed to shave off another <strong><em>67kB </em></strong>from<strong><em> </em></strong>the bundle. I was still seeing polyfills<strong><em> </em></strong>for<strong><em> </em></strong>node core modules like <strong><em>http, stream </em></strong>and <strong><em>buffer </em></strong>on the client side bundle, which I was pretty certain we did not need. Trying to exclude these using the <strong><em>IgnorePlugin </em></strong>did not work. I parked these, to tackle them later on in the process.</p><h3>Removing babel-polyfill</h3><p>From then on the next big piece of chunk in the bundle I was seeing was <strong><em>babel-polyfill </em></strong>which came in at <strong><em>74.2</em></strong> <strong><em>kB</em></strong><em> </em>for the minified bundle<strong><em>.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8VkcPFy8TRisFXmuHax5w.png" /></figure><p>I knew that we were not using all of the ES6 features and very few ES7 features, plus there was an additional overhead of <strong><em>5.69 kB </em></strong>for the <strong><em>regenerator-runtime </em></strong>module to support generator functions, which we were not using at all. After going through the <strong><em>babel</em></strong> docs I figured out that, you can actually pull in only the polyfills you are currently using or want to use using <a href="https://github.com/zloirock/core-js"><strong><em>core-js</em></strong></a><strong><em>. </em></strong>So instead of pulling in <strong><em>babel-polyfill </em></strong>into our app, I simply pulled in the dependencies that were needed for the app by creating a file called <strong><em>es6-polyfill.js </em></strong>as below.</p><pre>//es6-polyfill.js</pre><pre>import &#39;core-js/es6/array&#39;<br>import &#39;core-js/es6/function&#39;<br>import &#39;core-js/es6/map&#39;<br>import &#39;core-js/es6/math&#39;<br>import &#39;core-js/es6/number&#39;<br>import &#39;core-js/es6/object&#39;<br>import &#39;core-js/es6/promise&#39;<br>import &#39;core-js/es6/regexp&#39;<br>import &#39;core-js/es6/string&#39;<br>import &#39;core-js/fn/array/includes&#39;</pre><p>And simply pulled in this file as the first dependency on our client side entry file. This ended up shaving off <strong><em>59kB </em></strong>from your client side bundle. Win Win. The above approach also get’s rid of <strong><em>es5-shims </em></strong>from <strong><em>babel-polyfill, </em></strong>which we did not need, as were targeting browsers that were <strong><em>IE10 </em></strong>and greater.</p><h3>Getting rid of multiple promise polyfills</h3><p>At this point I was quite into the process and looking at every possible way I could reduce any extra fat necessary off of the client side bundle. So this next one is a bit of adventurous one. We were using <a href="https://github.com/yahoo/fetchr"><strong><em>fetchr</em></strong></a><strong><em> </em></strong>to make api calls on client side and server side. One thing I realised going through <strong><em>source-map-explorer </em></strong>was that <strong><em>fetchr </em></strong>bring’s it’s own implementation of promise with it i.e. <strong><em>es6-promise. </em></strong>However we were already polyfilling promise support with <strong><em>core-js </em></strong>for the app<strong><em>. </em></strong>At this point I wanted to experiment and see if I could only use one implementation of promise for the whole of the app including it’s dependencies. <strong><em>webpack’s </em></strong>alias feature came in pretty handy here.</p><pre>//webpack.config.js</pre><pre>{<br>...<br>resolve: {<br>    alias: {<br>      &#39;es6-promise$&#39;: path.resolve(__dirname, &#39;src/shims/shim-es6-promise.js&#39;)<br>    }<br>  }<br>...<br>}</pre><pre>//shim-es6-promise.js</pre><pre>module.exports = {<br>  Promise: require(&#39;core-js/es6/promise&#39;)<br>}</pre><p>What you are basically saying over here is, for everywhere, where a <strong><em>require(‘es6-promise’) </em></strong>is encountered, while bundling, use the file specified in the alias to resolve the dependency. This brought the bundle size down further by <strong><em>7kB. </em></strong>Not a lot, but with alias I had discovered a more powerful tool, to tackle some of the problem I could not previously.</p><h3>Getting rid of node core polyfills</h3><p>If you remember I mentioned that I was still seeing polyfills for node core modules <strong><em>http, stream </em></strong>and<strong><em> buffer </em></strong>being bundled on the client side. And adding them to <strong><em>IgnorePlugin </em></strong>was not working.</p><p>After looking further into the dependency tree<strong><em>, </em></strong>I realised<strong><em> </em></strong>we had server specific modules like <a href="https://www.npmjs.com/package/metrics"><strong><em>metric</em></strong></a><strong><em>s </em></strong>and <a href="https://www.npmjs.com/package/bunyan"><strong><em>bunyan</em></strong></a><strong><em> </em></strong>being bundled for the client side, as a result of the app being isomorphic. [Note: <strong><em>bunyan</em></strong> can be used on the client side, but we were not capturing any logs from <strong><em>bunyan</em></strong> on the client side, so was redundant]. As result of pulling these modules, <strong><em>webpack </em></strong>included polyfills for node core modules <strong><em>http, stream </em></strong>and <strong><em>buffer </em></strong>for the client side.</p><p>The approach I took here was to create my own shims for the <strong><em>bunyan </em></strong>and <strong><em>metrics </em></strong>and use <strong><em>webpack </em></strong>alias to resolve any calls to <strong><em>bunyan </em></strong>and <strong><em>metrics.</em></strong></p><pre>//webpack.config.js</pre><pre>{<br>...<br>resolve: {<br>    alias: {<br>      &#39;es6-promise$&#39;: path.resolve(__dirname, &#39;src/shims/shim-es6-promise.js&#39;),<br>      &#39;metrics&#39;: path.resolve(__driname, &#39;src/shims/shim-metrics.js&#39;),<br>      &#39;bunyan&#39;: path.resolve(__dirname, &#39;src/shims/shim-bunyan.js&#39;)<br>    }<br>  }<br>...<br>}</pre><p>What I did with the shims were basically mock the public api’s that were being called on these specific modules, just to ensure that they would not throw. For e.g my shim for <strong><em>metrics </em></strong>looks like:</p><pre>//shim-metrics.js</pre><pre>function Report () {}</pre><pre>Report.prototype.addMetric = function () {}<br>Report.prototype.summary = function () {}</pre><pre>module.exports = {<br>  Report: Report<br>}</pre><p>The <strong><em>bunyan</em></strong> shim was more involved and I ended up mocking most of it’s public api. This probably comes with a maintenance overhead, so probably depends on the teams mileage. But for what it’s worth I managed to shave off <strong><em>80kB </em></strong>off of the client side bundle using this technique. Plus I also came across this <a href="https://github.com/trentm/node-bunyan#webpack"><strong><em>documentation</em></strong></a><strong><em> </em></strong>on <strong><em>bunyan, </em></strong>for it’s usage with <strong><em>webpack, </em></strong>which does what I did with <strong><em>IgnorePlugin.</em></strong></p><h3>Using strip-loader</h3><p>None of the client side logs were being captured in production, so there were was no need of having them in the minified bundle. I used <a href="https://github.com/yahoo/strip-loader"><strong><em>strip-loader</em></strong></a><strong><em> </em></strong>to get rid off all the log statements from the production build, to see how much it would help.</p><pre>$ npm install --save-dev strip-loader</pre><pre>//webpack.config.js<br>{<br>  ...  <br>  module: {<br>        loaders: [<br>            { test: /<strong>\.</strong>js$/, loader: WebpackStrip.loader(&#39;logger.debug&#39;, &#39;logger.info&#39;, &#39;console.log&#39;) }<br>        ]<br>    }<br>...<br>}</pre><p>Doing this stripped off another <strong><em>6kB </em></strong>from the client side bundle.</p><h3>Switching to Preact</h3><p>The last thing I tried was to switch <strong><em>React </em></strong>to <strong><em>Preact. </em></strong>This was pretty easy to do again with <strong><em>webpack </em></strong>alias.</p><pre>$ npm install -S preact preact-compat</pre><pre>//webpack.config.js<br>{<br>...<br>  reslove: {<br>     alias: {<br>        &#39;react&#39;: &#39;preact-compat&#39;,<br>        &#39;react-dom&#39;: &#39;preact-compat&#39;<br>     }   <br>  }<br>...<br>}</pre><p>This shaved off <strong><em>115kB</em> </strong>from the bundle but I refrained from making this change as the underlying <strong><em>flux </em></strong>framework we used, exposed a component that relied on <strong><em>React.createClass </em></strong>which <strong><em>Preact </em></strong>currently does not support. Plus this would also probably have meant a full swoop or round of testing to feel confident with the build. But <strong><em>Preact </em></strong>is definitely an alternative I would consider for it’s size, going forward.</p><h3>Conclusion</h3><p>Overall I managed to shave off <strong><em>359kB </em></strong>from the minified bundle just by manually analysing what was being put in the bundle. If building an isomorphic app there could be chances that server specific modules could be leaking into the client side and hence unneeded polyfills for node core modules could be being bundled for the client side. Plus <strong><em>babel-polyfill </em></strong>seems to bring a lot that you may not need, so pulling in only what is needed seems to help a lot. Analyse and minimise.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41096559beca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/netscape/analysing-and-minimising-the-size-of-client-side-bundle-with-webpack-and-source-map-explorer-41096559beca">Analysing and minimising the size of client side bundle with webpack and source-map-explorer</a> was originally published in <a href="https://medium.com/netscape">Netscape</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>