@parcel/optimizer-terser 2.0.0-nightly.130 → 2.0.0-nightly.1303

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