将参数传递给组合的npm脚本 [英] Passing arguments to combined npm script
问题描述
我的package.json中包含以下内容
I have in my package.json the following
"scripts": {
...
"prod": "gulp build --production && webpack --env.config=production"
}
我现在想同时向gulp和webpack传递参数"theme",以便能够从命令行调整构建过程的输出.
I now want to pass a parameter "theme" to both gulp and webpack to be able to adjust the output of the build process from the command line.
我想出了如何将其传递到webpack:npm run prod -- --env.theme=themename
,但是gulp并没有解决这个问题.我还通过将npm脚本更改为"gulp build --production \"$1\" && webpack --env.config=production"
来使用yargs
-package,processs.argv
和bash字符串替换,但是这也没有解决.
I figured out how to pass it to webpack: npm run prod -- --env.theme=themename
but gulp does not take care of this. I also played around with the yargs
-package, processs.argv
and bash string substitution by changing the npm script to "gulp build --production \"$1\" && webpack --env.config=production"
but that did not work out either.
如何实现?我想念什么?任何提示高度赞赏!
How can this be achieved? What am I missing? Any hints highly appreciated!
推荐答案
If you're using Bash you can use a function in your npm-script.
例如:
"scripts": {
...
"prod": "func() { gulp build --production \"$1\" && webpack --env.config=production \"$1\"; }; func"
}
但是,对于跨平台解决方案,您需要考虑调用执行命令的nodejs脚本-以类似于答案
However, for a cross-platform solution you'll need to consider invoking a nodejs script which exec's the commands - in a similar way shown in Solution 2 of my answer here.
这篇关于将参数传递给组合的npm脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!