Skip to content

Instantly share code, notes, and snippets.

@skrajewski
Created March 29, 2016 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrajewski/0aeb982777bfe8f6489c to your computer and use it in GitHub Desktop.
Save skrajewski/0aeb982777bfe8f6489c to your computer and use it in GitHub Desktop.
Gulpfile.js with browserify and external vendor file
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var watchify = require('watchify');
var babelify = require('babelify');
var assign = require('lodash.assign');
var gutil = require('gulp-util');
var chalk = require('chalk');
var size = require('gulp-size');
var through = require('through');
const VENDORS = ['angular', 'jquery', 'bootstrap'];
function compile(watch) {
var browserifyOptions = {
entries: ['./build/src/index.js'],
debug: true,
external: VENDORS,
bundleExternal: false
};
var options = assign({}, watchify.args, browserifyOptions);
var bundler = browserify(options)
.transform(babelify, {presets: ["es2015"]})
.transform({global: true}, 'browserify-shim')
function rebundle() {
bundler.bundle()
.on('error', function (error) {
console.error(error.message);
this.emit('end');
})
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./maps'))
.pipe(size({showFiles: true}))
.pipe(gulp.dest('./dist/src'));
}
if (watch) {
bundler = watchify(bundler);
bundler.on('update', rebundle);
}
bundler.on('log', function (log) {
gutil.log(chalk.green('Browserify') + ': ' + log);
});
return rebundle();
}
gulp.task('build:app', function () {
return compile();
});
gulp.task('watch', function () {
return compile(true);
});
gulp.task('less', function () {
return gulp.src('./build/less/main.less')
});
gulp.task('build:vendor', function () {
var bundle = browserify({
debug: true
});
bundle.transform(function(file) {
if (file.indexOf('bootstrap') === -1) {
return through();
}
var data = '';
return through(write, end);
function write (buf) { data += buf }
function end () {
data = "var jQuery = require('jquery');\n\n" + data;
this.queue(data);
this.queue(null);
}
}, {global: true});
VENDORS.forEach(function (lib) {
bundle.require(lib);
});
return bundle.bundle()
.pipe(source('vendor.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./maps'))
.pipe(size({showFiles: true}))
.pipe(gulp.dest('./dist/src/'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment