Created
February 22, 2015 17:37
-
-
Save cecilemuller/de44435d8d1b4d30c5de to your computer and use it in GitHub Desktop.
Minified Browserify bundle with sourcemap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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