How to uglify output with Browserify in Gulp?

前端 未结 3 778
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 18:41

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

gulpfile.js

var browserify = require(\'browserify\');
var gulp =          


        
3条回答
  •  野性不改
    2021-01-29 19:10

    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.

提交回复
热议问题