How to read mode in webpack.config.js
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
Search for a command to run...
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
No comments yet. Be the first to comment.
I’ve been working in programming for the last 16 years. Let’s see what changes I see in our industry that are enabled by generative AI. Rapid prototyping The last few months, I’ve been working on a set of WordPress plugins, related to event organizat...
Writing unit tests takes time and effort. Nonetheless, many teams insist on writing them anyway—that’s because of the benefits they bring to a project. Those benefits are mainly the following: fast feedback—unit tests speed up each iteration of twea...

Pure functions are the perfect case for unit testing. For a given input, we always expect the same output—there is no internal state involved. Let’s take a look at a few examples and some simple tests that check if the methods work as expected. Jasmi...

Let’s say you have a job interview in a few days. How should you prepare for it so that you can make an informed decision about joining the company, as well as make sure that your interests are taken care of? Prepare your questions An interview is a ...

Creating example projects is a common way of showing your skills to potential employers. Let’s take a look at what’s important to keep in mind when building personal projects with an eye toward impressing prospective employers. Simplicity Building ap...

If you want to cover all build use cases with one webpack.config, at some point, you will want to start tweaking the configuration based on the use case. This short guide will show you how to do it with --mode=production as an example.
I start the example with code generated by my degit generator. It comes with no webpack config - it has everything set up to work with the default values.
First, I want to support two types of build
--mode=none, for testing locally unobfuscated code--mode=production, meant for deployingIn the real-world project, you can find yourself with similar requirements - dedicated build for unit or e2e tests, etc.
The first step is to define 2nd build script in package.json:
{
...
"scripts": {
...
"build": "webpack --mode=none",
"build:production": "webpack --mode=production"
},
}
One of the reasons we would like to differentiate the build is the source map setting controlled by the devtool flag on the configuration object. There are more than 25 possible values described in its documentation, and some are fast & optimized for speed in the development workflow, while others are slower but a better fit for deploying on the production.
Webpack works with many approaches to the configuration file. One of the allowed forms is configuration function. This approach is a bit more complicated than configuration objects, but conveniently it will enable the behavior we want to have here.
webpack.config.js:
module.exports = function (env, argv) {
return {
devtool: argv.mode === "production" ? "source-map" : "eval",
};
};
argv is provided with all the values that we have in the webpack callargv.mode is equal "production" when we run npm run build:production" and"none"for the defaultnpm run build"source-map" creates a slow but high-quality source map recommended the production use"eval" is quick but meant for development onlySo in the end, we have:
$ npm run build
> webpack-starter@1.0.0 build /home/marcin/workspace/github/webpack-read-mode
> webpack --mode=none
asset main.js 1.1 KiB [compared for emit] (name: main)
./src/index.js 23 bytes [built] [code generated]
webpack 5.59.1 compiled successfully in 66 ms
$ ls dist
main.js
big, one file for the local build;
$ npm run build:production
> webpack-starter@1.0.0 build:production /home/marcin/workspace/github/webpack-read-mode
> webpack --mode=production
asset main.js 55 bytes [emitted] [minimized] (name: main) 1 related asset
./src/index.js 23 bytes [built] [code generated]
webpack 5.59.1 compiled successfully in 170 ms
$ ls dist
main.js main.js.map
And smaller main.js & an additional file with a source map for the production build.
I hope you find this guide useful for the projects you are working on. If you are interested in the other strategies for managing different build use-case side-to-side, let me know in the comments.