@parcel/optimizer-terser 2.0.0-nightly.149 → 2.0.0-nightly.1491
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 +148 -82
- package/package.json +13 -8
- package/src/TerserOptimizer.js +97 -56
package/lib/TerserOptimizer.js
CHANGED
|
@@ -4,104 +4,170 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
function _nullthrows() {
|
|
8
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
9
|
+
_nullthrows = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _terser() {
|
|
15
|
+
const data = require("terser");
|
|
16
|
+
_terser = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _plugin() {
|
|
22
|
+
const data = require("@parcel/plugin");
|
|
23
|
+
_plugin = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _utils() {
|
|
29
|
+
const data = require("@parcel/utils");
|
|
30
|
+
_utils = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _sourceMap() {
|
|
36
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
37
|
+
_sourceMap = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _diagnostic() {
|
|
43
|
+
const data = _interopRequireWildcard(require("@parcel/diagnostic"));
|
|
44
|
+
_diagnostic = function () {
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
function _path() {
|
|
50
|
+
const data = _interopRequireDefault(require("path"));
|
|
51
|
+
_path = function () {
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
57
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
20
58
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
59
|
+
var _default = exports.default = new (_plugin().Optimizer)({
|
|
60
|
+
async loadConfig({
|
|
61
|
+
config,
|
|
62
|
+
options
|
|
63
|
+
}) {
|
|
64
|
+
let userConfig = await config.getConfigFrom(_path().default.join(options.projectRoot, 'index'), ['.terserrc', '.terserrc.js', '.terserrc.cjs', '.terserrc.mjs']);
|
|
65
|
+
return userConfig === null || userConfig === void 0 ? void 0 : userConfig.contents;
|
|
66
|
+
},
|
|
29
67
|
async optimize({
|
|
30
68
|
contents,
|
|
31
69
|
map,
|
|
32
70
|
bundle,
|
|
33
|
-
|
|
71
|
+
config: userConfig,
|
|
72
|
+
options,
|
|
73
|
+
getSourceMapReference
|
|
34
74
|
}) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!bundle.env.minify) {
|
|
75
|
+
if (!bundle.env.shouldOptimize) {
|
|
38
76
|
return {
|
|
39
77
|
contents,
|
|
40
78
|
map
|
|
41
79
|
};
|
|
42
80
|
}
|
|
43
|
-
|
|
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
|
-
},
|
|
81
|
+
let code = await (0, _utils().blobToString)(contents);
|
|
82
|
+
let originalMap = map ? await map.stringify({}) : null;
|
|
83
|
+
let config = {
|
|
84
|
+
...userConfig,
|
|
85
|
+
sourceMap: bundle.env.sourceMap ? {
|
|
86
|
+
filename: _path().default.relative(options.projectRoot, _path().default.join(bundle.target.distDir, bundle.name)),
|
|
87
|
+
asObject: true,
|
|
88
|
+
content: originalMap
|
|
89
|
+
} : false,
|
|
90
|
+
toplevel: bundle.env.outputFormat === 'esmodule' || bundle.env.outputFormat === 'commonjs',
|
|
59
91
|
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
|
-
|
|
84
|
-
|
|
92
|
+
};
|
|
93
|
+
let result;
|
|
94
|
+
try {
|
|
95
|
+
result = await (0, _terser().minify)(code, config);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
// $FlowFixMe
|
|
98
|
+
let {
|
|
99
|
+
message,
|
|
100
|
+
line,
|
|
101
|
+
col
|
|
102
|
+
} = error;
|
|
103
|
+
if (line != null && col != null) {
|
|
104
|
+
message = (0, _diagnostic().escapeMarkdown)(message);
|
|
105
|
+
let diagnostics = [];
|
|
106
|
+
let mapping = map === null || map === void 0 ? void 0 : map.findClosestMapping(line, col);
|
|
107
|
+
if (mapping && mapping.original && mapping.source) {
|
|
108
|
+
let {
|
|
109
|
+
source,
|
|
110
|
+
original
|
|
111
|
+
} = mapping;
|
|
112
|
+
let filePath = _path().default.resolve(options.projectRoot, source);
|
|
113
|
+
diagnostics.push({
|
|
114
|
+
message,
|
|
115
|
+
origin: '@parcel/optimizer-terser',
|
|
116
|
+
codeFrames: [{
|
|
117
|
+
language: 'js',
|
|
118
|
+
filePath,
|
|
119
|
+
code: await options.inputFS.readFile(filePath, 'utf8'),
|
|
120
|
+
codeHighlights: [{
|
|
121
|
+
message,
|
|
122
|
+
start: original,
|
|
123
|
+
end: original
|
|
124
|
+
}]
|
|
125
|
+
}],
|
|
126
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."]
|
|
127
|
+
});
|
|
85
128
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
129
|
+
if (diagnostics.length === 0 || options.logLevel === 'verbose') {
|
|
130
|
+
let loc = {
|
|
131
|
+
line: line,
|
|
132
|
+
column: col
|
|
133
|
+
};
|
|
134
|
+
diagnostics.push({
|
|
135
|
+
message,
|
|
136
|
+
origin: '@parcel/optimizer-terser',
|
|
137
|
+
codeFrames: [{
|
|
138
|
+
language: 'js',
|
|
139
|
+
filePath: undefined,
|
|
140
|
+
code,
|
|
141
|
+
codeHighlights: [{
|
|
142
|
+
message,
|
|
143
|
+
start: loc,
|
|
144
|
+
end: loc
|
|
145
|
+
}]
|
|
146
|
+
}],
|
|
147
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."]
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
throw new (_diagnostic().default)({
|
|
151
|
+
diagnostic: diagnostics
|
|
152
|
+
});
|
|
153
|
+
} else {
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
91
156
|
}
|
|
92
|
-
|
|
93
|
-
let
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
157
|
+
let sourceMap = null;
|
|
158
|
+
let minifiedContents = (0, _nullthrows().default)(result.code);
|
|
159
|
+
let resultMap = result.map;
|
|
160
|
+
if (resultMap && typeof resultMap !== 'string') {
|
|
161
|
+
sourceMap = new (_sourceMap().default)(options.projectRoot);
|
|
162
|
+
sourceMap.addVLQMap(resultMap);
|
|
163
|
+
let sourcemapReference = await getSourceMapReference(sourceMap);
|
|
164
|
+
if (sourcemapReference) {
|
|
165
|
+
minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
|
|
166
|
+
}
|
|
97
167
|
}
|
|
98
|
-
|
|
99
168
|
return {
|
|
100
|
-
contents:
|
|
169
|
+
contents: minifiedContents,
|
|
101
170
|
map: sourceMap
|
|
102
171
|
};
|
|
103
172
|
}
|
|
104
|
-
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
exports.default = _default;
|
|
173
|
+
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/optimizer-terser",
|
|
3
|
-
"version": "2.0.0-nightly.
|
|
3
|
+
"version": "2.0.0-nightly.1491+fdf495ba3",
|
|
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": "
|
|
19
|
+
"node": ">= 12.0.0",
|
|
20
|
+
"parcel": "2.0.0-nightly.1489+fdf495ba3"
|
|
17
21
|
},
|
|
18
22
|
"dependencies": {
|
|
19
|
-
"@parcel/
|
|
20
|
-
"@parcel/
|
|
21
|
-
"@parcel/
|
|
23
|
+
"@parcel/diagnostic": "2.0.0-nightly.1491+fdf495ba3",
|
|
24
|
+
"@parcel/plugin": "2.0.0-nightly.1491+fdf495ba3",
|
|
25
|
+
"@parcel/source-map": "^2.1.1",
|
|
26
|
+
"@parcel/utils": "2.0.0-nightly.1491+fdf495ba3",
|
|
22
27
|
"nullthrows": "^1.1.1",
|
|
23
|
-
"terser": "^
|
|
28
|
+
"terser": "^5.2.0"
|
|
24
29
|
},
|
|
25
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "fdf495ba3c8ed727d10e87cc9697bc9ef5ec997a"
|
|
26
31
|
}
|
package/src/TerserOptimizer.js
CHANGED
|
@@ -3,78 +3,119 @@
|
|
|
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
|
-
|
|
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', '.terserrc.cjs', '.terserrc.mjs'],
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
return userConfig?.contents;
|
|
20
|
+
},
|
|
21
|
+
async optimize({
|
|
22
|
+
contents,
|
|
23
|
+
map,
|
|
24
|
+
bundle,
|
|
25
|
+
config: userConfig,
|
|
26
|
+
options,
|
|
27
|
+
getSourceMapReference,
|
|
28
|
+
}) {
|
|
29
|
+
if (!bundle.env.shouldOptimize) {
|
|
13
30
|
return {contents, map};
|
|
14
31
|
}
|
|
15
32
|
|
|
16
|
-
|
|
17
|
-
throw new Error(
|
|
18
|
-
'TerserOptimizer: Only string contents are currently supported',
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let userConfig = await loadConfig(
|
|
23
|
-
options.inputFS,
|
|
24
|
-
path.join(options.projectRoot, 'index'),
|
|
25
|
-
['.terserrc', '.uglifyrc', '.uglifyrc.js', '.terserrc.js'],
|
|
26
|
-
);
|
|
33
|
+
let code = await blobToString(contents);
|
|
27
34
|
|
|
35
|
+
let originalMap = map ? await map.stringify({}) : null;
|
|
28
36
|
let config = {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
...userConfig,
|
|
38
|
+
sourceMap: bundle.env.sourceMap
|
|
39
|
+
? {
|
|
40
|
+
filename: path.relative(
|
|
41
|
+
options.projectRoot,
|
|
42
|
+
path.join(bundle.target.distDir, bundle.name),
|
|
43
|
+
),
|
|
44
|
+
asObject: true,
|
|
45
|
+
content: originalMap,
|
|
46
|
+
}
|
|
47
|
+
: false,
|
|
48
|
+
toplevel:
|
|
49
|
+
bundle.env.outputFormat === 'esmodule' ||
|
|
50
|
+
bundle.env.outputFormat === 'commonjs',
|
|
40
51
|
module: bundle.env.outputFormat === 'esmodule',
|
|
41
52
|
};
|
|
42
53
|
|
|
43
|
-
let
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
let result;
|
|
55
|
+
try {
|
|
56
|
+
result = await minify(code, config);
|
|
57
|
+
} catch (error) {
|
|
46
58
|
// $FlowFixMe
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
let {message, line, col} = error;
|
|
60
|
+
if (line != null && col != null) {
|
|
61
|
+
message = escapeMarkdown(message);
|
|
62
|
+
let diagnostics = [];
|
|
63
|
+
let mapping = map?.findClosestMapping(line, col);
|
|
64
|
+
if (mapping && mapping.original && mapping.source) {
|
|
65
|
+
let {source, original} = mapping;
|
|
66
|
+
let filePath = path.resolve(options.projectRoot, source);
|
|
67
|
+
diagnostics.push({
|
|
68
|
+
message,
|
|
69
|
+
origin: '@parcel/optimizer-terser',
|
|
70
|
+
codeFrames: [
|
|
71
|
+
{
|
|
72
|
+
language: 'js',
|
|
73
|
+
filePath,
|
|
74
|
+
code: await options.inputFS.readFile(filePath, 'utf8'),
|
|
75
|
+
codeHighlights: [{message, start: original, end: original}],
|
|
57
76
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
});
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
}
|
|
77
|
+
],
|
|
78
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."],
|
|
79
|
+
});
|
|
80
|
+
}
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
if (diagnostics.length === 0 || options.logLevel === 'verbose') {
|
|
83
|
+
let loc = {
|
|
84
|
+
line: line,
|
|
85
|
+
column: col,
|
|
86
|
+
};
|
|
87
|
+
diagnostics.push({
|
|
88
|
+
message,
|
|
89
|
+
origin: '@parcel/optimizer-terser',
|
|
90
|
+
codeFrames: [
|
|
91
|
+
{
|
|
92
|
+
language: 'js',
|
|
93
|
+
filePath: undefined,
|
|
94
|
+
code,
|
|
95
|
+
codeHighlights: [{message, start: loc, end: loc}],
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
hints: ["It's likely that Terser doesn't support this syntax yet."],
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
throw new ThrowableDiagnostic({diagnostic: diagnostics});
|
|
102
|
+
} else {
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
70
105
|
}
|
|
71
106
|
|
|
72
|
-
let
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
107
|
+
let sourceMap = null;
|
|
108
|
+
let minifiedContents: string = nullthrows(result.code);
|
|
109
|
+
let resultMap = result.map;
|
|
110
|
+
if (resultMap && typeof resultMap !== 'string') {
|
|
111
|
+
sourceMap = new SourceMap(options.projectRoot);
|
|
112
|
+
sourceMap.addVLQMap(resultMap);
|
|
113
|
+
let sourcemapReference = await getSourceMapReference(sourceMap);
|
|
114
|
+
if (sourcemapReference) {
|
|
115
|
+
minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
|
|
116
|
+
}
|
|
76
117
|
}
|
|
77
118
|
|
|
78
|
-
return {contents:
|
|
119
|
+
return {contents: minifiedContents, map: sourceMap};
|
|
79
120
|
},
|
|
80
|
-
});
|
|
121
|
+
}): Optimizer);
|