Skip to content

Commit

Permalink
fix: don't crash in non-parallel mode (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored May 12, 2021
1 parent bfbb68a commit a177425
Show file tree
Hide file tree
Showing 18 changed files with 2,675 additions and 2,694 deletions.
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
6 changes: 0 additions & 6 deletions husky.config.js

This file was deleted.

3,501 changes: 1,728 additions & 1,773 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
"pretest": "npm run lint",
"test": "npm run test:coverage",
"prepare": "npm run build",
"prepare": "npm run build && husky install",
"release": "standard-version"
},
"files": [
Expand All @@ -49,35 +49,35 @@
"schema-utils": "^3.0.0",
"serialize-javascript": "^5.0.1",
"source-map": "^0.6.1",
"terser": "^5.6.0"
"terser": "^5.7.0"
},
"devDependencies": {
"@babel/cli": "^7.13.0",
"@babel/core": "^7.13.8",
"@babel/preset-env": "^7.13.9",
"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"@babel/cli": "^7.13.16",
"@babel/core": "^7.14.0",
"@babel/preset-env": "^7.14.1",
"@commitlint/cli": "^12.1.3",
"@commitlint/config-conventional": "^12.1.3",
"@types/serialize-javascript": "^5.0.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.6.3",
"copy-webpack-plugin": "^8.0.0",
"copy-webpack-plugin": "^8.1.1",
"cross-env": "^7.0.3",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint": "^7.26.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.22.1",
"file-loader": "^6.2.0",
"husky": "^4.3.8",
"husky": "^6.0.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"memfs": "^3.2.0",
"memfs": "^3.2.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"standard-version": "^9.1.1",
"typescript": "^4.2.2",
"uglify-js": "^3.13.0",
"webpack": "^5.24.3",
"prettier": "^2.3.0",
"standard-version": "^9.3.0",
"typescript": "^4.2.4",
"uglify-js": "^3.13.6",
"webpack": "^5.37.0",
"worker-loader": "^3.0.8"
},
"keywords": [
Expand Down
73 changes: 38 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ class TerserPlugin {
return true;
})
.map(async (name) => {
const { info, source } = /** @type {Asset} */ (compilation.getAsset(
name
));
const { info, source } = /** @type {Asset} */ (
compilation.getAsset(name)
);

const eTag = cache.getLazyHashedEtag(source);
const cacheItem = cache.getItemCache(name, eTag);
Expand Down Expand Up @@ -277,10 +277,12 @@ class TerserPlugin {

initializedWorker =
/** @type {MinifyWorker} */
(new Worker(require.resolve("./minify"), {
numWorkers: numberOfWorkers,
enableWorkerThreads: true,
}));
(
new Worker(require.resolve("./minify"), {
numWorkers: numberOfWorkers,
enableWorkerThreads: true,
})
);

// https://github.com/facebook/jest/issues/8872#issuecomment-524822081
const workerStdout = initializedWorker.getStdout();
Expand All @@ -304,11 +306,8 @@ class TerserPlugin {
? /** @type {number} */ (numberOfWorkers)
: Infinity
);
const {
SourceMapSource,
ConcatSource,
RawSource,
} = compiler.webpack.sources;
const { SourceMapSource, ConcatSource, RawSource } =
compiler.webpack.sources;

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

const {
source: sourceFromInputSource,
map,
} = inputSource.sourceAndMap();
const { source: sourceFromInputSource, map } =
inputSource.sourceAndMap();

input = sourceFromInputSource;

Expand Down Expand Up @@ -380,18 +377,20 @@ class TerserPlugin {

compilation.errors.push(
/** @type {WebpackError} */
(TerserPlugin.buildError(
error,
name,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined,
hasSourceMap
? new SourceMapConsumer(
/** @type {RawSourceMap} */ (inputSourceMap)
)
: // eslint-disable-next-line no-undefined
undefined
))
(
TerserPlugin.buildError(
error,
name,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined,
hasSourceMap
? new SourceMapConsumer(
/** @type {RawSourceMap} */ (inputSourceMap)
)
: // eslint-disable-next-line no-undefined
undefined
)
)
);

return;
Expand Down Expand Up @@ -536,7 +535,10 @@ class TerserPlugin {
* @returns {Promise<ExtractedCommentsInfoWIthFrom>}
*/
async (previousPromise, [from, value]) => {
const previous = /** @type {ExtractedCommentsInfoWIthFrom | undefined} **/ (await previousPromise);
const previous =
/** @type {ExtractedCommentsInfoWIthFrom | undefined} **/ (
await previousPromise
);
const { commentsFilename, extractedCommentsSource } = value;

if (previous && previous.commentsFilename === commentsFilename) {
Expand All @@ -556,9 +558,9 @@ class TerserPlugin {
Array.from(
new Set([
.../** @type {string}*/ (prevSource.source()).split("\n\n"),
.../** @type {string}*/ (extractedCommentsSource.source()).split(
"\n\n"
),
.../** @type {string}*/ (
extractedCommentsSource.source()
).split("\n\n"),
])
).join("\n\n")
);
Expand Down Expand Up @@ -635,9 +637,10 @@ class TerserPlugin {
);

compiler.hooks.compilation.tap(pluginName, (compilation) => {
const hooks = compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(
compilation
);
const hooks =
compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(
compilation
);
const data = serialize({
terser: terserPackageJson.version,
terserOptions: this.options.terserOptions,
Expand Down
74 changes: 44 additions & 30 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,35 @@ const { minify: terserMinify } = require("terser");
* @returns {NormalizedTerserMinifyOptions}
*/
function buildTerserOptions(terserOptions = {}) {
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return {
...terserOptions,
compress:
typeof terserOptions.compress === "boolean"
? terserOptions.compress
: { ...terserOptions.compress },
// ecma: terserOptions.ecma,
// ie8: terserOptions.ie8,
// keep_classnames: terserOptions.keep_classnames,
// keep_fnames: terserOptions.keep_fnames,
mangle:
terserOptions.mangle == null
? true
: typeof terserOptions.mangle === "boolean"
? terserOptions.mangle
: { ...terserOptions.mangle },
// Ignoring sourceMap from options
// eslint-disable-next-line no-undefined
sourceMap: undefined,
// module: terserOptions.module,
// nameCache: { ...terserOptions.toplevel },
// the `output` option is deprecated
...(terserOptions.format
? { format: { beautify: false, ...terserOptions.format } }
: { output: { beautify: false, ...terserOptions.output } }),
parse: { ...terserOptions.parse },
// safari10: terserOptions.safari10,
// Ignoring sourceMap from options
// eslint-disable-next-line no-undefined
sourceMap: undefined,
// toplevel: terserOptions.toplevel
};
}

Expand Down Expand Up @@ -135,31 +149,30 @@ function buildComments(extractComments, terserOptions, extractedComments) {
}

if (condition[key] === "some") {
condition[key] = /** @type {ExtractCommentsFunction} */ ((
astNode,
comment
) =>
(comment.type === "comment2" || comment.type === "comment1") &&
/@preserve|@lic|@cc_on|^\**!/i.test(comment.value));
condition[key] = /** @type {ExtractCommentsFunction} */ (
(astNode, comment) =>
(comment.type === "comment2" || comment.type === "comment1") &&
/@preserve|@lic|@cc_on|^\**!/i.test(comment.value)
);

break;
}

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

condition[key] = /** @type {ExtractCommentsFunction} */ ((
astNode,
comment
) => new RegExp(/** @type {string} */ (regexStr)).test(comment.value));
condition[key] = /** @type {ExtractCommentsFunction} */ (
(astNode, comment) =>
new RegExp(/** @type {string} */ (regexStr)).test(comment.value)
);

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

condition[key] = /** @type {ExtractCommentsFunction} */ ((
astNode,
comment
) => /** @type {RegExp} */ (regex).test(comment.value));
condition[key] = /** @type {ExtractCommentsFunction} */ (
(astNode, comment) =>
/** @type {RegExp} */ (regex).test(comment.value)
);
}
});

Expand All @@ -181,10 +194,9 @@ function buildComments(extractComments, terserOptions, extractedComments) {
}
}

return /** @type {{ preserve: ExtractCommentsFunction }} */ (condition).preserve(
astNode,
comment
);
return /** @type {{ preserve: ExtractCommentsFunction }} */ (
condition
).preserve(astNode, comment);
};
}

Expand Down Expand Up @@ -247,15 +259,17 @@ function transform(options) {
// eslint-disable-next-line no-param-reassign
const evaluatedOptions =
/** @type {InternalMinifyOptions} */
// eslint-disable-next-line no-new-func
(new Function(
"exports",
"require",
"module",
"__filename",
"__dirname",
`'use strict'\nreturn ${options}`
)(exports, require, module, __filename, __dirname));
(
// eslint-disable-next-line no-new-func
new Function(
"exports",
"require",
"module",
"__filename",
"__dirname",
`'use strict'\nreturn ${options}`
)(exports, require, module, __filename, __dirname)
);

return minify(evaluatedOptions);
}
Expand Down
Loading

0 comments on commit a177425

Please sign in to comment.