在Gulp中,如果多个文件中的任何一个更新,我如何只在一个文件上运行任务?

Tay*_*tay 6 gulp

我可能试图让gulp做一些不是惯用的东西,但是这里有.我希望我的构建任务只在源文件比输出文件更新时运行.

在gulp中,似乎标准做法是创建始终运行的构建任务,然后设置监视任务以仅在某些文件更改时运行该构建任务.没关系,但这意味着你总是在第一次运行时建立.

那么,有可能做我想要的吗?这是我到目前为止所做的事情(更新的是gulp-newer):

gulp.task('build_lib', function() {

return gulp.src(["app/**/*.ts"])
    .pipe(newer("out/outputLib.js")) //are any of these files newer than the output?

 ** NEED SOMETHING HERE **
   how do I say, "If I got _any_ files from the step before, replace all of them with a single hardcoded file "app/scripts/LibSource.ts" "?

    .pipe(typescript({
        declaration: true,
        sourcemap: true,
        emitError: false,
        safe: true,
        target: "ES5",
        out: "outputLib.js"
    }))
    .pipe(gulp.dest('out/'))
Run Code Online (Sandbox Code Playgroud)

});

我尝试使用gulpif,但是如果没有文件进入它开始它似乎不起作用.

    .pipe(gulpif(are_there_any_files_at_all,
        gulp.src(["app/scripts/LibSource.ts"])))
Run Code Online (Sandbox Code Playgroud)

但是,我的条件函数甚至没有被调用,因为没有文件可以调用它.gulpif在这种情况下调用了truthy流,因此LibSource被添加到我的流中,这不是我想要的.

也许在单个流中完成所有这些并不是正确的调用,因为我通过"gulp-newer"过滤器传递这些文件的唯一原因是看它们是否更新.然后我丢弃它们并用另一个文件替换它们.我的问题仍然存在.

Kyl*_*ung 6

您可以编写自己的through/transform流来处理这样的条件:

// Additional core libs needed below
var path = require('path');
var fs = require('fs');
// Additional npm libs
var newer = require('gulp-newer');
var through = require('through');
var File = require('vinyl');

gulp.task('build_lib', function() {
  return gulp.src(["app/**/*.ts"])
    .pipe(newer("out/outputLib.js"))
    .pipe(through(function(file) {
      // If any files get through newer, just return the one entry
      var libsrcpath = path.resolve('app', 'scripts', 'LibSource.ts');
      // Pass libsrc through the stream
      this.queue(new File({
        base: path.dirname(libsrcpath),
        path: libsrcpath,
        contents: new Buffer(fs.readFileSync(libsrcpath))
      }));
      // Then end this stream by passing null to queue
      // this will ignore any other additional files
      this.queue(null);
    }))
    .pipe(typescript({
      declaration: true,
      sourcemap: true,
      emitError: true,
      safe: true,
      target: "ES5",
      out: "outputLib.js"
    }))
    .pipe(gulp.dest('out/'));
});
Run Code Online (Sandbox Code Playgroud)