向 gulp 任务发送参数 [英] Send arguments to gulp task
问题描述
关于这个主题的 stackoverflow 上还有其他问题,但不是我希望的问题.
There are other questions on stackoverflow about this topic, but not the one I was hoping for.
这是我的 gulpfile.js 的简化版本
Here is a simplified version of my gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('css', function() {
gulp.src('site/scss/style.scss')
.pipe(sass())
.pipe(gulp.dest('assets/css'))
});
gulp.task('watch', function() {
gulp.watch('site/scss/**/*.scss', ['css']);
});
我可以成功做到这一点:
gulp watch
现在我希望能够在我的终端命令行中执行这样的操作:
gulp watch my-domain-name
或
gulp watch my-other-domain
我的 gulpfile 在我看来应该是这样的:
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('css', function(name) {
gulp.src('site/scss/style.scss')
.pipe(sass())
.pipe(gulp.dest('assets/css'))
});
gulp.task('watch', function(name) {
gulp.watch('site/scss/**/*.scss', ['css', name]);
});
我尝试将 name
作为我的监视任务的变量发送.就我而言,它是 my-domain-name
或 my-other-domain
取决于我在终端中写的内容.
I try to send name
around as a variable of my watch task. In my case it would be my-domain-name
or my-other-domain
depending on what I write in my terminal.
如何将参数从终端发送到 watch 任务,然后再发送到 css 任务?
How can I send a parameter from the terminal to the watch task and then over to the css task?
推荐答案
从命令行向 gulp 传递参数的来源有很多.见:
There are a number of sources for passing parameters from the command line to gulp. See:
https://www.sitepoint.com/pass-parameters-gulp-tasks/
https://github.com/gulpjs/gulp/blob/master/docs/recipes/pass-arguments-from-cli.md
官方配方使用minimist 一个非常受欢迎的包.
The official recipe uses minimist an extremely popular package.
因此,使用这些方法,您不会将命令行参数直接传递给 watch 任务,而是传递给任何任务都可以访问的对象.
So with these approaches you would not be passing the command line args directly to the watch task, but rather to an object that can be accessed by any task.
它看起来像 gulp-param 可以做你想做的事,直接注入命令行参数到每个任务的回调函数中.
And it look like gulp-param may do exactly what you want, directly injecting command line args into each task's callback function.
这篇关于向 gulp 任务发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!