仅包含if存在

use*_*926 11 gulp

我想在gulp中包含一个文件,只要它存在,当我编译时,用于开发.目前我有以下内容:

gulp.task('compile:js:development', function() {
  return gulp.src([
    'src/js/**/*.js',
  ]).pipe(concat('dist.js'))
    .pipe(gulp.dest('compiled/js/'))
});
Run Code Online (Sandbox Code Playgroud)

我需要向此数组添加另一个文件,但仅限于该文件存在.我已经看到gulp-if但我不认为我有能力寻找.

我还要警告开发人员,在控制台中进行开发编译时,该文件不存在.

Bri*_*laz 18

Gulp只是一个节点应用程序,因此您可以在gulpfile中使用任何节点函数.您可以使用fs.exists()轻松检查文件是否存在

gulp.task('compile:js:development', function() {
  var fs = require('fs'),
      files = ['src/js/**/*.js'],
      extraFile = 'path/to/other/file';

  if (fs.existsSync(extraFile)) {
    files.push(extraFile);
  } else {
    console.log('FILE DOES NOT EXIST');
  }

  return gulp.src(files)
    .pipe(concat('dist.js'))
    .pipe(gulp.dest('compiled/js/'))
});
Run Code Online (Sandbox Code Playgroud)