Created
December 14, 2015 13:06
-
-
Save c-vetter/7d63109200dfb6c147ab to your computer and use it in GitHub Desktop.
Using gulp-ng-annotate with lazypipe inside gulp-watch without triggering tasks still crashes even with plumber. This setup results in a continuous pipe setup resilient against syntax errors with very little overhead.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp') | |
var gulpIf = require('gulp-if') | |
var gulpNgAnnotate = require('gulp-ng-annotate') | |
var gulpPlumber = require('gulp-plumber') | |
var gulpWatch = require('gulp-watch') | |
var lazypipe = require('lazypipe') | |
var processPipe | |
gulp.task('watch-annotate', watch) | |
processPipe = lazypipe() | |
.pipe(ngAnnotateWithoutCrashingOnSyntaxErrors) | |
/** | |
* Triggers building index.html on start and on file changes. | |
*/ | |
function watch () { | |
gulpWatch('src/**/*.js') | |
.pipe(gulpPlumber()) | |
.pipe(processPipe()) | |
.pipe(gulp.dest('dest')) | |
} | |
/** | |
* Catches SyntaxErrors before ngAnnotate gets the chance to break everything. | |
* | |
* @returns {Pipe} | |
*/ | |
function ngAnnotateWithoutCrashingOnSyntaxErrors () { | |
return gulpIf(function (file) { | |
try { | |
require(file.path) | |
} catch (e) { | |
if (e instanceof SyntaxError) { | |
// this is where everything would break down otherwise | |
return false | |
} | |
} | |
return true | |
}, gulpNgAnnotate() | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment