将参数传递给组合的npm脚本 [英] Passing arguments to combined npm script

查看:80
本文介绍了将参数传递给组合的npm脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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!

推荐答案

如果您使用的是Bash,则可以使用

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脚本-以类似于答案中所示的方式显示://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json#answer-51401577>此处.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆