如何在丢失的文件上获得gulp错误

How to get gulp to error on missing file?

本文关键字:gulp 错误 文件      更新时间:2023-09-26

我已经设置了这样的gulpfile.js:

var scripts = [
    'bower_components/timezone-js/src/date.js',                            
    'bower_components/jquery/jquery.min.js',                               
    'bower_components/jquery-migrate/jquery-migrate.js',                   
    'bower_components/jquery-ui/ui/minified/jquery-ui.min.js',              
    'bower_components/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',  
    ...
];
gulp.task('scripts', function () {
    return gulp.src(scripts, {base: '.'})
        .pipe(plumber(plumberOptions))
        .pipe(sourcemaps.init({
            loadMaps: false,
            debug: debug,
        }))
        ...

。,我所有的脚本文件都是精确匹配的。不匹配。

有时我弄乱了文件路径或作者更改了目录结构。我希望在发生这种情况时得到通知,而不是脚本被静默地排除并导致运行时错误。

我是否有办法让gulp.src报告这些错误?

根据这个答案使用gulp-expect-file

var coffee = require('gulp-coffee');
var expect = require('gulp-expect-file');
gulp.task('mytask', function() {
  var files = ['idontexist.html'];
  return gulp.src(files)
    .pipe(expect(files))
    .pipe(coffee());
});

(由于rve)

gulp.src实际上只是vinyl-fs.src的别名,看起来像这样:

function src(glob, opt) {
  opt = opt || {};
  var pass = through.obj();
  if (!isValidGlob(glob)) {
    throw new Error('Invalid glob argument: ' + glob);
  }
  // return dead stream if empty array
  if (Array.isArray(glob) && glob.length === 0) {
    process.nextTick(pass.end.bind(pass));
    return pass;
  }
  var options = defaults(opt, {
    read: true,
    buffer: true
  });
  var globStream = gs.create(glob, options);
  // when people write to use just pass it through
  var outputStream = globStream
    .pipe(through.obj(createFile))
    .pipe(getStats(options));
  if (options.read !== false) {
    outputStream = outputStream
      .pipe(getContents(options));
  }
  return outputStream.pipe(pass);
}

它反过来使用glob-stream,后者使用glob。您可以绕过其中的大部分,直接使用through2从数组文件创建管道。我还没想出怎么做。