Skip to content

Instantly share code, notes, and snippets.

@apfelbox
Last active February 21, 2017 15:26
Show Gist options
  • Save apfelbox/b2d5ee9fe32e5b42adb2 to your computer and use it in GitHub Desktop.
Save apfelbox/b2d5ee9fe32e5b42adb2 to your computer and use it in GitHub Desktop.
(gulp + browserify) - gulp-browserify
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/");
}
);
@latviancoder
Copy link

Man, I was looking for this such a long time. Thanks! This one catches everything!

@dougmacklin
Copy link

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.

@randseay
Copy link

@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
});

@cipiripper
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment