是否有可能通过一个标志Gulp让它以不同的方式运行任务? [英] Is it possible to pass a flag to Gulp to have it run tasks in different ways?
问题描述
gulp.task('my-task',function(){
返回gulp.src(options.SCSS_SOURCE)
.pipe(sass({style:'nested'}))
.pipe(autoprefixer('last 10 version'))
(concat('style.css'))
.pipe(gulp.dest(options.SCSS_DEST));
});
是否有可能将命令行标志传递给gulp(这不是任务)并让它运行有条件的基础上的任务?例如
$ gulp my-task -a 1
然后在我的gulpfile.js中:
gulp.task('如果(a == 1){
var source = options.SCSS_SOURCE;
} else {
var source = options.OTHER_SOURCE;
}
return gulp.src(source)
.pipe(sass({style:'nested'}))
.pipe(autoprefixer('last 10 version'))
.pipe(concat('style.css'))
.pipe(gulp.dest(options.SCSS_DEST));
});
Gulp不提供任何类型的util ,但是您可以使用许多命令参数解析器之一。我喜欢 yargs
。应该是:
var argv = require('yargs')。argv;
gulp.task('my-task',function(){
return gulp.src(argv.a == 1?options.SCSS_SOURCE:options.OTHER_SOURCE)
.pipe({style:'nested'}))
.pipe(autoprefixer('last 10 version'))
.pipe(concat('style.css'))
.pipe(gulp.dest(options.SCSS_DEST));
});
您也可以将它与 gulp-if
有条件地管道流,对开发和生产建设非常有用:
var argv = require('yargs')。argv,
gulpif = require('gulp-if'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify');
gulp.task('my-js-task',function(){
gulp.src('src / ** / *。js')
.pipe concat('out.js'))
.pipe(gulpif(argv.production,uglify()))
.pipe(gulpif(argv.production,rename({suffix:'.min'} )))
.pipe(gulp.dest('dist /'));
});
用调用gulp my-js-task
或 gulp my-js-task --production
。
Normally in Gulp tasks look like this:
gulp.task('my-task', function() {
return gulp.src(options.SCSS_SOURCE)
.pipe(sass({style:'nested'}))
.pipe(autoprefixer('last 10 version'))
.pipe(concat('style.css'))
.pipe(gulp.dest(options.SCSS_DEST));
});
Is it possible to pass a command line flag to gulp (that's not a task) and have it run tasks conditionally based on that? For instance
$ gulp my-task -a 1
And then in my gulpfile.js:
gulp.task('my-task', function() {
if (a == 1) {
var source = options.SCSS_SOURCE;
} else {
var source = options.OTHER_SOURCE;
}
return gulp.src(source)
.pipe(sass({style:'nested'}))
.pipe(autoprefixer('last 10 version'))
.pipe(concat('style.css'))
.pipe(gulp.dest(options.SCSS_DEST));
});
Gulp doesn't offer any kind of util for that, but you can use one of the many command args parsers. I like yargs
. Should be:
var argv = require('yargs').argv;
gulp.task('my-task', function() {
return gulp.src(argv.a == 1 ? options.SCSS_SOURCE : options.OTHER_SOURCE)
.pipe(sass({style:'nested'}))
.pipe(autoprefixer('last 10 version'))
.pipe(concat('style.css'))
.pipe(gulp.dest(options.SCSS_DEST));
});
You can also combine it with gulp-if
to conditionally pipe the stream, very useful for dev vs. prod building:
var argv = require('yargs').argv,
gulpif = require('gulp-if'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify');
gulp.task('my-js-task', function() {
gulp.src('src/**/*.js')
.pipe(concat('out.js'))
.pipe(gulpif(argv.production, uglify()))
.pipe(gulpif(argv.production, rename({suffix: '.min'})))
.pipe(gulp.dest('dist/'));
});
And call with gulp my-js-task
or gulp my-js-task --production
.
这篇关于是否有可能通过一个标志Gulp让它以不同的方式运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!