-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
67 lines (54 loc) · 1.99 KB
/
index.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
59
60
61
62
63
64
65
66
67
var UglifyJS = require("uglify-js");
var loaderUtils = require('loader-utils');
var sourceMap = require('source-map');
function mergeSourceMap(map, inputMap) {
var inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
var outputMapConsumer = new sourceMap.SourceMapConsumer(map);
var mergedGenerator = new sourceMap.SourceMapGenerator({
file: inputMapConsumer.file,
sourceRoot: inputMapConsumer.sourceRoot
});
var source = outputMapConsumer.sources[0];
inputMapConsumer.eachMapping(function (mapping) {
var generatedPosition = outputMapConsumer.generatedPositionFor({
line: mapping.generatedLine,
column: mapping.generatedColumn,
source: source
});
if (generatedPosition.column != null) {
mergedGenerator.addMapping({
source: mapping.source,
original: mapping.source == null ? null : {
line: mapping.originalLine,
column: mapping.originalColumn
},
generated: generatedPosition
});
}
});
var mergedMap = mergedGenerator.toJSON();
inputMap.mappings = mergedMap.mappings;
return inputMap
};
module.exports = function(source, inputSourceMap) {
var callback = this.async();
if (this.cacheable) {
this.cacheable();
}
var opts = loaderUtils.getOptions(this) || {};
// just an indicator to generate source maps, the output result.map will be modified anyway
// tell UglifyJS2 not to emit a name by just setting outSourceMap to true
opts.sourceMap = true;
var result = UglifyJS.minify(source, opts);
var sourceMap = JSON.parse(result.map);
if (inputSourceMap) {
callback(null, result.code, mergeSourceMap(sourceMap, inputSourceMap));
} else {
var sourceFilename = loaderUtils.getRemainingRequest(this);
var current = loaderUtils.getCurrentRequest(this);
sourceMap.sources = [sourceFilename];
sourceMap.file = current;
sourceMap.sourcesContent = [source];
callback(null, result.code, sourceMap);
}
};