我想自动修复我的CSS源文件

我想自动修复我的CSS源文件

本文介绍了我想自动修复我的CSS源文件,但是我得到了一个无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gulp-stylefmt,它每次保存任何CSS文件时都会运行:
$ b

 函数stylelint(){
return gulp.src('src / ** / *。css')
.pipe($。stylefmt())
.pipe(gulp.dest('的src /'));
}

同一时间 gulp.watch code> src / ** / *。css 并再次运行 stylelint 任务。

控制台输出: $ b

[16:32:24]开始'stylelint'...
[16:32:25] 554 ms后完成'stylelint'
[16:32:25]开始'stylelint'...
[16:32:25] 60 ms后完成'stylelint'
[16:32:25]开始'stylelint'...
[16:32:25] 33 ms后完成'stylelint'
[16:32:25]开始'stylelint '...
[16:32:25] 36 ms后完成'stylelint'
[16:32:26]开始'stylelint'...
[16:32:26 ] 34 ms后完成'stylelint'
[16:32:26]开始'stylelint'...
[16:32:26] 29 ms后完成'stylelint'
[16 :32:26]开始'stylelint'...
[16:32:26] 27 ms后完成'stylelint'
[16:32:26]开始'stylelint'...
[16:32:26]完成了tylelint'32 ms后

有一种方法可以纠正这种行为并运行stylelint em>任务只有一次?



UPD 。我添加了 gulp-cached ,现在它只运行两次。我可以改进它运行一次吗?

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


解决方案

1 by @SvenSchoenung gulp.lastRun 。在我的情况下,我没有使用 gulp-cached ,但是我使用了 gulp4

  function stylelint(){
return gulp.src('src / ** / *。css',{since:gulp.lastRun(stylelint)})
.pipe($。stylefmt())
.pipe(gulp.dest('src /'));
}


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/'));
}
解决方案

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

这篇关于我想自动修复我的CSS源文件,但是我得到了一个无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:39