Node.js 在所有文件的gulp任务完成后运行代码
所以,我一直在尝试“大口喝”,看看它在速度方面与Grunt相比如何,我对结果印象非常深刻,但我有一件事我不知道如何在“大口喝”中做到 因此,我有一个吞咽任务来缩小HTML:Node.js 在所有文件的gulp任务完成后运行代码,node.js,build,gulp,Node.js,Build,Gulp,所以,我一直在尝试“大口喝”,看看它在速度方面与Grunt相比如何,我对结果印象非常深刻,但我有一件事我不知道如何在“大口喝”中做到 因此,我有一个吞咽任务来缩小HTML: gulp.task('html-minify', function() { var files = [ relativePaths.webPath + '/*.html', relativePaths.webPath + '/components/**/*.html', relativePaths
gulp.task('html-minify', function() {
var files = [
relativePaths.webPath + '/*.html',
relativePaths.webPath + '/components/**/*.html',
relativePaths.webPath + '/' + relativePaths.appPath + '/components/**/*.html'
];
var changedFiles = buildMetaData.getChangedFiles(files);
//TODO: needs to execute only after successful run of the task
buildMetaData.addBuildMetaDataFiles(changedFiles);
buildMetaData.writeFile();
return gulp.src(changedFiles, {
base: relativePaths.webPath
})
.pipe(filelog())
.pipe(minifyHtml({
empty: true,
quotes: true,
conditionals: true,
comments: true
}))
.pipe(gulp.dest(relativePaths.webPath + '/' + relativePaths.appPath + '/' + relativePaths.buildPath));
});
buildMetaData对象具有我需要的自定义功能,以及为什么我不能使用GulpChanged之类的插件。我想弄清楚的是,在缩微完成后如何(如果可能的话)运行一段代码,处理所有文件并成功运行。通过gulp可以实现类似的功能吗?您可以根据
html minify
创建一个任务:
gulp.task('other-task', ['html-minify'], function() {
//stuff
});
您还可以在html minify
任务中侦听流end
事件:
gulp.task('html-minify', function(done) {
var files = [
relativePaths.webPath + '/*.html',
relativePaths.webPath + '/components/**/*.html',
relativePaths.webPath + '/' + relativePaths.appPath + '/components/**/*.html'
];
var changedFiles = buildMetaData.getChangedFiles(files);
//TODO: needs to execute only after successful run of the task
buildMetaData.addBuildMetaDataFiles(changedFiles);
buildMetaData.writeFile();
var stream = gulp.src(changedFiles, {
base: relativePaths.webPath
})
.pipe(filelog())
.pipe(minifyHtml({
empty: true,
quotes: true,
conditionals: true,
comments: true
}))
.pipe(gulp.dest(relativePaths.webPath + '/' + relativePaths.appPath + '/' + relativePaths.buildPath));
stream.on('end', function() {
//run some code here
done();
});
stream.on('error', function(err) {
done(err);
});
});
您还可以使用合并两个流。此示例使用从命令行获取输入,构建一个配置,然后合并两个:
var enviroment = argv.env || 'development';
gulp('build', function () {
var config = gulp.src('config/' + enviroment + '.json')
.on('end', function() { gutil.log(warn('Configured ' + enviroment + ' enviroment.')); })
.pipe(ngConstant({name: 'app.config'}));
var scripts = gulp.src('js/*');
return es.merge(config, scripts)
.pipe(concat('app.js'))
.pipe(gulp.dest('app/dist'))
.on('error', function() { });
});
除了标准的任务之前,您还可以等待上一个任务完成。当您需要将参数传递给before任务(gulp当前不支持该任务)时,这非常有用:
狼吞虎咽
使用依赖项任务:
gulp.task('qr-task', ['md-task', 'js-task'], function() {
gulp.src(src + '/*.qr')
.pipe(plugin())
.pipe(gulp.dest(dist));
});
虽然主任务在所有相关任务之后启动,但它们(相关任务)将并行运行(同时运行),因此不要假设任务将按顺序启动/完成(md和js在qr之前并行运行)
如果您想要几个任务的确切顺序,并且不想拆分它们,可以使用async&Wait来实现这一点:
function Async(p) {
return new Promise((res, rej) => p.on('error', err => rej(err)).on('end', () => res()));
}
gulp.task('task', async () => {
await Async(gulp.src(src + '/*.md')
.pipe(plugin())
.pipe(gulp.dest(dist)));
await Async(gulp.src(src + '/*.js')
.pipe(plugin())
.pipe(gulp.dest(dist)));
await Async(gulp.src(src + '/*.qr')
.pipe(plugin())
.pipe(gulp.dest(dist)));
});
吞咽V4
在gulp 4中,旧的依赖模式被删除,您将得到以下错误:
AssertionError [ERR_ASSERTION]: Task function must be specified
相反,您必须使用gulp.parallel和gulp.series(提供任务的正确执行):
有关更多详细信息,请访问第二种方法是我正在寻找的,谢谢。您可以执行
stream.on('error',done')代码>将其简化为一小部分。无需创建另一个匿名函数。【选项1】的神奇之处在于,调用“其他任务”将自动运行“html minify”,等待它完成,然后再自行运行。谢谢如果在所有任务完成后,Gulp本身没有发出事件,我会非常惊讶和失望。。
AssertionError [ERR_ASSERTION]: Task function must be specified
gulp.task('qr-task', gulp.series('md-task', 'js-task', function(done) {
gulp.src(src + '/*.qr')
.pipe(plugin())
.pipe(gulp.dest(dist));
done();
}));