@parcel/optimizer-terser 2.0.0-nightly.92 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/TerserOptimizer.js +176 -61
- package/package.json +13 -8
- package/src/TerserOptimizer.js +103 -55
package/lib/TerserOptimizer.js
CHANGED
|
@@ -5,99 +5,214 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
function _nullthrows() {
|
|
9
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
_nullthrows = function () {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
function _terser() {
|
|
19
|
+
const data = require("terser");
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
_terser = function () {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _plugin() {
|
|
29
|
+
const data = require("@parcel/plugin");
|
|
30
|
+
|
|
31
|
+
_plugin = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function _utils() {
|
|
39
|
+
const data = require("@parcel/utils");
|
|
40
|
+
|
|
41
|
+
_utils = function () {
|
|
42
|
+
return data;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function _sourceMap() {
|
|
49
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
50
|
+
|
|
51
|
+
_sourceMap = function () {
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function _diagnostic() {
|
|
59
|
+
const data = _interopRequireWildcard(require("@parcel/diagnostic"));
|
|
60
|
+
|
|
61
|
+
_diagnostic = function () {
|
|
62
|
+
return data;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function _path() {
|
|
69
|
+
const data = _interopRequireDefault(require("path"));
|
|
70
|
+
|
|
71
|
+
_path = function () {
|
|
72
|
+
return data;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return data;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
79
|
+
|
|
80
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
81
|
|
|
20
82
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
83
|
|
|
22
|
-
|
|
84
|
+
var _default = new (_plugin().Optimizer)({
|
|
85
|
+
async loadConfig({
|
|
86
|
+
config,
|
|
87
|
+
options
|
|
88
|
+
}) {
|
|
89
|
+
let userConfig = await config.getConfigFrom(_path().default.join(options.projectRoot, 'index'), ['.terserrc', '.terserrc.js']);
|
|
23
90
|
|
|
24
|
-
|
|
91
|
+
if (userConfig) {
|
|
92
|
+
let isJavascript = _path().default.extname(userConfig.filePath) === '.js';
|
|
25
93
|
|
|
26
|
-
|
|
94
|
+
if (isJavascript) {
|
|
95
|
+
config.invalidateOnStartup();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return userConfig === null || userConfig === void 0 ? void 0 : userConfig.contents;
|
|
100
|
+
},
|
|
27
101
|
|
|
28
|
-
var _default = new _plugin.Optimizer({
|
|
29
102
|
async optimize({
|
|
30
103
|
contents,
|
|
31
104
|
map,
|
|
32
105
|
bundle,
|
|
33
|
-
|
|
106
|
+
config: userConfig,
|
|
107
|
+
options,
|
|
108
|
+
getSourceMapReference
|
|
34
109
|
}) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!options.minify) {
|
|
110
|
+
if (!bundle.env.shouldOptimize) {
|
|
38
111
|
return {
|
|
39
112
|
contents,
|
|
40
113
|
map
|
|
41
114
|
};
|
|
42
115
|
}
|
|
43
116
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
compress: _objectSpread({}, userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$config = userConfig.config) === null || _userConfig$config === void 0 ? void 0 : _userConfig$config.compress, {
|
|
54
|
-
toplevel: bundle.env.outputFormat === 'esmodule' || bundle.env.outputFormat === 'commonjs'
|
|
55
|
-
}),
|
|
56
|
-
sourceMap: {
|
|
57
|
-
filename: _path.default.relative(options.projectRoot, bundle.filePath)
|
|
58
|
-
},
|
|
117
|
+
let code = await (0, _utils().blobToString)(contents);
|
|
118
|
+
let originalMap = map ? await map.stringify({}) : null;
|
|
119
|
+
let config = { ...userConfig,
|
|
120
|
+
sourceMap: bundle.env.sourceMap ? {
|
|
121
|
+
filename: _path().default.relative(options.projectRoot, _path().default.join(bundle.target.distDir, bundle.name)),
|
|
122
|
+
asObject: true,
|
|
123
|
+
content: originalMap
|
|
124
|
+
} : false,
|
|
125
|
+
toplevel: bundle.env.outputFormat === 'esmodule' || bundle.env.outputFormat === 'commonjs',
|
|
59
126
|
module: bundle.env.outputFormat === 'esmodule'
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
127
|
+
};
|
|
128
|
+
let result;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
result = await (0, _terser().minify)(code, config);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
// $FlowFixMe
|
|
134
|
+
let {
|
|
135
|
+
message,
|
|
136
|
+
line,
|
|
137
|
+
col
|
|
138
|
+
} = error;
|
|
139
|
+
|
|
140
|
+
if (line != null && col != null) {
|
|
141
|
+
message = (0, _diagnostic().escapeMarkdown)(message);
|
|
142
|
+
let diagnostics = [];
|
|
143
|
+
let mapping = map === null || map === void 0 ? void 0 : map.findClosestMapping(line, col);
|
|
144
|
+
|
|
145
|
+
if (mapping && mapping.original && mapping.source) {
|
|
146
|
+
let {
|
|
147
|
+
source,
|
|
148
|
+
original
|
|
149
|
+
} = mapping;
|
|
150
|
+
|
|
151
|
+
let filePath = _path().default.resolve(options.projectRoot, source);
|
|
152
|
+
|
|
153
|
+
diagnostics.push({
|
|
154
|
+
message,
|
|
155
|
+
origin: '@parcel/optimizer-terser',
|
|
156
|
+
codeFrames: [{
|
|
157
|
+
language: 'js',
|
|
158
|
+
filePath,
|
|
159
|
+
code: await options.inputFS.readFile(filePath, 'utf8'),
|
|
160
|
+
codeHighlights: [{
|
|
161
|
+
message,
|
|
162
|
+
start: original,
|
|
163
|
+
end: original
|
|
164
|
+
}]
|
|
165
|
+
}],
|
|
166
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."]
|
|
167
|
+
});
|
|
168
|
+
}
|
|
84
169
|
|
|
170
|
+
if (diagnostics.length === 0 || options.logLevel === 'verbose') {
|
|
171
|
+
let loc = {
|
|
172
|
+
line: line,
|
|
173
|
+
column: col
|
|
174
|
+
};
|
|
175
|
+
diagnostics.push({
|
|
176
|
+
message,
|
|
177
|
+
origin: '@parcel/optimizer-terser',
|
|
178
|
+
codeFrames: [{
|
|
179
|
+
language: 'js',
|
|
180
|
+
filePath: undefined,
|
|
181
|
+
code,
|
|
182
|
+
codeHighlights: [{
|
|
183
|
+
message,
|
|
184
|
+
start: loc,
|
|
185
|
+
end: loc
|
|
186
|
+
}]
|
|
187
|
+
}],
|
|
188
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."]
|
|
189
|
+
});
|
|
85
190
|
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
191
|
|
|
89
|
-
|
|
90
|
-
|
|
192
|
+
throw new (_diagnostic().default)({
|
|
193
|
+
diagnostic: diagnostics
|
|
194
|
+
});
|
|
195
|
+
} else {
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
91
198
|
}
|
|
92
199
|
|
|
93
|
-
let
|
|
200
|
+
let sourceMap = null;
|
|
201
|
+
let minifiedContents = (0, _nullthrows().default)(result.code);
|
|
202
|
+
let resultMap = result.map;
|
|
203
|
+
|
|
204
|
+
if (resultMap && typeof resultMap !== 'string') {
|
|
205
|
+
sourceMap = new (_sourceMap().default)(options.projectRoot);
|
|
206
|
+
sourceMap.addVLQMap(resultMap);
|
|
207
|
+
let sourcemapReference = await getSourceMapReference(sourceMap);
|
|
94
208
|
|
|
95
|
-
|
|
96
|
-
|
|
209
|
+
if (sourcemapReference) {
|
|
210
|
+
minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
|
|
211
|
+
}
|
|
97
212
|
}
|
|
98
213
|
|
|
99
214
|
return {
|
|
100
|
-
contents:
|
|
215
|
+
contents: minifiedContents,
|
|
101
216
|
map: sourceMap
|
|
102
217
|
};
|
|
103
218
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/optimizer-terser",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
+
"funding": {
|
|
9
|
+
"type": "opencollective",
|
|
10
|
+
"url": "https://opencollective.com/parcel"
|
|
11
|
+
},
|
|
8
12
|
"repository": {
|
|
9
13
|
"type": "git",
|
|
10
14
|
"url": "https://github.com/parcel-bundler/parcel.git"
|
|
@@ -12,15 +16,16 @@
|
|
|
12
16
|
"main": "lib/TerserOptimizer.js",
|
|
13
17
|
"source": "src/TerserOptimizer.js",
|
|
14
18
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
16
|
-
"parcel": "^2.0.
|
|
19
|
+
"node": ">= 12.0.0",
|
|
20
|
+
"parcel": "^2.0.1"
|
|
17
21
|
},
|
|
18
22
|
"dependencies": {
|
|
19
|
-
"@parcel/
|
|
20
|
-
"@parcel/
|
|
21
|
-
"@parcel/
|
|
23
|
+
"@parcel/diagnostic": "^2.0.1",
|
|
24
|
+
"@parcel/plugin": "^2.0.1",
|
|
25
|
+
"@parcel/source-map": "^2.0.0",
|
|
26
|
+
"@parcel/utils": "^2.0.1",
|
|
22
27
|
"nullthrows": "^1.1.1",
|
|
23
|
-
"terser": "^
|
|
28
|
+
"terser": "^5.2.0"
|
|
24
29
|
},
|
|
25
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "28b47e6bdca7de2a06b7cc39a4a0b1df89f3fe15"
|
|
26
31
|
}
|
package/src/TerserOptimizer.js
CHANGED
|
@@ -3,78 +3,126 @@
|
|
|
3
3
|
import nullthrows from 'nullthrows';
|
|
4
4
|
import {minify} from 'terser';
|
|
5
5
|
import {Optimizer} from '@parcel/plugin';
|
|
6
|
-
import {
|
|
6
|
+
import {blobToString} from '@parcel/utils';
|
|
7
7
|
import SourceMap from '@parcel/source-map';
|
|
8
|
+
import ThrowableDiagnostic, {escapeMarkdown} from '@parcel/diagnostic';
|
|
9
|
+
|
|
8
10
|
import path from 'path';
|
|
9
11
|
|
|
10
|
-
export default new Optimizer({
|
|
11
|
-
async
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
export default (new Optimizer({
|
|
13
|
+
async loadConfig({config, options}) {
|
|
14
|
+
let userConfig = await config.getConfigFrom(
|
|
15
|
+
path.join(options.projectRoot, 'index'),
|
|
16
|
+
['.terserrc', '.terserrc.js'],
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
if (userConfig) {
|
|
20
|
+
let isJavascript = path.extname(userConfig.filePath) === '.js';
|
|
21
|
+
if (isJavascript) {
|
|
22
|
+
config.invalidateOnStartup();
|
|
23
|
+
}
|
|
14
24
|
}
|
|
15
25
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
26
|
+
return userConfig?.contents;
|
|
27
|
+
},
|
|
28
|
+
async optimize({
|
|
29
|
+
contents,
|
|
30
|
+
map,
|
|
31
|
+
bundle,
|
|
32
|
+
config: userConfig,
|
|
33
|
+
options,
|
|
34
|
+
getSourceMapReference,
|
|
35
|
+
}) {
|
|
36
|
+
if (!bundle.env.shouldOptimize) {
|
|
37
|
+
return {contents, map};
|
|
20
38
|
}
|
|
21
39
|
|
|
22
|
-
let
|
|
23
|
-
options.inputFS,
|
|
24
|
-
path.join(options.projectRoot, 'index'),
|
|
25
|
-
['.terserrc', '.uglifyrc', '.uglifyrc.js', '.terserrc.js'],
|
|
26
|
-
);
|
|
40
|
+
let code = await blobToString(contents);
|
|
27
41
|
|
|
42
|
+
let originalMap = map ? await map.stringify({}) : null;
|
|
28
43
|
let config = {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
...userConfig,
|
|
45
|
+
sourceMap: bundle.env.sourceMap
|
|
46
|
+
? {
|
|
47
|
+
filename: path.relative(
|
|
48
|
+
options.projectRoot,
|
|
49
|
+
path.join(bundle.target.distDir, bundle.name),
|
|
50
|
+
),
|
|
51
|
+
asObject: true,
|
|
52
|
+
content: originalMap,
|
|
53
|
+
}
|
|
54
|
+
: false,
|
|
55
|
+
toplevel:
|
|
56
|
+
bundle.env.outputFormat === 'esmodule' ||
|
|
57
|
+
bundle.env.outputFormat === 'commonjs',
|
|
40
58
|
module: bundle.env.outputFormat === 'esmodule',
|
|
41
59
|
};
|
|
42
60
|
|
|
43
|
-
let
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
let result;
|
|
62
|
+
try {
|
|
63
|
+
result = await minify(code, config);
|
|
64
|
+
} catch (error) {
|
|
46
65
|
// $FlowFixMe
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
66
|
+
let {message, line, col} = error;
|
|
67
|
+
if (line != null && col != null) {
|
|
68
|
+
message = escapeMarkdown(message);
|
|
69
|
+
let diagnostics = [];
|
|
70
|
+
let mapping = map?.findClosestMapping(line, col);
|
|
71
|
+
if (mapping && mapping.original && mapping.source) {
|
|
72
|
+
let {source, original} = mapping;
|
|
73
|
+
let filePath = path.resolve(options.projectRoot, source);
|
|
74
|
+
diagnostics.push({
|
|
75
|
+
message,
|
|
76
|
+
origin: '@parcel/optimizer-terser',
|
|
77
|
+
codeFrames: [
|
|
78
|
+
{
|
|
79
|
+
language: 'js',
|
|
80
|
+
filePath,
|
|
81
|
+
code: await options.inputFS.readFile(filePath, 'utf8'),
|
|
82
|
+
codeHighlights: [{message, start: original, end: original}],
|
|
57
83
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
});
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
}
|
|
84
|
+
],
|
|
85
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
67
88
|
|
|
68
|
-
|
|
69
|
-
|
|
89
|
+
if (diagnostics.length === 0 || options.logLevel === 'verbose') {
|
|
90
|
+
let loc = {
|
|
91
|
+
line: line,
|
|
92
|
+
column: col,
|
|
93
|
+
};
|
|
94
|
+
diagnostics.push({
|
|
95
|
+
message,
|
|
96
|
+
origin: '@parcel/optimizer-terser',
|
|
97
|
+
codeFrames: [
|
|
98
|
+
{
|
|
99
|
+
language: 'js',
|
|
100
|
+
filePath: undefined,
|
|
101
|
+
code,
|
|
102
|
+
codeHighlights: [{message, start: loc, end: loc}],
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."],
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
throw new ThrowableDiagnostic({diagnostic: diagnostics});
|
|
109
|
+
} else {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
70
112
|
}
|
|
71
113
|
|
|
72
|
-
let
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
114
|
+
let sourceMap = null;
|
|
115
|
+
let minifiedContents: string = nullthrows(result.code);
|
|
116
|
+
let resultMap = result.map;
|
|
117
|
+
if (resultMap && typeof resultMap !== 'string') {
|
|
118
|
+
sourceMap = new SourceMap(options.projectRoot);
|
|
119
|
+
sourceMap.addVLQMap(resultMap);
|
|
120
|
+
let sourcemapReference = await getSourceMapReference(sourceMap);
|
|
121
|
+
if (sourcemapReference) {
|
|
122
|
+
minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
|
|
123
|
+
}
|
|
76
124
|
}
|
|
77
125
|
|
|
78
|
-
return {contents:
|
|
126
|
+
return {contents: minifiedContents, map: sourceMap};
|
|
79
127
|
},
|
|
80
|
-
});
|
|
128
|
+
}): Optimizer);
|