Webpack从CLI

时间:2017-03-30 10:04:19

标签: node.js webpack

我想从CLI中指出我的条目名称,以便编译许多Sass主题。

我的package.json:

./node_modules/.bin/webpack 
  --config ./app/Resources/build/webpack.dev.js 
  --progress 
  --colors 
   --watch THEME_NAME

我在 webpack.config.js 中恢复了THEME_NAME:

var processArguments = process.argv;
var theme = processArguments[7]; // Return theme name

并将其添加到我的 entryApp

entryApp: {
  app: './app/Resources/private/js/app.js',
  theme: './app/Resources/private/scss/themes/' + theme + '/' + theme + '.scss'
},

但我有一个错误:

  

未找到Entry模块中的错误

我绝对肯定路径很好:(

任何人都可以帮助我?

谢谢!

1 个答案:

答案 0 :(得分:1)

Webpack CLI的任何参数都被解释为入口点(CLI Usage)。

致电时:

webpack [options] myTheme

它试图找到模块myTheme,就像你已经将它指定为配置中的入口点一样。

你需要使用另一种方法。一种可能性是使用环境变量。

THEME=myTheme webpack [options]

然后,您可以在配置中使用process.env.THEME访问它。

或者,您可以将函数导出为webpack配置,该配置返回配置对象。 webpack CLI将使用--env CLI选项指定的任何选项传递给该函数。请参阅Exporting a function to use --env

使用隐式返回配置对象的箭头函数,它将如下所示:

module.exports = env => ({
  entry: {
    entryApp: {
      app: './app/Resources/private/js/app.js',
      theme: './app/Resources/private/scss/themes/' + env.theme + '/' + env.theme + '.scss'
    },
  }
  // Rest of your config
});

你可以按如下方式调用webpack:

webpack --env.theme=myTheme [other options]