简体   繁体   English

将参数传递给组合的 npm 脚本

[英]Passing arguments to combined npm script

I have in my package.json the following我的 package.json 中有以下内容

"scripts": {
    ...
    "prod": "gulp build --production && webpack --env.config=production"
}

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.我现在想将参数“主题”传递给 gulp 和 webpack,以便能够从命令行调整构建过程的输出。

I figured out how to pass it to webpack: npm run prod -- --env.theme=themename but gulp does not take care of this.我想出了如何将它传递给 webpack: npm run prod -- --env.theme=themenamenpm run prod -- --env.theme=themename不处理这个。 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.我也发挥与周围的yargs -package, processs.argv和bash字符串替换通过改变故宫脚本"gulp build --production \\"$1\\" && webpack --env.config=production" ,但没有工作了任何一个。

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.如果您使用 Bash,则可以在 npm 脚本中使用一个函数

For instance:例如:

"scripts": {
    ...
    "prod": "func() { gulp build --production \"$1\" && webpack --env.config=production \"$1\"; }; func"
}

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 .但是,对于跨平台解决方案,您需要考虑调用执行命令的 nodejs 脚本 - 与我在此处回答的解决方案 2中所示的方式类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM