<?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 Daniel Kopka on Medium]]></title>
        <description><![CDATA[Stories by Daniel Kopka on Medium]]></description>
        <link>https://medium.com/@daniel.kopka?source=rss-ee60d68de3e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*RDJ5sP3gN0OcfzI1WC5Fiw.jpeg</url>
            <title>Stories by Daniel Kopka on Medium</title>
            <link>https://medium.com/@daniel.kopka?source=rss-ee60d68de3e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:18:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel.kopka/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[How I deal with Webpack configuration]]></title>
            <link>https://medium.com/@daniel.kopka/how-i-deal-with-webpack-configuration-d61f82e4367d?source=rss-ee60d68de3e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d61f82e4367d</guid>
            <category><![CDATA[webpack-2]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Daniel Kopka]]></dc:creator>
            <pubDate>Thu, 06 Jul 2017 18:19:13 GMT</pubDate>
            <atom:updated>2017-07-06T18:19:13.745Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gdoQ1_5OID90wf1eLTFvWw.png" /></figure><p>There are a lot of examples for configuring <a href="https://webpack.js.org/">Webpack</a>, but most of them work for small projects and don’t scale quite well in case of a fully automated pipeline which includes CI and CD.</p><p>I’m a huge fan of <a href="https://www.python.org/">Python</a> and <a href="https://www.djangoproject.com/">Django</a>, and this is my small attempt to show you how to organise DRY configuration files which will cover the whole development cycle.</p><h3>The Development Cycle</h3><p>At the beginning of the project in particular scale, it’s enough to include local development and the build. After few iterations you will end up with:</p><ul><li>Local development</li><li>Testing</li><li>Staging</li><li>Production</li></ul><h3>The Problem</h3><p>Webpack documentations says that we should create a webpack.config.js file but how to squeeze at least <strong>four different </strong>configurations in it? Let’s try with one file.</p><p>For such purpose, you can use Webpack env <a href="https://webpack.js.org/configuration/configuration-types/">feature</a> which allows us to pass custom CLI attributes which will be available in the configuration file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08e79caa3fc81841aca1e608711155a6/href">https://medium.com/media/08e79caa3fc81841aca1e608711155a6/href</a></iframe><p>You can change environment via different CLI attribute:</p><pre>$: webpack --env.production<br>$: webpack --env.development</pre><p>This solution looks nice, right? Using ES6, arrow functions and spread operator all new fancy and modern features are here, so what’s wrong? <strong>EVERYTHING!</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*2HuU6yIlUaxvV8hgsNDVcQ.gif" /></figure><p>As I enjoy using <em>spread operator</em>, it makes the final result of our configuration <em>unreadable</em> and the whole <em>boolean logic</em> around it to make it work doesn’t help much. If you do a step back for a bit and think about at least <strong>four</strong> configurations to cover, you will quickly end up with an unreadable and unmaintainable file.</p><h3>The Clash Of Worlds</h3><p>At this point, Python and Django come handy. Django has a superb pattern regarding configuration management which is based on environmental variable and dynamic modules loading.</p><p>By default, Django is looking for DJANGO_SETTINGS_MODULE env variable which holds dot path to the configuration file, e.g., DJANGO_SETTINGS_MODULE=”settings.development” which means go and load ./settings/development.py file. Cool right? So how can we adapt this in JS and Webpack world?</p><p>Quite simple, everyone knows NODE_ENV so let’s use it as an equivalent to DJANGO_SETTINGS_MODULE and the NodeJS require will do the rest.</p><h3>The Files Structure</h3><p>We need to cover few configurations, so it makes sense to split them into separate files and keep the common code as the standalone file. Let’s start flat:</p><pre>├── webpack.config.common.js <br>├── webpack.config.development.js<br>├── webpack.config.production.js<br>├── webpack.config.staging.js<br>└── webpack.config.test.js</pre><p>It looks crappy, so let’s fix it quick:</p><pre>└── webpack.config<br>    ├── common.js<br>    ├── development.js<br>    ├── index.js<br>    ├── production.js<br>    ├── staging.js   <br>    └── test.js</pre><p>Better right? Let’s see what’s inside and how it works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be9acedb9deb28d5c4785ac520a63b16/href">https://medium.com/media/be9acedb9deb28d5c4785ac520a63b16/href</a></iframe><p>What exactly happened? We have divided <em>webpack.config.js</em> into four different files which are combined by <a href="https://www.npmjs.com/package/webpack-merge">webpack-merge</a> package:</p><ul><li><strong>common.js</strong> contains all basic configuration which is shared with other environments, things like entry points, common loaders, and plugins</li><li><strong>development.js</strong> imports the common file and includes all settings related to local environment which should hold dev server and hot reload</li><li><strong>production.js</strong> also imports common file and contains all parameters related to final bundle like minification and platform/framework specific optimizations</li><li><strong>test.js/staging.js </strong>you can use the same approach here or import any settings file which works best for you</li></ul><p>Now you have a clean picture of each environment which will scale in case of new configurations, and it’s DRY. Success!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*uGJG9zGLl_uZNhFUX9bfDA.gif" /></figure><p>But wait for one sec! How exactly can you use it?</p><h3>Usage Of NODE_ENV</h3><p>Let’s get back to the NODE_ENV and how you can use it to import proper settings. The first solution will rely on bash and classic string interpolation:</p><pre>$: webpack --config ./webpack.config/$NODE_ENV.js</pre><p>We can do better and make sure that we have default value for NODE_ENV:</p><pre>$: NODE_ENV=${NODE_ENV:=development} webpack --config ./webpack.config/$NODE_ENV.js</pre><p>I would recommend to put this in scripts section of your package.json file more or less like this:</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;webpack-dev-server --config ./webpack.config/$NODE_ENV.js&quot;,<br>    &quot;build&quot;: &quot;webpack --config ./webpack.config/$NODE_ENV.js&quot;,<br>  }<br>  ...<br>}</pre><pre>// or to be super safe</pre><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;NODE_ENV=${NODE_ENV:=development} webpack-dev-server --config ./webpack.config/$NODE_ENV.js&quot;,<br>    &quot;build&quot;: &quot;NODE_ENV=${NODE_ENV:=development} webpack --config ./webpack.config/$NODE_ENV.js&quot;,<br>  }<br>  ...<br>}</pre><p>Then you can use it via NPM:</p><pre>$: npm run start<br>$: npm run build</pre><h3>The Last Quick Tip</h3><p>In case the ./webpack.config/$NODE_ENV.js pattern with bash based logic looks bad to you, make use of the index.js file which can cover some logic:</p><pre>require(&#39;babel-register&#39;);</pre><pre><em>const</em> ENV = process.env.NODE_ENV || &#39;development&#39;;</pre><pre><em>const</em> CONFIG = require(`./${ENV}`);</pre><pre><em>module</em>.<em>exports</em> = CONFIG;</pre><p>In this case you inject <strong>babel-register (</strong>which will enable ES6 in all configuration files, so you can skip bash part) replace scripts with simplified syntax:</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;webpack-dev-server --config ./webpack.config/index.js&quot;,<br>    &quot;build&quot;: &quot;webpack --config ./webpack.config/index.js&quot;,<br>  }<br>  ...<br>}</pre><h3>Wrapping It Up</h3><p>I’ve used this approach with success in different environments with CI/CD and Docker (and not only for Webpack). This method works well for application settings and other third party libraries.</p><p>Source code from this tutorial is available on GitHub:</p><p><a href="https://github.com/dancio/webpack-configuration-management">dancio/webpack-configuration-management</a></p><p>I hope you enjoyed my take on Webpack configuration. How do you tackle such issues? Anything you would change or add? Let me know your thoughts in the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d61f82e4367d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>