Skip to content

Commit a177425

Browse files
fix: don't crash in non-parallel mode (#395)
1 parent bfbb68a commit a177425

18 files changed

+2675
-2694
lines changed

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no-install commitlint --edit $1

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx lint-staged

husky.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

package-lock.json

Lines changed: 1728 additions & 1773 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
3434
"pretest": "npm run lint",
3535
"test": "npm run test:coverage",
36-
"prepare": "npm run build",
36+
"prepare": "npm run build && husky install",
3737
"release": "standard-version"
3838
},
3939
"files": [
@@ -49,35 +49,35 @@
4949
"schema-utils": "^3.0.0",
5050
"serialize-javascript": "^5.0.1",
5151
"source-map": "^0.6.1",
52-
"terser": "^5.6.0"
52+
"terser": "^5.7.0"
5353
},
5454
"devDependencies": {
55-
"@babel/cli": "^7.13.0",
56-
"@babel/core": "^7.13.8",
57-
"@babel/preset-env": "^7.13.9",
58-
"@commitlint/cli": "^12.0.1",
59-
"@commitlint/config-conventional": "^12.0.1",
55+
"@babel/cli": "^7.13.16",
56+
"@babel/core": "^7.14.0",
57+
"@babel/preset-env": "^7.14.1",
58+
"@commitlint/cli": "^12.1.3",
59+
"@commitlint/config-conventional": "^12.1.3",
6060
"@types/serialize-javascript": "^5.0.0",
6161
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
6262
"babel-jest": "^26.6.3",
63-
"copy-webpack-plugin": "^8.0.0",
63+
"copy-webpack-plugin": "^8.1.1",
6464
"cross-env": "^7.0.3",
6565
"del": "^6.0.0",
6666
"del-cli": "^3.0.1",
67-
"eslint": "^7.21.0",
68-
"eslint-config-prettier": "^8.1.0",
67+
"eslint": "^7.26.0",
68+
"eslint-config-prettier": "^8.3.0",
6969
"eslint-plugin-import": "^2.22.1",
7070
"file-loader": "^6.2.0",
71-
"husky": "^4.3.8",
71+
"husky": "^6.0.0",
7272
"jest": "^26.6.3",
7373
"lint-staged": "^10.5.4",
74-
"memfs": "^3.2.0",
74+
"memfs": "^3.2.2",
7575
"npm-run-all": "^4.1.5",
76-
"prettier": "^2.2.1",
77-
"standard-version": "^9.1.1",
78-
"typescript": "^4.2.2",
79-
"uglify-js": "^3.13.0",
80-
"webpack": "^5.24.3",
76+
"prettier": "^2.3.0",
77+
"standard-version": "^9.3.0",
78+
"typescript": "^4.2.4",
79+
"uglify-js": "^3.13.6",
80+
"webpack": "^5.37.0",
8181
"worker-loader": "^3.0.8"
8282
},
8383
"keywords": [

src/index.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ class TerserPlugin {
240240
return true;
241241
})
242242
.map(async (name) => {
243-
const { info, source } = /** @type {Asset} */ (compilation.getAsset(
244-
name
245-
));
243+
const { info, source } = /** @type {Asset} */ (
244+
compilation.getAsset(name)
245+
);
246246

247247
const eTag = cache.getLazyHashedEtag(source);
248248
const cacheItem = cache.getItemCache(name, eTag);
@@ -277,10 +277,12 @@ class TerserPlugin {
277277

278278
initializedWorker =
279279
/** @type {MinifyWorker} */
280-
(new Worker(require.resolve("./minify"), {
281-
numWorkers: numberOfWorkers,
282-
enableWorkerThreads: true,
283-
}));
280+
(
281+
new Worker(require.resolve("./minify"), {
282+
numWorkers: numberOfWorkers,
283+
enableWorkerThreads: true,
284+
})
285+
);
284286

285287
// https://github.com/facebook/jest/issues/8872#issuecomment-524822081
286288
const workerStdout = initializedWorker.getStdout();
@@ -304,11 +306,8 @@ class TerserPlugin {
304306
? /** @type {number} */ (numberOfWorkers)
305307
: Infinity
306308
);
307-
const {
308-
SourceMapSource,
309-
ConcatSource,
310-
RawSource,
311-
} = compiler.webpack.sources;
309+
const { SourceMapSource, ConcatSource, RawSource } =
310+
compiler.webpack.sources;
312311

313312
/** @typedef {{ extractedCommentsSource : import("webpack").sources.RawSource, commentsFilename: string }} ExtractedCommentsInfo */
314313
/** @type {Map<string, ExtractedCommentsInfo>} */
@@ -326,10 +325,8 @@ class TerserPlugin {
326325
/** @type {RawSourceMap | undefined} */
327326
let inputSourceMap;
328327

329-
const {
330-
source: sourceFromInputSource,
331-
map,
332-
} = inputSource.sourceAndMap();
328+
const { source: sourceFromInputSource, map } =
329+
inputSource.sourceAndMap();
333330

334331
input = sourceFromInputSource;
335332

@@ -380,18 +377,20 @@ class TerserPlugin {
380377

381378
compilation.errors.push(
382379
/** @type {WebpackError} */
383-
(TerserPlugin.buildError(
384-
error,
385-
name,
386-
// eslint-disable-next-line no-undefined
387-
hasSourceMap ? compilation.requestShortener : undefined,
388-
hasSourceMap
389-
? new SourceMapConsumer(
390-
/** @type {RawSourceMap} */ (inputSourceMap)
391-
)
392-
: // eslint-disable-next-line no-undefined
393-
undefined
394-
))
380+
(
381+
TerserPlugin.buildError(
382+
error,
383+
name,
384+
// eslint-disable-next-line no-undefined
385+
hasSourceMap ? compilation.requestShortener : undefined,
386+
hasSourceMap
387+
? new SourceMapConsumer(
388+
/** @type {RawSourceMap} */ (inputSourceMap)
389+
)
390+
: // eslint-disable-next-line no-undefined
391+
undefined
392+
)
393+
)
395394
);
396395

397396
return;
@@ -536,7 +535,10 @@ class TerserPlugin {
536535
* @returns {Promise<ExtractedCommentsInfoWIthFrom>}
537536
*/
538537
async (previousPromise, [from, value]) => {
539-
const previous = /** @type {ExtractedCommentsInfoWIthFrom | undefined} **/ (await previousPromise);
538+
const previous =
539+
/** @type {ExtractedCommentsInfoWIthFrom | undefined} **/ (
540+
await previousPromise
541+
);
540542
const { commentsFilename, extractedCommentsSource } = value;
541543

542544
if (previous && previous.commentsFilename === commentsFilename) {
@@ -556,9 +558,9 @@ class TerserPlugin {
556558
Array.from(
557559
new Set([
558560
.../** @type {string}*/ (prevSource.source()).split("\n\n"),
559-
.../** @type {string}*/ (extractedCommentsSource.source()).split(
560-
"\n\n"
561-
),
561+
.../** @type {string}*/ (
562+
extractedCommentsSource.source()
563+
).split("\n\n"),
562564
])
563565
).join("\n\n")
564566
);
@@ -635,9 +637,10 @@ class TerserPlugin {
635637
);
636638

637639
compiler.hooks.compilation.tap(pluginName, (compilation) => {
638-
const hooks = compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(
639-
compilation
640-
);
640+
const hooks =
641+
compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(
642+
compilation
643+
);
641644
const data = serialize({
642645
terser: terserPackageJson.version,
643646
terserOptions: this.options.terserOptions,

src/minify.js

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,35 @@ const { minify: terserMinify } = require("terser");
4141
* @returns {NormalizedTerserMinifyOptions}
4242
*/
4343
function buildTerserOptions(terserOptions = {}) {
44+
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
4445
return {
4546
...terserOptions,
47+
compress:
48+
typeof terserOptions.compress === "boolean"
49+
? terserOptions.compress
50+
: { ...terserOptions.compress },
51+
// ecma: terserOptions.ecma,
52+
// ie8: terserOptions.ie8,
53+
// keep_classnames: terserOptions.keep_classnames,
54+
// keep_fnames: terserOptions.keep_fnames,
4655
mangle:
4756
terserOptions.mangle == null
4857
? true
4958
: typeof terserOptions.mangle === "boolean"
5059
? terserOptions.mangle
5160
: { ...terserOptions.mangle },
52-
// Ignoring sourceMap from options
53-
// eslint-disable-next-line no-undefined
54-
sourceMap: undefined,
61+
// module: terserOptions.module,
62+
// nameCache: { ...terserOptions.toplevel },
5563
// the `output` option is deprecated
5664
...(terserOptions.format
5765
? { format: { beautify: false, ...terserOptions.format } }
5866
: { output: { beautify: false, ...terserOptions.output } }),
67+
parse: { ...terserOptions.parse },
68+
// safari10: terserOptions.safari10,
69+
// Ignoring sourceMap from options
70+
// eslint-disable-next-line no-undefined
71+
sourceMap: undefined,
72+
// toplevel: terserOptions.toplevel
5973
};
6074
}
6175

@@ -135,31 +149,30 @@ function buildComments(extractComments, terserOptions, extractedComments) {
135149
}
136150

137151
if (condition[key] === "some") {
138-
condition[key] = /** @type {ExtractCommentsFunction} */ ((
139-
astNode,
140-
comment
141-
) =>
142-
(comment.type === "comment2" || comment.type === "comment1") &&
143-
/@preserve|@lic|@cc_on|^\**!/i.test(comment.value));
152+
condition[key] = /** @type {ExtractCommentsFunction} */ (
153+
(astNode, comment) =>
154+
(comment.type === "comment2" || comment.type === "comment1") &&
155+
/@preserve|@lic|@cc_on|^\**!/i.test(comment.value)
156+
);
144157

145158
break;
146159
}
147160

148161
regexStr = /** @type {string} */ (condition[key]);
149162

150-
condition[key] = /** @type {ExtractCommentsFunction} */ ((
151-
astNode,
152-
comment
153-
) => new RegExp(/** @type {string} */ (regexStr)).test(comment.value));
163+
condition[key] = /** @type {ExtractCommentsFunction} */ (
164+
(astNode, comment) =>
165+
new RegExp(/** @type {string} */ (regexStr)).test(comment.value)
166+
);
154167

155168
break;
156169
default:
157170
regex = /** @type {RegExp} */ (condition[key]);
158171

159-
condition[key] = /** @type {ExtractCommentsFunction} */ ((
160-
astNode,
161-
comment
162-
) => /** @type {RegExp} */ (regex).test(comment.value));
172+
condition[key] = /** @type {ExtractCommentsFunction} */ (
173+
(astNode, comment) =>
174+
/** @type {RegExp} */ (regex).test(comment.value)
175+
);
163176
}
164177
});
165178

@@ -181,10 +194,9 @@ function buildComments(extractComments, terserOptions, extractedComments) {
181194
}
182195
}
183196

184-
return /** @type {{ preserve: ExtractCommentsFunction }} */ (condition).preserve(
185-
astNode,
186-
comment
187-
);
197+
return /** @type {{ preserve: ExtractCommentsFunction }} */ (
198+
condition
199+
).preserve(astNode, comment);
188200
};
189201
}
190202

@@ -247,15 +259,17 @@ function transform(options) {
247259
// eslint-disable-next-line no-param-reassign
248260
const evaluatedOptions =
249261
/** @type {InternalMinifyOptions} */
250-
// eslint-disable-next-line no-new-func
251-
(new Function(
252-
"exports",
253-
"require",
254-
"module",
255-
"__filename",
256-
"__dirname",
257-
`'use strict'\nreturn ${options}`
258-
)(exports, require, module, __filename, __dirname));
262+
(
263+
// eslint-disable-next-line no-new-func
264+
new Function(
265+
"exports",
266+
"require",
267+
"module",
268+
"__filename",
269+
"__dirname",
270+
`'use strict'\nreturn ${options}`
271+
)(exports, require, module, __filename, __dirname)
272+
);
259273

260274
return minify(evaluatedOptions);
261275
}

0 commit comments

Comments
 (0)