防止错误破坏/损坏gulp watch

node.js Node.js

Itachi

2020-03-24

我正在运行gulp 3.6.2,并具有从在线示例中设置的以下任务

gulp.task('watch', ['default'], function () {
  gulp.watch([
    'views/**/*.html',        
    'public/**/*.js',
    'public/**/*.css'        
  ], function (event) {
    return gulp.src(event.path)
      .pipe(refresh(lrserver));
  });

  gulp.watch(['./app/**/*.coffee'],['scripts']);
  gulp.watch('./app/**/*.scss',['scss']);
});

每当我的CoffeeScript gulp手表出现错误时,手表就会停止-显然不是我想要的。

正如其他地方推荐的那样

gulp.watch(['./app/**/*.coffee'],['scripts']).on('error', swallowError);
gulp.watch('./app/**/*.scss',['scss']).on('error', swallowError);
function swallowError (error) { error.end(); }

但它似乎不起作用。

我究竟做错了什么?


响应@Aperçu的回答,我修改了swallowError方法并尝试了以下操作:

gulp.task('scripts', function () {
  gulp.src('./app/script/*.coffee')
    .pipe(coffee({ bare: true }))
    .pipe(gulp.dest('./public/js'))
    .on('error', swallowError);
});

重新启动,然后在我的coffee文件中创建语法错误。同样的问题:

[gulp] Finished 'scripts' after 306 μs

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: W:\bariokart\app\script\trishell.coffee:5:1: error: unexpected *
*
^
  at Stream.modifyFile (W:\bariokart\node_modules\gulp-coffee\index.js:37:33)
  at Stream.stream.write (W:\bariokart\node_modules\gulp-coffee\node_modules\event-stream\node_modules\through\index.js:26:11)
  at Stream.ondata (stream.js:51:26)
  at Stream.EventEmitter.emit (events.js:95:17)
  at queueData (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:43:21)
  at next (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:71:7)
  at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:85:7
  at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\lib\src\bufferFile.js:8:5
  at fs.js:266:14
  at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\graceful-fs.js:104:5
  at Object.oncomplete (fs.js:107:15)

第3266篇《防止错误破坏/损坏gulp watch》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
2020.03.24

一个简单的解决方案是gulp watch在Bash(或sh)shell中放入无限循环。

while true; do gulp; gulp watch; sleep 1; done

编辑JavaScript时,请将此命令的输出保存在屏幕上的可见区域。当您的编辑导致错误时,Gulp将崩溃,打印其堆栈跟踪,等待一秒钟,然后继续查看源文件。然后,您可以更正语法错误,Gulp将通过打印正常输出或再次崩溃(然后恢复)来指示编辑是否成功。

这将在Linux或Mac终端中工作。如果您使用的是Windows,请使用Cygwin或Ubuntu Bash(Windows 10)。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android