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