Search code examples
node.jsbrowserifygulp-uglify

gulp - uglify js files with browserify?


I am following the example from here exactly and have my own version:

gulp.task('build-js', function() {
    var bundler = browserify('js/*.js');

    return bundler.pipe()
        .pipe(source('bundle.min.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('dist'))
        .pipe(livereload());
});

But why do I get this error:

$ gulp
[21:48:33] Using gulpfile /var/www/html/mysite/gulpfile.js
[21:48:33] Starting 'apply-prod-environment'...
Setting NODE_ENV to 'production'
Successfully set NODE_ENV to production
[21:48:33] Finished 'apply-prod-environment' after 169 μs
[21:48:33] Starting 'build-js'...
[21:48:33] 'build-js' errored after 11 ms
[21:48:33] TypeError: bundler.pipe is not a function

What have I missed?


Solution

  • It seems that example provided by vinyl-buffer has a mistake: You must call bundle() before adding transforms, not pipe():

    return bundler.bundle()
        .pipe(source('bundle.min.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('dist'))
        .pipe(livereload());
    

    Another example is provided in the Gulp repository (one should indeed use the browserify API directly in Gulp).