https://github.com/webpack/webpack/issues/2254

Yes this is intended. Custom argumens can be passed via --env prefix, i. e. --env.compress. Than export a function from the webpack.config.js and it's called with the env parameter.

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$/
        }))
  }
}

通过 argv 访问

In Webpack 1.x, I can pass in my own command line arguments like this:

webpack --config ./webpack.config.prod.js --compress true

Here --compress is the custom command line arguments, it can be used like this in the webpack.config.js:

var argv = require('yargs').argv;

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

相关文章:

  • 2021-08-07
  • 2022-12-23
  • 2021-12-03
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2021-06-15
猜你喜欢
  • 2021-11-09
  • 2021-06-24
  • 2021-06-08
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案