-
Notifications
You must be signed in to change notification settings - Fork 1
/
js-process.js
58 lines (50 loc) · 1.45 KB
/
js-process.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var fs = require('fs');
var babel = require('babel-core');
function generateSourceMap(map) {
function toBase64(string) {
return new Buffer(string).toString('base64');
}
var mapStr = JSON.stringify(map);
// align `?instr` in filename to fold 4
mapStr = mapStr.replace(/(,"sources":\[)("[^"]+")/, function(m, prefix, filename) {
while (filename.length % 3 != 1)
filename = ' ' + filename;
return prefix + filename;
});
// return comment for inject to js
return (
'\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,' +
toBase64(mapStr)
);
}
module.exports = function(options) {
var registratorName = options.registratorName;
return function instrumentCode(content, filename, cb) {
content = String(content || '');
filename = filename || 'unknown';
try {
var instrumentedCode = babel.transform(content, {
filename: filename,
sourceMaps: true,
sourceFileName: filename + '?instr',
plugins: [
require('babel-plugin-source-wrapper').configure({
registratorName: registratorName,
blackbox: options.blackbox
})
]
});
cb(null, instrumentedCode.code +
generateSourceMap({
version: instrumentedCode.map.version,
sections: [{
offset: { line: 0, column: 0 },
map: instrumentedCode.map
}]
})
);
} catch(e) {
cb(e);
}
}
};