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