Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create Separate webpack Configs for Development and Production with webpack-merge

    Andy Van SlaarsAndy Van Slaars
    webpackwebpack

    The development and production modes in webpack optimize the output in different ways. In development mode, the focus is on faster builds and a better developer experience. In production mode, the focus is on highly optimized bundles, leading to a better user experience. The good news is, we can have both. In this lesson, we'll separate our webpack config into two configurations and update our scripts to run the right config for our needs.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Right now, we have two scripts in our package.json that both run webpack. One uses the configuration as-is, and the other one starts to override settings, where here, we're taking the default mode from the config, and we're overriding it with development.

    As our webpack config gets more and more complicated, we have more chances that we're going to want to override things for our development build versus a production build, and vice-versa. It would be nice to maintain two separate configs without duplicating any of the settings that are shared between the two.

    We're going to start by installing webpack-merge. We'll npm i -d webpack-merge. From here, we're going to go in, and we're going to rename our webpack config file. This is going to be webpack.config.base.js.

    Then I'm going to add a new file. This'll be webpack.config.dev.js. Then I'm going to create one more. That'll be webpack.config.prod.js. Now, we have a base config, which is basically everything that we have here.

    We have our entry output module, our Babel loader rules, and our HTML webpack plugin. Then we have two empty files that are going to act as our specific dev and production configs. I'm going to take the mode setting out of our base config, and then I'm going to close this file.

    Then in my dev config, and here, I'm going to define a const called merge. Here, I'm going to require the package that we just installed, webpack-merge. Then I'm going to define another constant we'll call baseConfig.

    That's going to be a require for our webpack.config.base. Then we need export a webpack configuration. We'll do a module.export, and we're going to call our merge function that we just imported.

    The first argument is going to be our base config. Then we want to merge that with an object that represents our dev-specific overrides. For now, the only setting we would need to override for a particular environment is mode.

    We'll pass in mode. We'll make the value development. Then we can save that. Now, I'm going to select all of this and copy it. Then in my webpack.config.prod.js, I'll just paste that all in, and then update mode to be production.

    Now that we have these specific config files, we need to update our package.json to use them. In package.json, in build, I'm going to add a flag to webpack called config. That's going to point to our production config file.

    webpack.config.prod.js, and then down here in the dev script, where I'm overriding mode, I no longer need to do that. I do need to specify the config. Again, I'll add the config flag, webpack.config.dev.js. I'll save that.

    Then in my terminal, if I do an npm run build, and then I look at the output from that, we'll see that everything's been minified. Now, if I go back into the terminal and npm run dev, it's going to run in watch mode because of this flag.

    I can still look at the output. We'll see that it's not minified, so I know it's using that development config file. Then I can hit Control+C to get out of watch mode. Now, our webpack configuration is spread across three different files.

    We have one place where we can put all of our shared settings, regardless of whether we're building for development or production. Then we have a separate config file, where we can put dev-only settings. Then we have the same thing for production.