Gulp 为什么合并流需要异步完成信号?

lnlaulya  于 2022-12-08  发布在  Gulp
关注(0)|答案(2)|浏览(156)

我正在从gulp@3.9.1迁移到gulp@4.0.2,并在此过程中升级我的gulp依赖项。我的gulpfile中有以下任务,您可以假设directories只是我要执行此操作的目录数组:

var gulp = require('gulp');
var ngAnnotate = require('gulp-ng-annotate'); //annotates dependencies in Angular components
var rev = require('gulp-rev'); //appends a hash to the end of file names to eliminate stale cached files
var revReplace = require('gulp-rev-replace');
var uglify = require('gulp-uglify'); // minimizes javascript files
var compressCss = require('gulp-minify-css');
var useref = require('gulp-useref'); // replaces style and script blocks in HTML files
var filter = require('gulp-filter');
var merge = require('merge-stream');
var sourcemaps = require('gulp-sourcemaps');

function minify() {
    var tasks = directories.map(function (directory) {
        var cssFilter = filter("**/all.min.css", {restore:true});
        var jsAppFilter = filter("**/app.min.js", {restore:true});
        var jsFilter = filter("**/*.js", {restore:true});

        return gulp.src(dstBasePath + directory + "index.html", {allowEmpty: true})
            .pipe(useref())
            .pipe(cssFilter)
            .pipe(compressCss({keepSpecialComments:false}))
            .pipe(rev())
            .pipe(cssFilter.restore)
            .pipe(jsAppFilter)
            .pipe(sourcemaps.init())
            .pipe(ngAnnotate({add:true, single_quotes:true}))
            .pipe(jsAppFilter.restore)
            .pipe(jsFilter)
            .pipe(uglify())
            .pipe(rev())
            .pipe(jsFilter.restore)
            .pipe(revReplace())
            .pipe(sourcemaps.write('.'))    // sourcemaps need to be written to same folder for Datadog upload to work
            .pipe(gulp.dest(dstBasePath + directory))
    });

    return merge(tasks);
}

为什么在运行任务时会导致Gulp错误“Did you forget to signal async completion?”请注意,我使用的是Gulp 4。我已经尝试将回调done传递给此任务,并将.addListener('end', done)添加到最后一个管道,但这会导致合并流过早结束(大概是在第一个插件结束的时候)。所以也许其中一个插件在完成的时候没有发出信号,但是你怎么才能让它工作呢?谢谢你提供的任何见解。

yhxst69z

yhxst69z1#

return merge(folders.map(function (folder) {//这在过去对我很有效
如没有merge的此形式

gulp.task('init', function (done) {

  var zips = getZips(zipsPath);

  var tasks = zips.map(function (zip) {
  
    return gulp.src(zipsPath + "/" +  zip)
              
      .pipe(unzip({ keepEmpty: true }))
      
      .pipe(gulp.dest(path.join("src", path.basename(zip, ".zip"))))
      
      .on('end', function() {   // this bit is necessary
        done();
       });
  });

  return tasks;
});
6mzjoqzu

6mzjoqzu2#

Gulp4要求你发出异步完成的信号。下面这个类似问题的答案提供了一些有用的信息:Gulp error: The following tasks did not complete: Did you forget to signal async completion?
我有一个类似的例子,我返回一组合并的任务,我可以通过使函数异步并等待合并来解决这个错误。我的例子看起来像这样:

gulp.task("build", async function () {
    ...
    return await merge(tasks);
});

所以我想你应该可以做一些

async function minify(){
    ...
    return await merge(tasks);
}

相关问题