gpt4 book ai didi

gulp - 在 Gulp 中,如果多个文件中的任何一个较新,我如何仅对一个文件运行任务?

转载 作者:行者123 更新时间:2023-12-04 14:44:52 25 4
gpt4 key购买 nike

我可能试图让 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/'))

});

我尝试使用 gulpif,但如果开始时没有文件进入它,它似乎不起作用。
    .pipe(gulpif(are_there_any_files_at_all,
gulp.src(["app/scripts/LibSource.ts"])))

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

也许在单个流中执行所有这些操作确实不是正确的调用,因为我将这些文件通过“gulp-newer”过滤器的唯一原因是查看它们中是否有任何更新。然后我丢弃它们并用另一个文件替换它们。我的问题仍然存在。

最佳答案

您可以编写自己的通过/转换流来处理这样的条件:

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

关于gulp - 在 Gulp 中,如果多个文件中的任何一个较新,我如何仅对一个文件运行任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24140249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com