@parcel/optimizer-swc 2.8.3 → 2.8.4-nightly.0

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.
@@ -55,6 +55,16 @@ function _sourceMap() {
55
55
  return data;
56
56
  }
57
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
+
58
68
  function _path() {
59
69
  const data = _interopRequireDefault(require("path"));
60
70
 
@@ -65,6 +75,10 @@ function _path() {
65
75
  return data;
66
76
  }
67
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; }
81
+
68
82
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
69
83
 
70
84
  var _default = new (_plugin().Optimizer)({
@@ -72,16 +86,7 @@ var _default = new (_plugin().Optimizer)({
72
86
  config,
73
87
  options
74
88
  }) {
75
- let userConfig = await config.getConfigFrom(_path().default.join(options.projectRoot, 'index'), ['.terserrc', '.terserrc.js', '.terserrc.cjs']);
76
-
77
- if (userConfig) {
78
- let isJavascript = _path().default.extname(userConfig.filePath) === '.js';
79
-
80
- if (isJavascript) {
81
- config.invalidateOnStartup();
82
- }
83
- }
84
-
89
+ let userConfig = await config.getConfigFrom(_path().default.join(options.projectRoot, 'index'), ['.terserrc', '.terserrc.js', '.terserrc.cjs', '.terserrc.mjs']);
85
90
  return userConfig === null || userConfig === void 0 ? void 0 : userConfig.contents;
86
91
  },
87
92
 
@@ -101,17 +106,83 @@ var _default = new (_plugin().Optimizer)({
101
106
  }
102
107
 
103
108
  let code = await (0, _utils().blobToString)(contents);
104
- let config = {
105
- mangle: true,
106
- compress: true,
107
- ...userConfig,
108
- sourceMap: bundle.env.sourceMap ? {
109
- filename: _path().default.relative(options.projectRoot, _path().default.join(bundle.target.distDir, bundle.name))
110
- } : false,
111
- toplevel: bundle.env.outputFormat === 'esmodule' || bundle.env.outputFormat === 'commonjs',
112
- module: bundle.env.outputFormat === 'esmodule'
113
- };
114
- let result = await (0, _core().minify)(code, config);
109
+ let result;
110
+
111
+ try {
112
+ result = await (0, _core().transform)(code, {
113
+ jsc: {
114
+ target: 'es2022',
115
+ minify: {
116
+ mangle: true,
117
+ compress: true,
118
+ ...userConfig,
119
+ toplevel: bundle.env.outputFormat === 'esmodule' || bundle.env.outputFormat === 'commonjs',
120
+ module: bundle.env.outputFormat === 'esmodule'
121
+ }
122
+ },
123
+ minify: true,
124
+ sourceMaps: !!bundle.env.sourceMap,
125
+ configFile: false,
126
+ swcrc: false
127
+ });
128
+ } catch (err) {
129
+ // SWC doesn't give us nice error objects, so we need to parse the message.
130
+ let message = (0, _diagnostic().escapeMarkdown)(((0, _utils().stripAnsi)(err.message).split('\n').find(line => line.trim().length > 0) || '').trim().replace(/^(×|x)\s+/, ''));
131
+ let location = err.message.match(/(?:╭─|,-)\[(\d+):(\d+)\]/);
132
+
133
+ if (location) {
134
+ let line = Number(location[1]);
135
+ let col = Number(location[1]);
136
+ let mapping = originalMap === null || originalMap === void 0 ? void 0 : originalMap.findClosestMapping(line, col);
137
+
138
+ if (mapping && mapping.original && mapping.source) {
139
+ let {
140
+ source,
141
+ original
142
+ } = mapping;
143
+
144
+ let filePath = _path().default.resolve(options.projectRoot, source);
145
+
146
+ throw new (_diagnostic().default)({
147
+ diagnostic: {
148
+ message,
149
+ origin: '@parcel/optimizer-swc',
150
+ codeFrames: [{
151
+ language: 'js',
152
+ filePath,
153
+ codeHighlights: [{
154
+ start: original,
155
+ end: original
156
+ }]
157
+ }]
158
+ }
159
+ });
160
+ }
161
+
162
+ let loc = {
163
+ line: line,
164
+ column: col
165
+ };
166
+ throw new (_diagnostic().default)({
167
+ diagnostic: {
168
+ message,
169
+ origin: '@parcel/optimizer-swc',
170
+ codeFrames: [{
171
+ language: 'js',
172
+ filePath: undefined,
173
+ code,
174
+ codeHighlights: [{
175
+ start: loc,
176
+ end: loc
177
+ }]
178
+ }]
179
+ }
180
+ });
181
+ }
182
+
183
+ throw err;
184
+ }
185
+
115
186
  let sourceMap = null;
116
187
  let minifiedContents = (0, _nullthrows().default)(result.code);
117
188
  let resultMap = result.map;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/optimizer-swc",
3
- "version": "2.8.3",
3
+ "version": "2.8.4-nightly.0+7b79c6d",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,14 +17,15 @@
17
17
  "source": "src/SwcOptimizer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.8.3"
20
+ "parcel": "2.8.4-nightly.0+7b79c6d"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/plugin": "2.8.3",
23
+ "@parcel/diagnostic": "2.8.4-nightly.0+7b79c6d",
24
+ "@parcel/plugin": "2.8.4-nightly.0+7b79c6d",
24
25
  "@parcel/source-map": "^2.1.1",
25
- "@parcel/utils": "2.8.3",
26
- "@swc/core": "^1.2.106",
26
+ "@parcel/utils": "2.8.4-nightly.0+7b79c6d",
27
+ "@swc/core": "^1.3.36",
27
28
  "nullthrows": "^1.1.1"
28
29
  },
29
- "gitHead": "349a6caf40ec8abb6a49fcae0765f8f8deb2073d"
30
+ "gitHead": "7b79c6d69ffabef89810a8db61e9abdeb70d6990"
30
31
  }
@@ -1,27 +1,20 @@
1
1
  // @flow
2
2
 
3
3
  import nullthrows from 'nullthrows';
4
- import {minify} from '@swc/core';
4
+ import {transform} from '@swc/core';
5
5
  import {Optimizer} from '@parcel/plugin';
6
- import {blobToString} from '@parcel/utils';
6
+ import {blobToString, stripAnsi} from '@parcel/utils';
7
7
  import SourceMap from '@parcel/source-map';
8
-
8
+ import ThrowableDiagnostic, {escapeMarkdown} from '@parcel/diagnostic';
9
9
  import path from 'path';
10
10
 
11
11
  export default (new Optimizer({
12
12
  async loadConfig({config, options}) {
13
13
  let userConfig = await config.getConfigFrom(
14
14
  path.join(options.projectRoot, 'index'),
15
- ['.terserrc', '.terserrc.js', '.terserrc.cjs'],
15
+ ['.terserrc', '.terserrc.js', '.terserrc.cjs', '.terserrc.mjs'],
16
16
  );
17
17
 
18
- if (userConfig) {
19
- let isJavascript = path.extname(userConfig.filePath) === '.js';
20
- if (isJavascript) {
21
- config.invalidateOnStartup();
22
- }
23
- }
24
-
25
18
  return userConfig?.contents;
26
19
  },
27
20
  async optimize({
@@ -37,25 +30,83 @@ export default (new Optimizer({
37
30
  }
38
31
 
39
32
  let code = await blobToString(contents);
40
- let config = {
41
- mangle: true,
42
- compress: true,
43
- ...userConfig,
44
- sourceMap: bundle.env.sourceMap
45
- ? {
46
- filename: path.relative(
47
- options.projectRoot,
48
- path.join(bundle.target.distDir, bundle.name),
49
- ),
50
- }
51
- : false,
52
- toplevel:
53
- bundle.env.outputFormat === 'esmodule' ||
54
- bundle.env.outputFormat === 'commonjs',
55
- module: bundle.env.outputFormat === 'esmodule',
56
- };
33
+ let result;
34
+ try {
35
+ result = await transform(code, {
36
+ jsc: {
37
+ target: 'es2022',
38
+ minify: {
39
+ mangle: true,
40
+ compress: true,
41
+ ...userConfig,
42
+ toplevel:
43
+ bundle.env.outputFormat === 'esmodule' ||
44
+ bundle.env.outputFormat === 'commonjs',
45
+ module: bundle.env.outputFormat === 'esmodule',
46
+ },
47
+ },
48
+ minify: true,
49
+ sourceMaps: !!bundle.env.sourceMap,
50
+ configFile: false,
51
+ swcrc: false,
52
+ });
53
+ } catch (err) {
54
+ // SWC doesn't give us nice error objects, so we need to parse the message.
55
+ let message = escapeMarkdown(
56
+ (
57
+ stripAnsi(err.message)
58
+ .split('\n')
59
+ .find(line => line.trim().length > 0) || ''
60
+ )
61
+ .trim()
62
+ .replace(/^(×|x)\s+/, ''),
63
+ );
64
+ let location = err.message.match(/(?:╭─|,-)\[(\d+):(\d+)\]/);
65
+ if (location) {
66
+ let line = Number(location[1]);
67
+ let col = Number(location[1]);
68
+ let mapping = originalMap?.findClosestMapping(line, col);
69
+ if (mapping && mapping.original && mapping.source) {
70
+ let {source, original} = mapping;
71
+ let filePath = path.resolve(options.projectRoot, source);
72
+ throw new ThrowableDiagnostic({
73
+ diagnostic: {
74
+ message,
75
+ origin: '@parcel/optimizer-swc',
76
+ codeFrames: [
77
+ {
78
+ language: 'js',
79
+ filePath,
80
+ codeHighlights: [{start: original, end: original}],
81
+ },
82
+ ],
83
+ },
84
+ });
85
+ }
57
86
 
58
- let result = await minify(code, config);
87
+ let loc = {
88
+ line: line,
89
+ column: col,
90
+ };
91
+
92
+ throw new ThrowableDiagnostic({
93
+ diagnostic: {
94
+ message,
95
+ origin: '@parcel/optimizer-swc',
96
+ codeFrames: [
97
+ {
98
+ language: 'js',
99
+ filePath: undefined,
100
+ code,
101
+ codeHighlights: [{start: loc, end: loc}],
102
+ },
103
+ ],
104
+ },
105
+ });
106
+ }
107
+
108
+ throw err;
109
+ }
59
110
 
60
111
  let sourceMap = null;
61
112
  let minifiedContents: string = nullthrows(result.code);