webpack development mode and hot module replacement

Compare development and production modes, configure hot module replacement with webpack-dev-server, and debug efficiently with source maps.

🎯 Introduction

webpack's development mode and hot module replacement can make frontend development much more efficient. This article explains how development and production modes differ, how to configure hot module replacement (HMR) with webpack-dev-server, and how source maps help you debug code and locate problems. You will learn to set up an effective local development environment.


🔥 Development and production modes (mode)

The mode option determines how webpack processes your code. The two main modes are:

  • development for development
  • production for production

Development mode (development)

Development mode prioritizes build speed and debugging. It provides:

  • Faster compilation
  • Unminified output for easier debugging
  • Plugins and configuration that support debugging, with source map behavior customizable through configuration

For example, webpack can rebuild quickly as you work so you can see your changes promptly.

Production mode (production)

Production mode optimizes the final output to make it smaller and faster:

  • Enables code minification, with CSS minification configured as needed
  • Removes unused code through tree shaking
  • Enables optimization plugins
  • Leaves source maps disabled by default to avoid performance and source exposure concerns

Development mode prioritizes the development experience, while production mode focuses on output quality and performance.

Use development for local development and debugging, and production for deployment builds.

🚀 Set up a local server with webpack-dev-server

webpack-dev-server is webpack's development server. It lets you preview your project locally and supports hot module replacement.

Installation

Install the dependency first:

npm install webpack-dev-server --save-dev

Create index.html

Directory structure

webpack-project/
├── public/
│   └── index.html

For development, create index.html in the public directory for static assets.

index.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>My first webpack project</title>
    </head>
    <body>
        <h1>Welcome to webpack!</h1>
        <script src="bundle.js"></script>
    </body>
</html>

Make sure the script's src is bundle.js, even though that file is not present in the public directory.

Basic configuration

Configure webpack.config.js:

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: __dirname + '/dist',
    },
    devServer: {
        port: 3000,
        open: true, // Open the browser automatically
        hot: true, // Enable hot module replacement
        liveReload: false, // Disable full-page reloads triggered by file changes
    },
};

Start the server:

Add the following to scripts in package.json.

package.json
"scripts": {
    "dev": "webpack-dev-server"
},

Run this command in the terminal:

npm run dev

Once the server starts, visit http://localhost:3000 to see your project. Saving code changes triggers updates in the browser.

publicPath specifies the base path for served assets. Make sure it matches your asset URLs.

⚡ Configure HMR (hot module replacement)

HMR replaces, adds, or removes modules without reloading the whole page. This can preserve application state and improve the development experience.

Enable HMR

  1. Set hot: true in devServer, as shown above.
  2. Set liveReload: false in devServer to disable full-page reloads on file changes, which can interfere with the state-preserving updates you want from HMR.

Hot updates in React, Vue, and other frameworks

Most frameworks provide their own HMR integrations that work with webpack to update individual components, reducing the amount of code you need to write manually.

If HMR causes unexpected state or page behavior, reload the page manually to restore a consistent state.

🐞 Debug with source maps

Debugging is an essential part of development. Source maps let browser developer tools show your original source code instead of the bundled or transformed output.

Configure source maps

webpack supports several source map options. For development, eval-source-map offers a useful balance of speed and debugging detail.

module.exports = {
    mode: 'development',
    devtool: 'eval-source-map', // Enable source maps
    // Other configuration...
};

Source map options

  • eval: fastest, with limited debugging information
  • eval-source-map: fast, with full source maps
  • source-map: generates a separate file; slower to generate, but useful when retaining production debugging information
Avoid exposing full source maps in production unless you need them, because they can reveal your source code.

❓ Questions and answers

Q1: How do webpack's modes differ?

development prioritizes build speed and debugging, producing unminified output. production prioritizes minification and performance, producing smaller bundles for deployment.

Q2: How does webpack-dev-server enable hot updates?

Enable hot: true and use the appropriate HMR integration to replace modules dynamically without reloading the whole page.


🧪 Exercises

  1. Install and run webpack-dev-server. Toggle hot on and off, edit a file, and compare the behavior. Starter code:
module.exports = {
    devServer: {
        hot: true,
    },
};

🎉 You can now use webpack's development mode, configure webpack-dev-server and HMR, and debug with source maps!