当前位置: 动力学知识库 > 问答 > 编程问答 >

How to make a gulp task with param?

问题描述:

There is a gulp task:

gulp.task('copy-bower-components-to-dev', function () {

gulp.src('./app/bower_components/**')

.pipe(gulp.dest('dev/bower_components'));

});

I also need the same task for another folder:

gulp.task('copy-bower-components-to-prod', function () {

gulp.src('./app/bower_components/**')

.pipe(gulp.dest('prod/bower_components'));

});

But it is not a clean code; how can I make a gulp task with params(in this case param with a folder name)? Thanks in advance!

网友答案:

The gulp-utils are actually perfect for this:

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'));
});

Run it with gulp --type=prod to set the new folder!

网友答案:

You can try this plugin gulp-foal to run a foal-task with param.

param NOT from 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);
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: