I have two versions of my application, for one I set --extended, and for the other not, like this
"scripts": {
"build": "webpack --mode production",
"extended": "webpack --mode production --extended",
// ...
}
Now, in my webpack, I access the extended like this
module.exports = (_env,argv)=> {
argv.extended
}
I am trying to improve this in a cross platform way to do something like
"scripts": {
"build": "webpack --mode production",
"extended": "--extended npm run build"
}
as to run the build
script from extended
but still access the --extended
variable.
I there a way to achieve this? Thank you
I read this whole question How to set environment variables from within package.json but can't find a way
Change the scripts
section of your package.json to the following:
"scripts": {
"build": "webpack --mode production",
"extended": "npm run build -- --extended"
}
Explanation:
As stated in the npm-run-script
documentation which can be found here:
... The special option
--
is used bygetopt
to delimit the end of the options. npm will pass all the arguments after the--
directly to your script:
So, essentially when you run the following command via your CLI:
$ npm run extended
npm invokes the extended
script, which then runs the build
script and passes the --extended
argument to the end of it (i.e. it passes --extended
to the end of the build
script).
Yes, you could also consider simplifying the scripts
section of your package.json further by deleting your extended
script completely.
For instance:
"scripts": {
"build": "webpack --mode production"
}
Then you can do either of the following:
Run the following command via your CLI:
$ npm run build
This will to invoke your build
script without the --extended
argument/option.
Or run the following command via your CLI instead:
$ npm run build -- --extended
This will invoke your build
script with the --extended
argument/option.