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

[亡魂溺海] 提交于 2020-01-02 04:50:38

问题


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/'));
}

回答1:


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/'));
}


来源:https://stackoverflow.com/questions/39978779/i-want-to-automatically-fix-my-css-source-files-but-i-get-an-endless-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!