node.js - 在完成所有文件的 gulp 任务后运行代码

所以我一直在尝试 Gulp,看看它在速度方面与 Grunt 相比如何,我对结果印象深刻,但我有一件事我不知道如何在 Gulp 中做。

所以我有这个 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
});

您还可以在 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);
  });
});

https://stackoverflow.com/questions/24600052/

相关文章:

eclipse - Java Eclipse 在保存时关闭自动构建工作区

android - Buck vs Gradle,Android 构建系统的优缺点

gcc - 如何在编译期间强制 cmake 包含 "-pthread"选项?

build - WiX x64 平台目标

c - 如何在 Windows 中使用 MSVC 从命令行构建 DLL

ant - 如何在使用 ANT 删除目录之前检查目录是否存在?

iphone - XCTest.framework 构建错误

c++ - 如何让cmake找到CUDA

visual-studio-2010 - 禁止在构建时创建 *.vshost.exe 和其他文件

c++ - 如何在 Mac OS X (C++) 中使用 dylib