Skip to content

Instantly share code, notes, and snippets.

@raghur
Created April 22, 2015 16:03
Show Gist options
  • Save raghur/734447b813e66113d7f9 to your computer and use it in GitHub Desktop.
Save raghur/734447b813e66113d7f9 to your computer and use it in GitHub Desktop.
Weird sourcemap behavior
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var watchify = require('watchify');
var notify = require("gulp-notify");
var buffer = require('vinyl-buffer');
var globby = require('globby');
var source = require('vinyl-source-stream');
var sourcemaps= require('gulp-sourcemaps');
var through= require('through2');
var reactify = require('reactify');
var uglify = require('gulp-uglify');
var ifelse = require('gulp-if-else');
var less = require('gulp-less');
var autoprefixer = require('gulp-autoprefixer')
//var bundleLogger = require('../util/bundleLogger');
//var handleErrors = require('../util/handleErrors');
var config = {
// Enable source maps
debug: true,
extensions: ['.jsx'],
// A separate bundle will be generated for each
// bundle config in the list below
bundleConfigs: [ 'app'],
srcFolder: './Scripts/Jsx/',
destFolder: './Scripts/'
};
var handleErrors = function () {
var args = Array.prototype.slice.call(arguments);
// Send error to notification center with gulp-notify
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
// Keep gulp from hanging on this task
this.emit('end');
};
function doBrowserify(callback) {
var bundleQueue = config.bundleConfigs.length;
var browserifyThis = function (entry) {
var reportFinished = function () {
// Log when bundling completes
//bundleLogger.end(bundleConfig.outputName);
//console.log(bundleConfig.outputName);
if (bundleQueue) {
bundleQueue--;
if (bundleQueue === 0) {
// If queue is empty, tell gulp the task is complete.
// https://github.com/gulpjs/gulp/blob/master/docs/API.md#accept-a-callback
callback();
}
}
};
var minify = function() {
var stream = through({objectMode:true})
stream
.pipe(sourcemaps.init().on('error', gutil.log))
.pipe(uglify())
.pipe(sourcemaps.write("./"));
return stream;
};
var bundledStream = through({objectMode:true});
bundledStream
// Report compile errors
.on('error', handleErrors)
.on('end', reportFinished)
// Use vinyl-source-stream to make the
// stream gulp compatible. Specifiy the
// desired output filename here.
.pipe(source(entry + '.js'))
// Specify the output destination
.pipe(buffer())
.pipe(ifelse(!config.debug,function() {return sourcemaps.init()}))
.pipe(ifelse(!config.debug, uglify))
.pipe(ifelse(!config.debug,function() {return sourcemaps.write("./")}))
//.pipe(ifelse(!config.debug, minify))
.pipe(gulp.dest(config.destFolder));
globby([config.srcFolder + "main.js"], function(err, entries) {
// ensure any errors from globby are handled
if (err) {
bundledStream.emit('error', err);
return;
}
// create the Browserify instance.
var b = browserify({
entries: entries,
//basedir: config.srcFolder,
debug: true,
transform: [reactify]
});
// pipe the Browserify stream into the stream we created earlier
// this starts our gulp pipeline.
b
.exclude('react')
.exclude('d3')
.bundle()
.pipe(bundledStream);
});
return bundledStream;
};
// Start bundling with Browserify for each bundleConfig specified
config.bundleConfigs.forEach(browserifyThis);
}
gulp.task('browserify', doBrowserify);
gulp.task('watch', function () {
gulp.watch(['./Scripts/Jsx/**/*.jsx', 'Scripts/Jsx/*.js'], ['browserify'], function () {
});
});
gulp.task('default', ['browserify'], function () {
});
gulp.task('release', function (callback) {
config.debug=false;
return doBrowserify(callback);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment