Chain Gulp glob to browserify transform

这一生的挚爱 提交于 2019-12-04 09:55:57

Use through2 to make a one-off custom plugin stream that does all of the dirty work.

Unfortanately vinyl-transform and vinyl-source-stream and the solutions that go along with those have flaws so we have to go for something custom.

var gulp = require('gulp');
var through = require('through2');
var browserify = require('browserify');

gulp.task('bundle', function() {
    var browserified = function() {
        return through.obj(function(chunk, enc, callback) {
            if(chunk.isBuffer()) {
                var b = browserify(chunk.path);
                    // Any custom browserify stuff should go here
                    //.transform(to5browserify);

                chunk.contents = b.bundle();

                this.push(chunk);

            }
            callback();
        });
    };

    return gulp.src(['./src/**/*.js'])
        .pipe(browserified())
        .pipe(gulp.dest('dest'));
});

You can specify globs in your BUNDLES array as well as exclude any files:

var BUNDLES = [
    "app/**/*.js",
    "export.js",
    "app/modules/**/*.js",
    "!app/modules/excluded/*.js"
];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!