Javascript 您如何运行 gulp “on end” 任务,但仅在当前任务结束时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26100666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:20:33  来源:igfitidea点击:

How do you run a gulp "on end" task but only at the end of the current task?

javascriptnode.jsgulp

提问by antjanus

I'm using Gulp to collect front-matter (via gulp-front-matter plugin) and then, after aggregating it, I'm saving it into another file. Among other data, I'm saving a bunch of CSS. Here's what I have for my compileCSStask:

我正在使用 Gulp 收集前沿信息(通过 gulp-front-matter 插件),然后在聚合后将其保存到另一个文件中。在其他数据中,我保存了一堆 CSS。这是我的compileCSS任务:

var collected = [];

gulp.src('./client/**/*.html')
  .pipe(frontMatter({ property: 'meta', remove: true }))
  .pipe(through.obj(function(file, enc, callback) {
       var css = file.meta;
       collected.push(css);
   }))
   .pipe(gulp.dest('./build/templates'))
   .on('end', function() {
       var cssPath = ['build', 'assets', 'css', 'compiled.css'];
       fs.writeFileSync(cssPath.join(path.sep), cssPath);
   })

;

;

The task works as expected (note, it's a simplified version). Everything works as expected and I get a compiled.cssfile with all of the front-matter CSS. However, I found a need to use the Prefixer not only on my regular css file but on this new compiled.css as well. So I created a prefixtask:

该任务按预期工作(注意,它是一个简化版本)。一切都按预期工作,我得到了一个compiled.css包含所有前端 CSS 的文件。但是,我发现不仅需要在我的常规 css 文件上使用 Prefixer,而且还需要在这个新的 compiler.css 上使用。所以我创建了一个prefix任务:

gulp.task('prefix', ['compileCSS', 'copy'], function() {
    gulp.src('./build/assets/css/*.css')
        .pipe(autoprefixer({ browsers: ['last 3 versions'] }))
        .pipe(gulp.dest('build'))
    ;
});

Now, the problem is that the on('end'function runs at the end of ALL the tasks, not just the compileCSStask.

现在,问题是该on('end'函数在所有任务的末尾运行,而不仅仅是compileCSS任务。

My question is, is there a way to inject an "on end" type task for each task? Or is there a way to use streams somehow (since the last task isn't an actual stream and doesn't take advantage of it, I don't see how).

我的问题是,有没有办法为每个任务注入一个“on end”类型的任务?或者有没有办法以某种方式使用流(因为最后一个任务不是实际的流并且没有利用它,我不知道如何)。

回答by antjanus

It wasn't the sequence that was the problem, I wasn't returning the stream which created a race condition so a fix like this worked:

这不是问题的序列,我没有返回创建竞争条件的流,所以这样的修复工作:

return gulp.src('./client/**/*.html')
          .pipe(dosomething());
  \ all other code

I guess the problem was that Gulp waits for a stream to be done before setting off the next event. Without returning streams or promises, Gulp simply runs the task and doesn't wait for it to finish.

我猜问题是 Gulp 在触发下一个事件之前等待一个流完成。不返回流或承诺,Gulp 只是运行任务而不等待它完成。

回答by Florent

You can try run-sequence:

你可以试试run-sequence

var runSequence = require('run-sequence');
gulp.task('mytask', function(cb) {
  runSequence(
    ['parallel1', 'parallel2'],
    'seq1',
    'seq2',
    cb
  );
});

If you want something more related to your gulpfile, you should include it to your question.

如果您想要与您的 更相关的内容gulpfile,则应将其包含在您的问题中。