-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathTerserOptimizer.js
109 lines (98 loc) · 3.29 KB
/
TerserOptimizer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// @flow
import nullthrows from 'nullthrows';
import {minify} from 'terser';
import {Optimizer} from '@parcel/plugin';
import {blobToString, loadConfig} from '@parcel/utils';
import SourceMap from '@parcel/source-map';
import ThrowableDiagnostic from '@parcel/diagnostic';
import path from 'path';
export default new Optimizer({
async optimize({contents, map, bundle, options, getSourceMapReference}) {
if (!bundle.env.minify) {
return {contents, map};
}
let code = await blobToString(contents);
let userConfig = await loadConfig(
options.inputFS,
path.join(options.projectRoot, 'index'),
['.terserrc', '.uglifyrc', '.uglifyrc.js', '.terserrc.js'],
);
let originalMap = map ? await map.stringify({}) : null;
let config = {
...userConfig?.config,
compress: {
...userConfig?.config?.compress,
toplevel:
bundle.env.outputFormat === 'esmodule' ||
bundle.env.outputFormat === 'commonjs',
},
sourceMap: options.sourceMaps
? {
filename: path.relative(options.projectRoot, bundle.filePath),
asObject: true,
content: originalMap,
}
: false,
module: bundle.env.outputFormat === 'esmodule',
};
let result = minify(code, config);
if (result.error) {
// $FlowFixMe
let {message, line, col} = result.error;
if (line != null && col != null) {
let diagnostic = [];
let mapping = map?.findClosestMapping(line, col);
if (mapping && mapping.original && mapping.source) {
let {source, original} = mapping;
let filePath = path.resolve(options.projectRoot, source);
diagnostic.push({
message,
origin: '@parcel/optimizer-terser',
language: 'js',
filePath,
codeFrame: {
code: await options.inputFS.readFile(filePath, 'utf8'),
codeHighlights: [{message, start: original, end: original}],
},
hints: ["It's likely that Terser doesn't support this syntax yet."],
});
}
if (diagnostic.length === 0 || options.logLevel === 'verbose') {
let loc = {
line: line,
column: col,
};
diagnostic.push({
message,
origin: '@parcel/optimizer-terser',
language: 'js',
filePath: undefined,
codeFrame: {
code,
codeHighlights: [{message, start: loc, end: loc}],
},
hints: ["It's likely that Terser doesn't support this syntax yet."],
});
}
throw new ThrowableDiagnostic({diagnostic});
} else {
throw result.error;
}
}
let sourceMap = null;
let minifiedContents: string = nullthrows(result.code);
if (result.map && typeof result.map !== 'string') {
sourceMap = new SourceMap();
sourceMap.addRawMappings(
result.map.mappings,
result.map.sources,
result.map.names || [],
);
let sourcemapReference = await getSourceMapReference(sourceMap);
if (sourcemapReference) {
minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
}
}
return {contents: minifiedContents, map: sourceMap};
},
});