Angular 2 / Webpack-CLI参数不起作用

时间:2018-10-05 08:05:14

标签: angular webpack

我想从命令行传递我的webpack脚本参数

CLI -> NPM -> package.json -> WEBPACK

我发现的大多数帖子仅涉及在package.json中设置参数

package.json -> WEBPACK

我只是无法使其工作

在我的package.json中,我首先尝试了

"build": "webpack -- --test",
"build": "webpack -- test",
"build": "webpack test",

我最终遇到

之类的错误
ERROR in Entry module not found: Error: Can't resolve 'test'
ERROR in Entry module not found: Error: Can't resolve '--test'
  • 我如何进行这项工作?
  • 运行npm时如何通过命令行传递此测试值?
  • 我应该使用process.env还是argv来获取传递的参数?

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用argv节点包来传递命令行参数-https://www.npmjs.com/package/argv

您可以通过命令行传递--env = test例如,然后可以导出env函数,该函数返回一个而不是直接返回一个对象 https://webpack.js.org/configuration/configuration-types/#exporting-a-function-to-use-env

样本

可通过--env前缀i传递自定义参数。 e。 --env.compress。比从webpack.config.js导出函数,它使用env参数调用。

module.exports = function(env) {
  // ...
  if (env.compress === 'true') {
    var CompressionPlugin = require('compression-webpack-plugin');
    config.plugins.push(
        new CompressionPlugin({
            asset: '{file}',
            algorithm: 'gzip',
            regExp: /\.js$|\.html$/
        }))
  }
}

答案 1 :(得分:0)

答案是

CLI:

npm run build -- --env.something=true

package.json:

"build": "webpack"

webpack.config.js:

module.exports = function(env) {

  if (env.something=== 'true') {
   ...
相关问题