webpack: Webpack 2.0 doesn't support custom command line arguments?
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$/
}))
}
However, in Webpack 2.0, this approach doesn’t work any more. When I type in webpack --config ./webpack.config.prod.js --compress true
. The webpack will just throw an error:
Unknown argument: compress
And then stops… Would there be a way in Webpack 2.0 to support custom command line arguments?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 16
- Comments: 25 (5 by maintainers)
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 theenv
parameter.I still think the advantage of allowing custom args is bigger than the advantage of typo checking
👎
If anyone else comes here searching for the answer, I solved it as follows:
see https://github.com/webpack/webpack/issues/2254#issuecomment-219219418
You can still pass arguments. Just prefix
--env.
: i. e.--env.magic=5
.@Pomax – you just have to return the config
> webpack --env.prod
This isn’t working for me.
Am I doing something wrong?
@SimenB Look like in your case a custom CLI tool for the build is more suitable. You basically build one. You can just move the CLI stuff from the
webpack.config.js
into abuild.js
and start webpack from this file (by using the node.js API).There were a couple of reasons why I decided to disallow custom arguments:
I just rewrote the build at work, and added yargs into webpack.config.js, and this works great (still using webpack 1). Especially demanding args is nice, but also printing usage message etc…
We have a quite complex build capable of building 5 different webapps, proxying different backends, and different versions of production to build for, and being able to use yargs at the top level for this is perfect, as we also use
webpack-config
.I know I can get (mostly) the same out of using the env-flag, but some things are missing, such as demanding, defaults (which is really nice), choices (even nicer) and usage printing. So it’s more limiting; I have to manually check that usage is correct, I have to manually set defaults, check if value is valid, that it’s passed a string or boolean, etc. All of these things I got declaratively from using yargs in webpack 1, while in webpack 2 I have to do it manually.
I understand that in most cases, using
env
is enough, but in our case, it’s actually not, without lot’s of boilerplate code added. Just removingstrict
, like in my PR, would solve this. And maybe try to flag somewhere very visible thatenv
is available. (I read it in your release notes, so I knew about it, but I can’t see it in the README, for instance)i believe your syntax should be --env.development
but really, whats better is doing the following: NODE_ENV=development; webpack …
we have the following npm run script in package.json using cross-env so that it also works on windows.
"production:build": "cross-env NODE_ENV=production webpack -p --progress --colors"
then in the code:
and use APP_ENV everywhere else
full example: https://github.com/SamanthaAdrichem/webpack-3.9.1-splitted-config-angular