Last active
February 21, 2017 15:26
-
-
Save apfelbox/b2d5ee9fe32e5b42adb2 to your computer and use it in GitHub Desktop.
(gulp + browserify) - gulp-browserify
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 watch = require("gulp-watch"); | |
var plumber = require("gulp-plumber"); | |
var tap = require("gulp-tap"); | |
var browserify = require("browserify"); | |
var gulpif = require("gulp-if"); | |
var streamify = require("gulp-streamify"); | |
var gutil = require('gulp-util'); | |
var isDebug = false; | |
watch( | |
{ | |
glob: "src/js/**/*.js" | |
}, | |
function () | |
{ | |
// my browserify entry points are the files directly in the js directory, included files are in subfolders | |
return gulp.src("src/js/*.js") | |
.pipe(plumber()) | |
.pipe(tap( | |
function (file) | |
{ | |
var d = require('domain').create(); | |
d.on("error", | |
function (err) { | |
gutil.log(gutil.colors.red("Browserify compile error:"), err.message, "\n\t", gutil.colors.cyan("in file"), file.path); | |
gutil.beep(); | |
} | |
); | |
d.run(function () { | |
file.contents = browserify({ | |
entries: [file.path], | |
debug: isDebug | |
}).bundle(); | |
}); | |
} | |
)) | |
.pipe(gulpif(!isDebug, streamify(uglify({ | |
compress: true | |
})))) | |
.pipe(gulp.dest("out/"); | |
} | |
); |
Thanks for this, however I have one lingering issue. Once there's an error, this correctly outputs the error message and prevents gulp from quitting, however it never finishes the task so future changes to any js files are ignored. Any ideas? I found this from @latviancoder's blog entry.
@dougmacklin I was running into this same issue, but found a way around it. Whilst handling the error, be sure to end the task. This way gulp can continue watching your files once again.
.on('error', function(err) {
gutil.log(
gutil.colors.red('Browserify compile error:'),
err.message
);
gutil.beep();
this.emit('end'); // Ends the task
});
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Man, I was looking for this such a long time. Thanks! This one catches everything!