Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to automatically fix my CSS source files, but I get an endless loop

I use gulp-stylefmt and it run every time I save any CSS file:

function stylelint () {
  return gulp.src('src/**/*.css')
    .pipe($.stylefmt())
    .pipe(gulp.dest('src/'));
}

Same time gulp.watch watches the path src/**/*.css and run stylelint task again.

Console output:

[16:32:24] Starting 'stylelint'...
[16:32:25] Finished 'stylelint' after 554 ms
[16:32:25] Starting 'stylelint'...
[16:32:25] Finished 'stylelint' after 60 ms
[16:32:25] Starting 'stylelint'...
[16:32:25] Finished 'stylelint' after 33 ms
[16:32:25] Starting 'stylelint'...
[16:32:25] Finished 'stylelint' after 36 ms
[16:32:26] Starting 'stylelint'...
[16:32:26] Finished 'stylelint' after 34 ms
[16:32:26] Starting 'stylelint'...
[16:32:26] Finished 'stylelint' after 29 ms
[16:32:26] Starting 'stylelint'...
[16:32:26] Finished 'stylelint' after 27 ms
[16:32:26] Starting 'stylelint'…
[16:32:26] Finished 'stylelint' after 32 ms

There is a way to correct this behavior and run stylelint task only one time?

UPD. I added gulp-cached and now it runs only twice. Can I improve it to run once?

function stylelint () {
  return gulp.src('src/**/*.css')
    .pipe($.cached('stylelint'))
    .pipe($.stylefmt())
    .pipe($.cached('stylelint'))
    .pipe(gulp.dest('src/'));
}
like image 670
dmin Avatar asked Oct 11 '16 13:10

dmin


1 Answers

I think I repeated the solution 1 by @SvenSchoenung with since and gulp.lastRun. In my case I did it without gulp-cached, but I use gulp4

function stylelint () {
  return gulp.src('src/**/*.css', {since: gulp.lastRun(stylelint)})
 .pipe($.stylefmt())
 .pipe(gulp.dest('src/'));
}
like image 199
dmin Avatar answered Nov 01 '22 22:11

dmin