简体   繁体   English

在自动更新/自动生成的文件上使用gulp进行无限监视

[英]Infinite watch loop with gulp on auto-updated / auto-generated files

I am attempting to use some gulp plugins ( jscs , csscomb ) to style my code on the fly during dev time. 我正在尝试使用一些jscs插件( jscscsscomb )在开发期间即时设置代码样式。

I'm having a problem with the gulp process running an infinite loop with the format task. 我在gulp进程运行带有format任务的无限循环时遇到问题。

What's I believe to be happening: 我相信会发生什么:

  • start a serve task of some kind 开始某种serve任务
  • an initial run is performed with all tasks to prep files for the staging server 对所有任务执行初始运行以为登台服务器准备文件
  • a local staging server is started in parallel with a watch task 本地登台服务器与监视任务并行启动
  • myfile.scss is updated by a developer myfile.scss由开发人员更新
  • the gulp watcher starts the csscomb task gulp观察器启动csscomb任务
  • csscomb plugin changes the file and replaces it csscomb插件更改文件并替换它
  • the watcher task sees the change from the file replacement & starts the format task again... 观察者任务会看到文件替换中的更改并再次启动格式化任务...
  • the csscomb plugin runs again and so on ... csscomb插件再次运行,依此类推...

Here is a snippet that causes this loop. 这是导致此循环的代码段。 (Note: this uses v4 of gulp) (注意:这使用的是Gulp v4)

'use strict'

import { task, parallel, series, src, dest, watch, plugins } from './gulp';

import { startStagingServer } from './servers';

import { solution } from './solution.map';

const path = require('path');

task('serve', parallel(startStagingServer, watchStyles);

function watchStyles() {
  watch([ solution.src.styles ], series(formatStyles, compileStyles))
}

function formatStyles(done) {
  return src([ solution.src.styles ])
    .pipe(plugins.csscomb())
    .pipe(dest(solution.src.mount)) // the root of the solution
}

function compileStyles() {
  return src([ solution.src.styles ])
    .pipe(plugins.sass().on('error', plug.sass.logError))
    .pipe(dest(path.join(solution.dest.stage, 'serve')));
}

Does anyone know a way to avoid this? 有谁知道避免这种情况的方法?

The way to avoid this is not to put the fix in the watcher. 避免这种情况的方法是不将修复程序放在观察程序中。 Use 2 separate functions: one that fixes and the other that doesn't. 使用2个单独的功能:一个可修复,而另一个则不能。 Only watch the one that doesn't. 只看那些不看的。 Example: 例:

function taskJscsFix() {
    return gulp.src(path.JS)
        .pipe(jscs({
            configPath: './gulp/.jscsrc',
            fix: true
        }))
        .pipe(gulp.dest(path.SRC.JS));
}

function taskScripts() {
    return gulp.src(path.JS)
        .pipe(jscs({
            configPath: './gulp/.jscsrc'
        }))
        .pipe(jscs.reporter())
        .pipe(gulp.dest(path.DEST.JS));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM