How to get Webpack to build multiple configuration exports in serial rather than in parallel?

Viewed 400

I am using Webpack's multiple configuration export functionality to build two separate sets of bundles. The second configuration here (labeled serviceWorkerConfig) requires a file emitted from the first configuration. I naively assumed they run in serial because it was an array, but I guess not.

const { merge } = require('webpack-merge');
const bundleProcessor = require("./webpack.bundleProcessor.config.js")
const serviceWorkerConfig = require("./webpack.serviceWorker.config.js")

module.exports = (env, argv) => {
    return [
        merge(bundleProcessor,
            { mode: "development", }
        ),
        merge(serviceWorkerConfig, // requires file emitted from first configuration
            { mode: "development" }
        ),
    ];
};

I tried adding module.exports.parallelism = 1; as in the example given in the docs, but that doesn't appear to work for me.

Is there a way to get Webpack to build multiple configuration exports in serial rather than in parallel?

2 Answers

From my experience, I did the similar thing by running Webpack serially by

  1. similar with your Webpack config, but returns a dict like
{
   a: {configForA}, b: {configForB}
}
  1. call webpack twice like
webpack NODE_ENV=a && webpack NODE_ENV=b

Though it's not some settings from Webpack, it worked. Not sure whether you tried it or if it suits.

parallelism is the number of CPU to use (you'd need a single threaded CPU for this to work). While maxConcurrentWorkers sounds rather promising, but this is only available through the NodeJS API. Beside that webpack-chain seems to be close to what you're asking for... let's face it, one cannot merge a single item (as shown in the example above - for comparison, the docs). And if it has to be merge, this should be a merged configuration, consisting of the configuration for that one file, to be merged with the serviceWorkerConfig, which depends on it.

I think that a plugin which merges configurations might not even fit the job.... but the one module has to depend on the other. When defined as library & externals, this would rather represent the dependency, which apparently exists but never had been declared.

A simple script (as suggested by @Wenbo) might indeed be the least effort... and it could in between also check for / copy over the one file in question.

Related