How to complete the gulp "end" task, but only at the end of the current task?

I use Gulp to collect front-matter (via the gulp-front-matter plugin), and then, after aggregating it, I save it to another file. Among other data, I save a bunch of CSS. Here is what I have for my compileCSS task:

 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 that this is a simplified version). Everything works as expected, and I get a compiled.css file with all the leading edge CSS. However, I found the need to use the prefix not only in my regular css file, but also in this new compiled.css file. So I created a prefix task:

 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 is executed at the end of ALL tasks, and not just in the compileCSS task.

My question is, is there a way to introduce an end-type task for each task? Or is there a way to use threads in some way (since the last task is not an actual thread and does not use it, I don’t see how).

+5
source share
2 answers

This was not the sequence that was the problem, I did not return the thread that created the race condition, so a fix like this worked:

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

I suppose the problem was that Gulp was expecting the thread to execute before setting the next event. Without thread returns or promises, Gulp just starts the task and does not wait for completion.

+9
source

You can try run-sequence :

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

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

+7
source

Source: https://habr.com/ru/post/1203626/


All Articles