114

I tried to uglify output of Browserify in Gulp, but it doesn't work.

gulpfile.js

var browserify = require('browserify');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var source = require('vinyl-source-stream');

gulp.task('browserify', function() {
    return browserify('./source/scripts/app.js')
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(uglify()) // ???
        .pipe(gulp.dest('./build/scripts'));
});

As I understand I cannot make it in steps as below. Do I need to make in one pipe to preserve the sequence?

gulp.task('browserify', function() {
    return browserify('./source/scripts/app.js')
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(uglify()) // ???
        .pipe(gulp.dest('./source/scripts'));
});

gulp.task('scripts', function() {
    return grunt.src('./source/scripts/budle.js')
        .pipe(uglify())
        .pipe(gulp.dest('./build/scripts'));
});

gulp.task('default', function(){
    gulp.start('browserify', 'scripts');
});
ndequeker
  • 7,336
  • 6
  • 54
  • 89
Nik Terentyev
  • 2,150
  • 3
  • 13
  • 22

3 Answers3

188

You actually got pretty close, except for one thing:

  • you need to convert the streaming vinyl file object given by source() with vinyl-buffer because gulp-uglify (and most gulp plugins) works on buffered vinyl file objects

So you'd have this instead

var browserify = require('browserify');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');

gulp.task('browserify', function() {
  return browserify('./source/scripts/app.js')
    .bundle()
    .pipe(source('bundle.js')) // gives streaming vinyl file object
    .pipe(buffer()) // <----- convert from streaming to buffered vinyl file object
    .pipe(uglify()) // now gulp-uglify works 
    .pipe(gulp.dest('./build/scripts'));
});

Or, you can choose to use vinyl-transform instead which takes care of both streaming and buffered vinyl file objects for you, like so

var gulp = require('gulp');
var browserify = require('browserify');
var transform = require('vinyl-transform');
var uglify = require('gulp-uglify');


gulp.task('build', function () {

  // use `vinyl-transform` to wrap the regular ReadableStream returned by `b.bundle();` with vinyl file object
  // so that we can use it down a vinyl pipeline
  // while taking care of both streaming and buffered vinyl file objects
  var browserified = transform(function(filename) {
    // filename = './source/scripts/app.js' in this case
    return browserify(filename)
      .bundle();
  });

  return gulp.src(['./source/scripts/app.js']) // you can also use glob patterns here to browserify->uglify multiple files
    .pipe(browserified)
    .pipe(uglify())
    .pipe(gulp.dest('./build/scripts'));
});

Both of the above recipes will achieve the same thing.

Its just about how you want to manage your pipes (converting between regular NodeJS Streams and streaming vinyl file objects and buffered vinyl file objects)

Edit: I've written a longer article regarding using gulp + browserify and different approaches at: https://medium.com/@sogko/gulp-browserify-the-gulp-y-way-bb359b3f9623

Hafiz Ismail
  • 3,098
  • 1
  • 23
  • 21
  • The vinyl-transform solution results in a broswerified bundle for each file in the glob, rather than a single bundle for all the files. @hafiz-ismail how can the vinyl-transform approach be used to create a single bundle? – Brian Leathem Nov 05 '14 at 22:01
  • @BrianLeathem: each file from the glob pattern will be a separate main entry file for browserify, and each entry file will result in separate bundle. If you want to concatenate all of the bundle outputs into a single file, you can use [`gulp-concat`](https://github.com/wearefractal/gulp-concat) and add it to the end of your gulp pipeline. That will be the equivalent of running `browserify > single-file.js` in terminal. Let me know if I answered your question. Cheers! – Hafiz Ismail Nov 06 '14 at 06:56
  • 11
    Hmm, I think the second example with `vinyl-transform` does not work anymore, eh?! – yckart Aug 07 '15 at 22:17
  • Regarding vinyl-transform: https://github.com/substack/node-browserify/issues/1198 – Egon Olieux Sep 05 '16 at 00:37
  • How to preserve the copyright notices in this scenario? – Pankaj Aug 31 '18 at 18:05
12

Two additional approaches, taken from the vinyl-source-stream NPM page:

Given:

var source = require('vinyl-source-stream');
var streamify = require('gulp-streamify');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
var gulpify = require('gulpify');
var gulp = require('gulp');

Approach 1 Using gulpify (deprecated)

gulp.task('gulpify', function() {
  gulp.src('index.js')
    .pipe(gulpify())
    .pipe(uglify())
    .pipe(gulp.dest('./bundle.js'));
});

Approach 2 Using vinyl-source-stream

gulp.task('browserify', function() {
  var bundleStream = browserify('index.js').bundle();

  bundleStream
    .pipe(source('index.js'))
    .pipe(streamify(uglify()))
    .pipe(gulp.dest('./bundle.js'));
});

One benefit of the second approach is that it uses the Browserify API directly, meaning that you don't have to wait for the authors of gulpify to update the library before you can.

Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
  • "gulpify has been deprecated in favor of using "browserify" directly in combination with the "vinyl-source-stream" module, or "gulp-browserify" if you would like to use a plugin" Source: https://www.npmjs.org/package/gulpify Also note that "gulp-browserify" is on the blaclist. – ZeeCoder Dec 01 '14 at 10:36
  • @ZeeCoder what do you mean by blacklist? – Giszmo Jun 05 '15 at 20:53
  • 4
    @Giszmo I mean this: https://github.com/gulpjs/plugins/blob/master/src/blackList.json – ZeeCoder Jun 06 '15 at 23:05
  • The second one was useful for me. Works like a charm. :) Tks! – dnvtrn Dec 28 '16 at 16:39
3

you may try browserify transform uglifyify.

H23120
  • 317
  • 4
  • 11
  • 3
    Important notes, it seems uglifyify is unmaintained and stuck on Uglify v1 which is deprecated. – user157251 Jan 13 '15 at 21:19
  • 2
    Uglifyify currently uses Uglify v2. Maybe it is not updated that frequently, but that does not seem to be necessary as it just depends on the uglify-js package. – inta Jul 09 '15 at 21:16