因此,我一直在尝试Gulp,看看它在速度上与Grunt相比如何,我对结果印象深刻,但是我有一件事我不知道如何在Gulp中进行。
因此,我有这个任务来缩小HTML:
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对象具有我需要的自定义功能,以及为什么不能使用像gulp-changed这样的插件。我要弄清楚的是,缩小完成后如何(如果可能)运行代码块来处理所有文件并成功运行。gulp有可能发生这种情况吗?
您可以根据以下条件完成一项任务html-minify
:
gulp.task('other-task', ['html-minify'], function() {
//stuff
});
您还可以end
在html-minify
任务中监听stream事件:
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);
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句