将参数传递给 Gulp 任务

编程入门 行业动态 更新时间:2024-10-27 04:31:27
本文介绍了将参数传递给 Gulp 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

通常我们可以通过类似 gulp mytask 的方式从控制台运行 gulp 任务.无论如何,我可以将参数传递给 gulp 任务吗?如果可能,请举例说明如何做到这一点.

Normally we can run gulp task from console via something like gulp mytask. Is there anyway that I can pass in parameter to gulp task? If possible, please show example how it can be done.

推荐答案

这是一个程序不能没有的功能.你可以试试yargs.

It's a feature programs cannot stay without. You can try yargs.

npm install --save-dev yargs

你可以这样使用它:

gulp mytask --production --test 1234

在代码中,例如:

var argv = require('yargs').argv; var isProduction = (argv.production === undefined) ? false : true;

为了您的理解:

> gulp watch console.log(argv.production === undefined); <-- true console.log(argv.test === undefined); <-- true > gulp watch --production console.log(argv.production === undefined); <-- false console.log(argv.production); <-- true console.log(argv.test === undefined); <-- true console.log(argv.test); <-- undefined > gulp watch --production --test 1234 console.log(argv.production === undefined); <-- false console.log(argv.production); <-- true console.log(argv.test === undefined); <-- false console.log(argv.test); <-- 1234

希望你能从这里拿走它.

Hope you can take it from here.

你可以使用另一个插件,minimist.还有另一篇文章,其中有 yargs 和 minimist 的好例子:(是否可以将标志传递给 Gulp 让它以不同的方式运行任务?)

There's another plugin that you can use, minimist. There's another post where there's good examples for both yargs and minimist: (Is it possible to pass a flag to Gulp to have it run tasks in different ways?)

更多推荐

将参数传递给 Gulp 任务

本文发布于:2023-11-06 07:42:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   Gulp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!