/
terser.js
67 lines (58 loc) 路 1.45 KB
/
terser.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
const {minify} = require('terser');
const SourceMap = require('../SourceMap');
module.exports = async function(asset) {
await asset.parseIfNeeded();
// Convert AST into JS
let source = (await asset.generate()).js;
let customConfig = await asset.getConfig(['.uglifyrc', '.terserrc']);
let options = {
warnings: true,
safari10: true,
mangle: {
toplevel: !asset.options.scopeHoist
}
};
let sourceMap;
if (asset.options.sourceMaps) {
sourceMap = new SourceMap();
options.output = {
source_map: {
add(source, gen_line, gen_col, orig_line, orig_col, name) {
sourceMap.addMapping({
source,
name,
original: {
line: orig_line,
column: orig_col
},
generated: {
line: gen_line,
column: gen_col
}
});
}
}
};
}
if (customConfig) {
options = Object.assign(options, customConfig);
}
let result = minify(source, options);
if (result.error) {
throw result.error;
}
if (sourceMap) {
if (asset.sourceMap) {
asset.sourceMap = await new SourceMap().extendSourceMap(
asset.sourceMap,
sourceMap
);
} else {
asset.sourceMap = sourceMap;
}
}
// babel-generator did our code generation for us, so remove the old AST
asset.ast = null;
asset.outputCode = result.code;
asset.isAstDirty = false;
};