Skip to content

Commit 90ac47b

Browse files
author
benholloway
committed
can now debug minified
1 parent 5fbb494 commit 90ac47b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

lib/build/esmangleify.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict';
2+
3+
var codegen = require('escodegen'),
4+
esprima = require('esprima'),
5+
through = require('through2'),
6+
convert = require('convert-source-map'),
7+
sourceMapToAst = require('sourcemap-to-ast'),
8+
esmangle = require('esmangle'),
9+
merge = require('lodash.merge');
10+
11+
/**
12+
* Esprima based minifier transform for browserify
13+
* @see http://sokra.github.io/source-map-visualization
14+
*/
15+
function esmangleify(opt) {
16+
17+
// options is the escodegen format
18+
var format = merge({
19+
renumber : true,
20+
hexadecimal: true,
21+
escapeless : true,
22+
compact : true,
23+
semicolons : false,
24+
parentheses: false
25+
}, opt);
26+
27+
// transform
28+
return function browserifyTransform(file) {
29+
var buffer = [];
30+
return through(transfrom, flush);
31+
32+
function transfrom(data, encoding, done) {
33+
/* jshint validthis:true */
34+
buffer.push(data);
35+
done();
36+
}
37+
38+
function flush(done) {
39+
/* jshint validthis:true */
40+
var content = buffer.join('');
41+
42+
// parse code to AST using esprima
43+
var ast;
44+
try {
45+
ast = esprima.parse(content, {
46+
loc : true,
47+
source: file
48+
});
49+
} catch(e) {
50+
return done(e);
51+
}
52+
53+
// make sure the AST has the data from the original source map
54+
var converter = convert.fromSource(content);
55+
var originalMap = converter && converter.toObject();
56+
var sourceContent = content;
57+
if (originalMap) {
58+
sourceMapToAst(ast, originalMap);
59+
sourceContent = originalMap.sourcesContent[0];
60+
}
61+
62+
// mangle the AST
63+
var updated = esmangle.mangle(ast);
64+
65+
// generate compressed code from the AST
66+
var pair = codegen.generate(updated, {
67+
sourceMap : true,
68+
sourceMapWithCode: true,
69+
format : format
70+
});
71+
72+
// ensure that the source map has sourcesContent or browserify will not work
73+
pair.map.setSourceContent(file, sourceContent);
74+
var mapComment = convert.fromJSON(pair.map.toString()).toComment();
75+
76+
// push to the output
77+
this.push(new Buffer(pair.code + mapComment));
78+
done();
79+
}
80+
};
81+
}
82+
83+
module.exports = esmangleify;

0 commit comments

Comments
 (0)