在所有文件完成任务之后运行代码

所以我一直在尝试一下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对象具有我需要的自定义function,为什么我不能使用像gulp-changed这样的插件。 我试图弄清楚如何(如果可能的话)在完成minify之后运行一段代码处理所有文件并且它成功运行。 是这样的东西可能与吞咽?

你可以做一个依赖html-minify的任务:

 gulp.task('other-task', ['html-minify'], function() { //stuff }); 

您也可以在html-minify任务中侦听streamend事件:

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

您还可以将两个stream与事件stream合并。 这个例子从命令行inputyargs ,build立一个configuration,然后合并这两个:

 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() { }); }); 

除了任务之前的标准,您还可以等待以前的任务完成。 当你需要将parameter passing给before任务时,这是非常有用的(这个吞吐量当前不支持):

 var tasks = { before: function(arg){ // do stuff }, build: function() { tasks.before(arg).on('end', function(){ console.log('do stuff') }); } }; gulp('build', tasks.build);