问题
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