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

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

问题描述

我的 package.json 中有以下内容

I have in my package.json the following

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

我现在想将参数theme"传递给 gulp 和 webpack,以便能够从命令行调整构建过程的输出.

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 并没有处理这个问题.我还尝试了 yargs-package、processs.argv 和 bash 字符串替换,方法是将 npm 脚本更改为 "gulp build --production "$1" && webpack --env.config=production" 但这也没有成功.

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,你可以使用 function 在你的 npm 脚本中.

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

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