@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.
@@ -5,61 +5,141 @@ 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 _diagnostic = _interopRequireDefault(require("@parcel/diagnostic"));
25
+ return data;
26
+ }
19
27
 
20
- var _path = _interopRequireDefault(require("path"));
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.minify) {
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 = { ...(userConfig === null || userConfig === void 0 ? void 0 : userConfig.config),
43
- sourceMap: options.sourceMaps ? {
44
- filename: _path.default.relative(options.projectRoot, bundle.filePath),
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 = (0, _terser.minify)(code, config);
128
+ let result;
52
129
 
53
- if (result.error) {
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
- } = result.error;
138
+ } = error;
60
139
 
61
140
  if (line != null && col != null) {
62
- let diagnostic = [];
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
- diagnostic.push({
153
+ diagnostics.push({
74
154
  message,
75
155
  origin: '@parcel/optimizer-terser',
76
- language: 'js',
77
- filePath,
78
- codeFrame: {
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 (diagnostic.length === 0 || options.logLevel === 'verbose') {
170
+ if (diagnostics.length === 0 || options.logLevel === 'verbose') {
91
171
  let loc = {
92
172
  line: line,
93
173
  column: col
94
174
  };
95
- diagnostic.push({
175
+ diagnostics.push({
96
176
  message,
97
177
  origin: '@parcel/optimizer-terser',
98
- language: 'js',
99
- filePath: undefined,
100
- codeFrame: {
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 result.error;
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 (result.map && typeof result.map !== 'string') {
124
- sourceMap = new _sourceMap.default();
125
- sourceMap.addRawMappings(result.map);
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-beta.1",
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": ">= 10.0.0",
16
- "parcel": "^2.0.0-alpha.1.1"
19
+ "node": ">= 12.0.0",
20
+ "parcel": "2.0.0-nightly.1000+5530a6ef"
17
21
  },
18
22
  "dependencies": {
19
- "@parcel/diagnostic": "2.0.0-beta.1",
20
- "@parcel/plugin": "2.0.0-beta.1",
21
- "@parcel/source-map": "2.0.0-alpha.4.13",
22
- "@parcel/utils": "2.0.0-beta.1",
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": "^4.3.0"
25
- },
26
- "peerDependencies": {
27
- "@parcel/core": "^2.0.0-alpha.3.1"
28
+ "terser": "^5.2.0"
28
29
  },
29
- "gitHead": "74335525be92e23bac4ed1bf30595443cfb238e3"
30
+ "gitHead": "5530a6eff8b619873353baeb0457ae4ec591e9fa"
30
31
  }
@@ -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, loadConfig} from '@parcel/utils';
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 optimize({contents, map, bundle, options, getSourceMapReference}) {
14
- 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'],
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?.config,
29
- sourceMap: options.sourceMaps
44
+ ...userConfig,
45
+ sourceMap: bundle.env.sourceMap
30
46
  ? {
31
- filename: path.relative(options.projectRoot, bundle.filePath),
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 = minify(code, config);
43
-
44
- if (result.error) {
61
+ let result;
62
+ try {
63
+ result = await minify(code, config);
64
+ } catch (error) {
45
65
  // $FlowFixMe
46
- let {message, line, col} = result.error;
66
+ let {message, line, col} = error;
47
67
  if (line != null && col != null) {
48
- let diagnostic = [];
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
- diagnostic.push({
74
+ diagnostics.push({
54
75
  message,
55
76
  origin: '@parcel/optimizer-terser',
56
- language: 'js',
57
- filePath,
58
- codeFrame: {
59
- code: await options.inputFS.readFile(filePath, 'utf8'),
60
- codeHighlights: [{message, start: original, end: original}],
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 (diagnostic.length === 0 || options.logLevel === 'verbose') {
89
+ if (diagnostics.length === 0 || options.logLevel === 'verbose') {
67
90
  let loc = {
68
91
  line: line,
69
92
  column: col,
70
93
  };
71
- diagnostic.push({
94
+ diagnostics.push({
72
95
  message,
73
96
  origin: '@parcel/optimizer-terser',
74
- language: 'js',
75
- filePath: undefined,
76
- codeFrame: {
77
- code,
78
- codeHighlights: [{message, start: loc, end: loc}],
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 result.error;
110
+ throw error;
86
111
  }
87
112
  }
88
113
 
89
114
  let sourceMap = null;
90
115
  let minifiedContents: string = nullthrows(result.code);
91
- if (result.map && typeof result.map !== 'string') {
92
- sourceMap = new SourceMap();
93
- sourceMap.addRawMappings(result.map);
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);