stackviz/gulp/tasks/browserify.js
Tim Buckley e631b8c62c Fix sourcemap generation
This fixes slightly broken sourcemap generation, and fully enables
sourcemaps for all builds. Previously, browserify would generate
its own sourcemaps and embed them directly in 'main.js' which often
confused Chrome. Additionally, partial sourcemaps were written to
'main.js.map' during production builds, but these did not include
browserify's inline sourcemaps which mapped library sources, causing
even more confusion for Chrome. This standardizes sourcemaps so they
are all written to a single output file, and generated regardless of
build type.

Change-Id: I1e676cae27082cb81d808d78fd600f09d6449498
2016-03-16 15:15:47 -06:00

75 lines
1.9 KiB
JavaScript

'use strict';
var config = require('../config');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var buffer = require('vinyl-buffer');
var streamify = require('gulp-streamify');
var watchify = require('watchify');
var browserify = require('browserify');
var babelify = require('babelify');
var uglify = require('gulp-uglify');
var handleErrors = require('../util/handleErrors');
var browserSync = require('browser-sync');
var ngAnnotate = require('browserify-ngannotate');
// Based on: http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
function buildScript(file) {
var bundler = browserify({
entries: config.browserify.entries,
debug: true,
cache: {},
packageCache: {},
fullPaths: true
}, watchify.args);
if ( !global.isProd ) {
bundler = watchify(bundler);
bundler.on('update', function() {
rebundle();
});
}
var transforms = [
babelify,
ngAnnotate,
'brfs',
'bulkify'
];
transforms.forEach(function(transform) {
bundler.transform(transform);
});
function rebundle() {
var stream = bundler.bundle();
var createSourcemap = config.browserify.sourcemap;
gutil.log('Rebundle...');
return stream.on('error', handleErrors)
.pipe(source(file))
.pipe(gulpif(createSourcemap, buffer()))
.pipe(gulpif(createSourcemap, sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(global.isProd, streamify(uglify({
compress: { drop_console: true }
}))))
.pipe(gulpif(createSourcemap, sourcemaps.write('./')))
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.reload({ stream: true, once: true }));
}
return rebundle();
}
gulp.task('browserify', function() {
return buildScript('main.js');
});