如何使用参数创建 gulp 任务?
How to make a gulp task with param?
有一个gulp任务:
gulp.task('copy-bower-components-to-dev', function () {
gulp.src('./app/bower_components/**')
.pipe(gulp.dest('dev/bower_components'));
});
我也需要对另一个文件夹执行相同的任务:
gulp.task('copy-bower-components-to-prod', function () {
gulp.src('./app/bower_components/**')
.pipe(gulp.dest('prod/bower_components'));
});
但这不是一个干净的代码;如何使用参数(在本例中是带有文件夹名称的参数)创建 gulp 任务?提前致谢!
gulp-utils
实际上非常适合这个:
var gutil = require('gulp-util');
function destination() {
return (!!gutil.env.type && gutil.env.type == 'prod' ? 'prod' : 'dev';
}
gulp.task('copy-bower-components', function () {
return gulp.src('./app/bower_components/**')
.pipe(gulp.dest(destination() + '/bower_components'));
});
运行它用gulp --type=prod
设置新文件夹!
您可以尝试使用此插件 gulp-foal 来 运行 带有参数的小马任务。
参数不是来自 cmd。
'use strict';
var gulp = require('gulp');
var clean = require('gulp-clean');
var foal = require('gulp-foal');
//use "foal.task(...)" to define a foal-task.
foal.task('clean_some', function(cleanPath) {
//"return" shall not be missed for running foal-task orderly.
return gulp.src(cleanPath)
.pipe(clean());
});
gulp.task('default', function(cb) {
//use "foal.run(...)" to run your foal-task with param.
foal.run(clean_some('test_path'), cb);
});
有一个gulp任务:
gulp.task('copy-bower-components-to-dev', function () {
gulp.src('./app/bower_components/**')
.pipe(gulp.dest('dev/bower_components'));
});
我也需要对另一个文件夹执行相同的任务:
gulp.task('copy-bower-components-to-prod', function () {
gulp.src('./app/bower_components/**')
.pipe(gulp.dest('prod/bower_components'));
});
但这不是一个干净的代码;如何使用参数(在本例中是带有文件夹名称的参数)创建 gulp 任务?提前致谢!
gulp-utils
实际上非常适合这个:
var gutil = require('gulp-util');
function destination() {
return (!!gutil.env.type && gutil.env.type == 'prod' ? 'prod' : 'dev';
}
gulp.task('copy-bower-components', function () {
return gulp.src('./app/bower_components/**')
.pipe(gulp.dest(destination() + '/bower_components'));
});
运行它用gulp --type=prod
设置新文件夹!
您可以尝试使用此插件 gulp-foal 来 运行 带有参数的小马任务。
参数不是来自 cmd。
'use strict';
var gulp = require('gulp');
var clean = require('gulp-clean');
var foal = require('gulp-foal');
//use "foal.task(...)" to define a foal-task.
foal.task('clean_some', function(cleanPath) {
//"return" shall not be missed for running foal-task orderly.
return gulp.src(cleanPath)
.pipe(clean());
});
gulp.task('default', function(cb) {
//use "foal.run(...)" to run your foal-task with param.
foal.run(clean_some('test_path'), cb);
});