Gulp.js Watch error Handling notes

Source: Internet
Author: User
Tags error handling

The following gulpfile.js files:

The code is as follows Copy Code
var gulp = require (' Gulp '),
Coffee = require (' Gulp-coffee '),
Path = {
Scripts: [' Coffee/**/*.coffee ']
};
Gulp.task (' Coffee ', function () {
Return Gulp.src (path.scripts)
. Pipe (coffee ())
. Pipe (Gulp.dest (' JS '));
});
Gulp.task (' Watch ', function () {
Gulp.watch (path.scripts, [' Coffee ']);
});

Execution at the command line:

The code is as follows Copy Code
Gulp Watch

Gulp Watch command monitor. Coffee file changes, and automatically compiled into a JS file.
The problem is that Zed has the ability to save files automatically, Coffeescript statements into half, switches to other windows to do something else, and Zed saves the file. Gulp Watch monitored the file changes and tried to compile the. coffee file, but because the statement was only half, gulp the error and forced the exit.
In this way, I have to restart the Gulp Watch command.


Solutions

One of the easiest solutions is to use Gutil.log to catch errors instead of letting them be thrown:

  code is as follows copy code
var gulp = Require (' Gulp '),
  coffee = require (' Gulp-coffee '),
  Gutil = require (' Gulp-util '),
  Path = { br>     scripts: [' Coffee/**/*.coffee ']
 };
Gulp.task (' Coffee ', function () {
  return gulp.src (path.scripts)
   . Pipe (coffee ())
    on (' Error ', Gutil.log)//Capture compilation errors here
    pipe (gulp.dest (' JS ');
});
Gulp.task (' Watch ', function () {
  Gulp.watch (path.scripts, [' Coffee ']);
});

However, this is handled in a way that Gulp watch no longer forces an exit when it meets an error, but does not continue. The Gulp Watch command is in a non-existent order, and changes made to the. Coffee file cannot be compiled.
a better way to handle this is to use Gulp-plumber1:
npm install gulp-plumber--save
New Gulpfile.js file:

The code is as follows Copy Code
var gulp = require (' Gulp '),
Coffee = require (' Gulp-coffee '),
Plumber = require (' Gulp-plumber '),
Path = {
Scripts: [' Coffee/**/*.coffee ']
};
Gulp.task (' Coffee ', function () {
Return Gulp.src (path.scripts)
. Pipe (Plumber ())//plumber to Pipe patch
. Pipe (coffee ())
. Pipe (Gulp.dest (' JS '));
});
Gulp.task (' Watch ', function () {
Gulp.watch (path.scripts, [' Coffee ']);
});

In this way, even if you compile incorrectly, the Gulp Watch command works fine.

Original from: Chen Sanbo Guest, zfanw.com thank stationmaster

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.