Gulp 吞下要处理的传入文件

Gulp 吞下要处理的传入文件,gulp,Gulp,我有一个包含两个任务的gulp文件,其中对一组文件执行完全相同的操作,唯一的区别是输入文件和输出名称以及目标。是否有一种方法可以使用相同的任务来处理2批文件而不复制代码?我想是某种函数。我试着输入参数并在谷歌上搜索,但我能找到我想要实现的任何例子 var outPutFolder = '../'; //Array of all the JS files to compile for scripts.min.js var js = [ 'js/library/_helpers.js',

我有一个包含两个任务的gulp文件,其中对一组文件执行完全相同的操作,唯一的区别是输入文件和输出名称以及目标。是否有一种方法可以使用相同的任务来处理2批文件而不复制代码?我想是某种函数。我试着输入参数并在谷歌上搜索,但我能找到我想要实现的任何例子

var outPutFolder = '../';

//Array of all the JS files to compile for scripts.min.js
var js = [
    'js/library/_helpers.js',
    'js/library/_form-validation.js',
    'js/page/_global.js',
    'js/library/_ga_event_tracking.js',
    'js/library/_postcode-anywhere.js',
    'js/library/_count-down-timer.js',
    'js/page/_home-page.js',
    'js/page/_main_navigation.js',
    'js/page/_myaccount.js',
];

//Array of all the JS files to compile for scripts-external.min.js
var externalJs = [
    'js/external/_bootstrap.min.js',
    'js/external/_bootstrap-select.js',
    'js/external/_bootbox.min.js',
    'js/external/_jquery.lazyload.js',
    'js/external/_jquery.bxslider.js',
];

//JS Task to check files for errors, compile and then minify
gulp.task('scripts', function() {
  return gulp.src(js)
      .pipe(eslint())
      .pipe(eslint.format())
      .pipe(concat('scripts.min.js'))
      .pipe(gulp.dest(outPutFolder + 'js'))
      .pipe(notify({ title:"scripts.min.js",message: "Successfully Compiled", onLast: true }))
      .on('end', function () { gutil.log('scripts.min.js compiled successfully!'); });
});

//JS Task to check files for errors, compile and then minify
gulp.task('scriptsExternal', function() {
  return gulp.src(externalJs)
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(concat('scripts-external.min.js'))
    .pipe(gulp.dest(outPutFolder + 'js'))
    .pipe(notify({ title: "scripts-external.min.js", message: "Successfully Compiled", onLast: true }))
    .on('end', function () { gutil.log('scripts-external.min.js compiled successfully!'); });
});

变体1:

gulpfile是纯JavaScript,因此可以使用函数。但是,如果按顺序使用此任务,则必须返回filestream。试试这个:

var outPutFolder = '../';

//Array of all the JS files to compile for scripts.min.js
var js = [ … ];

//Array of all the JS files to compile for scripts-external.min.js
var externalJs = [ … ];

function doStuff(in, out) {
  return gulp.src(in)
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(concat(out))
    .pipe(gulp.dest(outPutFolder + 'js'))
    .pipe(notify({ title: out,message: "Successfully Compiled", onLast: true }))
    .on('end', function () { gutil.log(out + ' compiled successfully!'); });
}

//JS Task to check files for errors, compile and then minify
gulp.task('scripts', function() {
  return doStuff(js, 'scripts.min.js');
});

//JS Task to check files for errors, compile and then minify
gulp.task('scriptsExternal', function() {
  return doStuff(externalJs, 'scripts-external.min.js');
});

变体2:

或者,您也可以通过以下方式触发:

并使用

gulp scripts --external

变体1:

gulpfile是纯JavaScript,因此可以使用函数。但是,如果按顺序使用此任务,则必须返回filestream。试试这个:

var outPutFolder = '../';

//Array of all the JS files to compile for scripts.min.js
var js = [ … ];

//Array of all the JS files to compile for scripts-external.min.js
var externalJs = [ … ];

function doStuff(in, out) {
  return gulp.src(in)
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(concat(out))
    .pipe(gulp.dest(outPutFolder + 'js'))
    .pipe(notify({ title: out,message: "Successfully Compiled", onLast: true }))
    .on('end', function () { gutil.log(out + ' compiled successfully!'); });
}

//JS Task to check files for errors, compile and then minify
gulp.task('scripts', function() {
  return doStuff(js, 'scripts.min.js');
});

//JS Task to check files for errors, compile and then minify
gulp.task('scriptsExternal', function() {
  return doStuff(externalJs, 'scripts-external.min.js');
});

变体2:

或者,您也可以通过以下方式触发:

并使用

gulp scripts --external