@parcel/optimizer-terser 2.0.0-nightly.97 → 2.0.0-nightly.970

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,214 @@ 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
+ }
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
- 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; }
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
- 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; }
91
+ if (userConfig) {
92
+ let isJavascript = _path().default.extname(userConfig.filePath) === '.js';
25
93
 
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; }
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
- options
106
+ config: userConfig,
107
+ options,
108
+ getSourceMapReference
34
109
  }) {
35
- var _userConfig$config;
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
- 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
- },
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
- 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
- }
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
- if (sourceMap && map) {
90
- sourceMap = await map.extend(sourceMap);
192
+ throw new (_diagnostic().default)({
193
+ diagnostic: diagnostics
194
+ });
195
+ } else {
196
+ throw error;
197
+ }
91
198
  }
92
199
 
93
- let result = (0, _terser.minify)(contents, config);
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
- if (result.error) {
96
- throw result.error;
209
+ if (sourcemapReference) {
210
+ minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
211
+ }
97
212
  }
98
213
 
99
214
  return {
100
- contents: (0, _nullthrows.default)(result.code),
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.0-nightly.97+a63f3fc9",
3
+ "version": "2.0.0-nightly.970+5a80fbe4",
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.968+5a80fbe4"
17
21
  },
18
22
  "dependencies": {
19
- "@parcel/plugin": "2.0.0-nightly.97+a63f3fc9",
20
- "@parcel/source-map": "2.0.0-nightly.97+a63f3fc9",
21
- "@parcel/utils": "2.0.0-nightly.97+a63f3fc9",
23
+ "@parcel/diagnostic": "2.0.0-nightly.970+5a80fbe4",
24
+ "@parcel/plugin": "2.0.0-nightly.970+5a80fbe4",
25
+ "@parcel/source-map": "^2.0.0",
26
+ "@parcel/utils": "2.0.0-nightly.970+5a80fbe4",
22
27
  "nullthrows": "^1.1.1",
23
- "terser": "^4.0.0"
28
+ "terser": "^5.2.0"
24
29
  },
25
- "gitHead": "a63f3fc9726483219412920faeb255e035f90747"
30
+ "gitHead": "5a80fbe4af8797d0aab248a66f5254f42f00e83e"
26
31
  }
@@ -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 {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 (!options.minify) {
13
- return {contents, map};
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
- if (typeof contents !== 'string') {
17
- throw new Error(
18
- 'TerserOptimizer: Only string contents are currently supported',
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 userConfig = await loadConfig(
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
- 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
- },
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 sourceMap = null;
44
- if (options.sourceMaps) {
45
- sourceMap = new SourceMap();
61
+ let result;
62
+ try {
63
+ result = await minify(code, config);
64
+ } catch (error) {
46
65
  // $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,
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
- generated: {
59
- line: gen_line,
60
- column: gen_col,
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
- if (sourceMap && map) {
69
- sourceMap = await map.extend(sourceMap);
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 result = minify(contents, config);
73
-
74
- if (result.error) {
75
- throw result.error;
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: nullthrows(result.code), map: sourceMap};
126
+ return {contents: minifiedContents, map: sourceMap};
79
127
  },
80
- });
128
+ }): Optimizer);