【问题标题】:"grunt watch" always stops watching after fatal errors - how to make it retry after some seconds automatically?“grunt watch”总是在出现致命错误后停止观看 - 如何让它在几秒钟后自动重试?
【发布时间】:2020-01-19 08:59:48
【问题描述】:

在一些项目中,我将 Grunt 用于 SASS 编译等任务。基本上它就像一个魅力,但只要我在 SASS 文件中输入,显然有时会发生错误,因为我还没有完成。

我的Gruntfile的watch任务是这样的:

// watch files
watch: {
    sass: {
        files: ['web/var/static/**/*.scss','web/var/static/lib/**','web/var/static/js/*.js'],
        tasks: ['default']
    }
},

我在 cli 中以 grunt watch 开头。

Grunt 日志中的示例错误:

Completed in 1.968s at Wed Sep 18 2019 13:43:01 GMT+0200 (Central European Summer Time) - Waiting...
>> File "web/var/static/sass/layout/footer.scss" changed.
Running "sass:dist" (sass) task

Running "autoprefixer:dist" (autoprefixer) task
>> 4 autoprefixed stylesheets created.

Running "cssmin:dist" (cssmin) task
>> 4 files created. 706.54 kB → 598.46 kB

Running "concat:dist" (concat) task

Running "uglify:main" (uglify) task

Done.
Completed in 1.769s at Wed Sep 18 2019 13:43:25 GMT+0200 (Central European Summer Time) - Waiting...
>> File "web/var/static/sass/layout/footer.scss" changed.
Running "sass:dist" (sass) task
Fatal error: Error: property "ul" must be followed by a ':'
        on line 52 of web/var/static/sass/layout/footer.scss
        from line 13 of web/var/static/sass/main.scss
>>     ul

   ----^

如果发生这种情况,我总是必须重新运行任务,因为 grunt 停止观看。这意味着:

  • 打开控制台窗口
  • 停止当前任务
  • 重新开始任务
  • 更改文件

这真的很慢。

有人遇到过类似的问题吗?一旦发生新文件更改,如何让 grunt 在出错后自动重试?

【问题讨论】:

  • 也许在options 对象中设置debounceDelayinterval 可能会有所帮助。
  • 有点帮助,但也有缺点。谢谢,再有一个解决方案会更好。
  • 你的 grunt 版本是什么?您可以发布导致上述错误的完整 grunt 文件和示例 sass 文件吗?您是否更改了一些设置,例如 spawnforever
  • @ChickenSoups Grunt 是最新的。 SASS 文件在打字过程中有时会明显损坏。我一直在尝试,我还没有尝试 spawn。
  • 所以你可以试试options: {spawn: true}。发布完整的 grunt 文件内容会很有帮助。

标签: javascript css sass gruntjs file-watcher


【解决方案1】:

spawn 方法返回对生成的孩子的引用。当孩子退出时,doneFunction 被调用。如果出现错误,您可以从以下函数中重试

function doneFunction(error, result, code) {
  // If the exit code was non-zero and a fallback wasn't specified, an Error
  // object, otherwise null.
  error
  // The result object is an object with the properties .stdout, .stderr, and
  // .code (exit code).
  result
  // When result is coerced to a string, the value is stdout if the exit code
  // was zero, the fallback if the exit code was non-zero and a fallback was
  // specified, or stderr if the exit code was non-zero and a fallback was
  // not specified.
  String(result)
  // The numeric exit code.
  code
}

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多