@parcel/optimizer-terser 2.0.0-nightly.149 → 2.0.0-nightly.1490

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.
@@ -4,104 +4,170 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
- var _nullthrows = _interopRequireDefault(require("nullthrows"));
9
-
10
- var _terser = require("terser");
11
-
12
- var _plugin = require("@parcel/plugin");
13
-
14
- var _utils = require("@parcel/utils");
15
-
16
- var _sourceMap = _interopRequireDefault(require("@parcel/source-map"));
17
-
18
- var _path = _interopRequireDefault(require("path"));
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
- 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; }
23
-
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; }
25
-
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; }
27
-
28
- var _default = new _plugin.Optimizer({
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
- options
71
+ config: userConfig,
72
+ options,
73
+ getSourceMapReference
34
74
  }) {
35
- var _userConfig$config;
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
- 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
- },
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
- 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
- }
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
- if (sourceMap && map) {
90
- sourceMap = await map.extend(sourceMap);
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 result = (0, _terser.minify)(contents, config);
94
-
95
- if (result.error) {
96
- throw result.error;
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: (0, _nullthrows.default)(result.code),
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.149+453f7b0b",
3
+ "version": "2.0.0-nightly.1490+642d9cabc",
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.1488+642d9cabc"
17
21
  },
18
22
  "dependencies": {
19
- "@parcel/plugin": "2.0.0-nightly.149+453f7b0b",
20
- "@parcel/source-map": "2.0.0-nightly.149+453f7b0b",
21
- "@parcel/utils": "2.0.0-nightly.149+453f7b0b",
23
+ "@parcel/diagnostic": "2.0.0-nightly.1490+642d9cabc",
24
+ "@parcel/plugin": "2.0.0-nightly.1490+642d9cabc",
25
+ "@parcel/source-map": "^2.1.1",
26
+ "@parcel/utils": "2.0.0-nightly.1490+642d9cabc",
22
27
  "nullthrows": "^1.1.1",
23
- "terser": "^4.0.0"
28
+ "terser": "^5.2.0"
24
29
  },
25
- "gitHead": "453f7b0bf295e6a7cbec5d24fab4c48e21e6168f"
30
+ "gitHead": "642d9cabc600bcb4681bbee0be168e05f5a8ffe5"
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);