当webpack.config文件正在导出多个配置时,请选择一个webpack配置



像这样的webpack.config.js文件可以导出多个配置:

module.exports = [{entry: 'a.js'}, {entry: 'b.js'}];

调用webpack时,如何从CLI中选择其中一个配置?

在Webpack 3.2中,您可以提供带有--config-name标志的配置名称。您的配置应该具有name属性。

当前无法从CLI中选择阵列中的一个配置。最好的办法是创建两个额外的配置文件(每个文件都引用主文件中两个配置中的一个),并将webpack指向该配置。

示例:

webpack.a.config.js

var allConfig = require('./webpack.config.js');
module.exports = allConfig[0];

然后用--config webpack.a.config.js调用Webpack。

试试这个:

// webpack.config.js
export default [
  webpackConfig1,
  webpackConfig2,
]
function webpackConfig1(){
  return {
    name: "main",
    entry: ["main.js"],
  }
}

function webpackConfig2(){
  return {
    name: "other",
    entry: ["other.js"],
  }
}

用法:

webpack // compiles both configs
webpack --config-name main // only with name=main
webpack --config-name other // only with name=other

最新更新