Search code examples
javascriptcoffeescriptgulpbrowserify

Use glob matching, when passing files to browserify in gulp


All the examples I have seen using browserify and gulp assume that you only want to browserify 1 file. This is usually not the case.

I came across an example that used vinyl-transforms, but I am unable to get it to work correctly. Here is the (coffee-script) code:

# Browserify JS

gulp.task 'browserify', [], ->

    # Create the transform
    br = transform (f) ->
        return browserify(f).bundle()

    # Run browserify
    gulp.src(['./public/js/**/*.js'])
        .pipe(br)
        .pipe(gulp.dest('.'))

But I get the following error:

[10:50:55] Starting 'browserify'...

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write after end

Solution

  • The easiest way would be to use glob directly:

    var glob = require('glob');
    
    gulp.task('browserify', function() {
      var files = glob.sync('./public/js/**/*.js');
      return browserify({entries: files})
        .bundle()
        .pipe(gulp.dest('.'));
    });