Configuration Types

Besides exporting a single configuration object, there are a few more ways that cover other needs as well.

Exporting a Function

Eventually you will find the need to disambiguate in your webpack.config.js between development and production builds. There are multiple ways to do that. One option is to export a function from your webpack configuration instead of exporting an object. The function will be invoked with two arguments:

-export default {
+export default function(env, argv) {
+  return {
+    mode: env.production ? 'production' : 'development',
+    devtool: env.production ? 'source-map' : 'eval',
     plugins: [
       new MinimizerPlugin({
         minimizerOptions: {
+          compress: argv.mode === 'production' // only if `--mode production` was passed
         }
       })
     ]
+  };
};

Exporting a Promise

Webpack will run the function exported by the configuration file and wait for a Promise to be returned. Handy when you need to asynchronously load configuration variables.

export default () =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        entry: "./app.js",
        /* ... */
      });
    }, 5000);
  });

Exporting multiple configurations

Instead of exporting a single configuration object/function, you may export multiple configurations (multiple functions are supported since webpack 3.1.0). When running webpack, all configurations are built. For instance, this is useful for bundling a library for multiple targets such as AMD and CommonJS:

export default [
  {
    output: {
      filename: "./dist-amd.js",
      libraryTarget: "amd",
    },
    name: "amd",
    entry: "./app.js",
    mode: "production",
  },
  {
    output: {
      filename: "./dist-commonjs.js",
      libraryTarget: "commonjs",
    },
    name: "commonjs",
    entry: "./app.js",
    mode: "production",
  },
];

dependencies

In case you have a configuration that depends on the output of another configuration, you can specify a list of dependencies using the dependencies array.

webpack.config.js

export default [
  {
    name: "client",
    target: "web",
    // …
  },
  {
    name: "server",
    target: "node",
    dependencies: ["client"],
  },
];

parallelism

In case you export multiple configurations, you can use the parallelism option on the configuration array to specify the maximum number of compilers that will compile in parallel.

  • Type: number
  • Available: 5.22.0+

webpack.config.js

const config = [
  {
    // config-1
  },
  {
    // config-2
  },
];

config.parallelism = 1;

export default config;

plugins

5.112.0+

Plugins set on the configuration array itself are applied once to the MultiCompiler that runs every configuration, instead of to each child compiler. Use it for work that concerns the whole set, such as reporting which builds changed or uploading their output once all of them finish:

  • Type: (WebpackPluginInstance | ((compiler: MultiCompiler) => void))[]

webpack.config.js

const config = [
  { name: "client", entry: "./src/client.js" },
  { name: "server", entry: "./src/server.js", target: "node" },
];

config.plugins = [
  {
    apply(multiCompiler) {
      multiCompiler.hooks.done.tap("Report", (multiStats, changedCompilers) => {
        console.log(
          `rebuilt: ${changedCompilers.map((c) => c.name).join(", ")}`,
        );
      });
    },
  },
];

export default config;

See the MultiCompiler hooks for what such a plugin can tap.

Edit this page·

11 Contributors

sokraskipjackkbariotissimon04fadysamirsadekbyzykEugeneHlushkodhurlburtusaanshumanvthorn0tusharthakur04