Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Created February 22, 2015 17:37
Show Gist options
  • Save cecilemuller/de44435d8d1b4d30c5de to your computer and use it in GitHub Desktop.
Save cecilemuller/de44435d8d1b4d30c5de to your computer and use it in GitHub Desktop.
Minified Browserify bundle with sourcemap
var browserify = require('browserify');
var uglify = require('uglify-js');
var extract = require('convert-source-map');
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
browserify(
'./src/index.js',
{
debug: true,
basedir: base
}
)
.bundle(function(err, compiled){
if (!err){
var browserified = compiled.toString('utf8');
var sourcemap1 = extract.fromSource(browserified).toJSON();
browserified = extract.removeComments(browserified);
var minified = uglify.minify(browserified, {
fromString: true,
outSourceMap: 'index.js.map'
});
var sourcemap2 = minified.map;
var combined = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(JSON.stringify(sourcemap1)));
combined.applySourceMap(new SourceMapConsumer(JSON.stringify(sourcemap2)));
var sourcemap = JSON.stringify(combined.toJSON());
// Resulting file contents
console.log(minified.code); //index.js
console.log(sourcemap); //index.js.map
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment