Socket
Socket
Sign inDemoInstall

terser-webpack-plugin

Package Overview
Dependencies
76
Maintainers
3
Versions
77
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.2.4 to 5.2.5

142

dist/index.js

@@ -18,4 +18,2 @@ "use strict";

var _pLimit = _interopRequireDefault(require("p-limit"));
var _jestWorker = require("jest-worker");

@@ -181,3 +179,5 @@

constructor(options) {
(0, _schemaUtils.validate)(schema, options || {}, {
(0, _schemaUtils.validate)(
/** @type {Schema} */
schema, options || {}, {
name: "Terser Plugin",

@@ -188,4 +188,8 @@ baseDataPath: "options"

const {
minify = _utils.terserMinify,
terserOptions = {},
minify =
/** @type {MinimizerImplementation<InferDefaultType<T>>} */
_utils.terserMinify,
terserOptions =
/** @type {InferDefaultType<T>} */
{},
test = /\.[cm]?js(\?.*)?$/i,

@@ -225,3 +229,3 @@ extractComments = true,

* @param {string} file
* @returns {WebpackError}
* @returns {Error}
*/

@@ -238,4 +242,3 @@

builtWarning.hideStack = true;
builtWarning.file = file; // @ts-ignore
builtWarning.file = file;
return builtWarning;

@@ -247,13 +250,12 @@ }

* @param {string} file
* @param {SourceMapConsumer} [sourceMap]
* @param {Compilation["requestShortener"]} [requestShortener]
* @param {SourceMapConsumer} [sourceMap]
* @returns {WebpackError}
* @returns {Error}
*/
static buildError(error, file, requestShortener, sourceMap) {
static buildError(error, file, sourceMap, requestShortener) {
/**
* @type {Error & { file: string }}
*/
// @ts-ignore
let builtError;

@@ -264,4 +266,3 @@

builtError = new Error(`${file} from Terser plugin\n${error}`);
builtError.file = file; // @ts-ignore
builtError.file = file;
return builtError;

@@ -279,4 +280,3 @@ }

builtError = new Error(`${file} from Terser plugin\n${error.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${error.line},${error.col}]${error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""}`);
builtError.file = file; // @ts-ignore
builtError.file = file;
return builtError;

@@ -287,4 +287,3 @@ } // @ts-ignore

builtError = new Error(`${file} from Terser plugin\n${error.message} [${file}:${error.line},${error.col}]${error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""}`);
builtError.file = file; // @ts-ignore
builtError.file = file;
return builtError;

@@ -296,4 +295,3 @@ }

builtError = new Error(`${file} from Terser plugin\n${typeof error.message !== "undefined" ? error.message : ""}\n${error.stack}`);
builtError.file = file; // @ts-ignore
builtError.file = file;
return builtError;

@@ -304,4 +302,3 @@ } // @ts-ignore

builtError = new Error(`${file} from Terser plugin\n${error.message}`);
builtError.file = file; // @ts-ignore
builtError.file = file;
return builtError;

@@ -337,6 +334,8 @@ }

let numberOfAssets = 0;
const assetsForJob = await Promise.all(Object.keys(assets).filter(name => {
const assetsForMinify = await Promise.all(Object.keys(assets).filter(name => {
const {
info
} = compilation.getAsset(name);
} =
/** @type {Asset} */
compilation.getAsset(name);

@@ -359,3 +358,5 @@ if ( // Skip double minimize assets from child compilation

source
} = compilation.getAsset(name);
} =
/** @type {Asset} */
compilation.getAsset(name);
const eTag = cache.getLazyHashedEtag(source);

@@ -377,4 +378,9 @@ const cacheItem = cache.getItemCache(name, eTag);

}));
if (assetsForMinify.length === 0) {
return;
}
/** @type {undefined | (() => MinimizerWorker<T>)} */
let getWorker;

@@ -397,3 +403,5 @@ /** @type {undefined | MinimizerWorker<T>} */

initializedWorker = new _jestWorker.Worker(require.resolve("./minify"), {
initializedWorker =
/** @type {MinimizerWorker<T>} */
new _jestWorker.Worker(require.resolve("./minify"), {
numWorkers: numberOfWorkers,

@@ -419,3 +427,2 @@ enableWorkerThreads: true

const limit = (0, _pLimit.default)(getWorker && numberOfAssets > 0 ? numberOfWorkers : Infinity);
const {

@@ -433,4 +440,4 @@ SourceMapSource,

for (const asset of assetsForJob) {
scheduledTasks.push(limit(async () => {
for (const asset of assetsForMinify) {
scheduledTasks.push(async () => {
const {

@@ -459,6 +466,12 @@ name,

if (TerserPlugin.isSourceMap(map)) {
inputSourceMap = map;
inputSourceMap =
/** @type {RawSourceMap} */
map;
} else {
inputSourceMap = map;
compilation.warnings.push(new Error(`${name} contains invalid source map`));
inputSourceMap =
/** @type {RawSourceMap} */
map;
compilation.warnings.push(
/** @type {WebpackError} */
new Error(`${name} contains invalid source map`));
}

@@ -502,5 +515,9 @@ }

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

@@ -510,3 +527,5 @@ }

if (typeof output.code === "undefined") {
compilation.errors.push(new Error(`${name} from Terser plugin\nMinimizer doesn't return result`));
compilation.errors.push(
/** @type {WebpackError} */
new Error(`${name} from Terser plugin\nMinimizer doesn't return result`));
return;

@@ -529,5 +548,7 @@ }

*/
item => TerserPlugin.buildError(item, name, // eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined, hasSourceMap ? new _sourceMap.SourceMapConsumer(inputSourceMap) : // eslint-disable-next-line no-undefined
undefined));
item => TerserPlugin.buildError(item, name, hasSourceMap ? new _sourceMap.SourceMapConsumer(
/** @type {RawSourceMap} */
inputSourceMap) : // eslint-disable-next-line no-undefined
undefined, // eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined));
}

@@ -546,3 +567,5 @@

if (output.map) {
output.source = new SourceMapSource(output.code, name, output.map, input, inputSourceMap, true);
output.source = new SourceMapSource(output.code, name, output.map, input,
/** @type {RawSourceMap} */
inputSourceMap, true);
} else {

@@ -605,19 +628,15 @@ output.source = new RawSource(output.code);

if (output.warnings && output.warnings.length > 0) {
output.warnings.forEach(
/**
* @param {Error} warning
*/
warning => {
compilation.warnings.push(warning);
});
for (const warning of output.warnings) {
compilation.warnings.push(
/** @type {WebpackError} */
warning);
}
}
if (output.errors && output.errors.length > 0) {
output.errors.forEach(
/**
* @param {Error & { filename?: string }} error
*/
error => {
compilation.errors.push(error);
});
for (const error of output.errors) {
compilation.errors.push(
/** @type {WebpackError} */
error);
}
}

@@ -649,6 +668,9 @@ /** @type {Record<string, any>} */

compilation.updateAsset(name, source, newInfo);
}));
});
}
await Promise.all(scheduledTasks);
const limit = getWorker && numberOfAssets > 0 ?
/** @type {number} */
numberOfWorkers : scheduledTasks.length;
await (0, _utils.throttleAll)(limit, scheduledTasks);

@@ -668,3 +690,5 @@ if (initializedWorker) {

async (previousPromise, [from, value]) => {
const previous = await previousPromise;
const previous =
/** @type {ExtractedCommentsInfoWIthFrom | undefined} **/
await previousPromise;
const {

@@ -720,3 +744,5 @@ commentsFilename,

};
}, Promise.resolve());
},
/** @type {Promise<unknown>} */
Promise.resolve());
}

@@ -723,0 +749,0 @@ /**

@@ -37,3 +37,8 @@ "use strict";

// eslint-disable-next-line no-param-reassign
const evaluatedOptions = // eslint-disable-next-line no-new-func
const evaluatedOptions =
/**
* @template T
* @type {import("./index.js").InternalOptions<T>}
* */
// eslint-disable-next-line no-new-func
new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);

@@ -40,0 +45,0 @@ return minify(evaluatedOptions);

@@ -6,6 +6,7 @@ "use strict";

});
exports.esbuildMinify = esbuildMinify;
exports.swcMinify = swcMinify;
exports.terserMinify = terserMinify;
exports.throttleAll = throttleAll;
exports.uglifyJsMinify = uglifyJsMinify;
exports.swcMinify = swcMinify;
exports.esbuildMinify = esbuildMinify;

@@ -39,3 +40,59 @@ /** @typedef {import("source-map").RawSourceMap} RawSourceMap */

*/
const notSettled = Symbol(`not-settled`);
/**
* @template T
* @typedef {() => Promise<T>} Task
*/
/**
* Run tasks with limited concurency.
* @template T
* @param {number} limit - Limit of tasks that run at once.
* @param {Task<T>[]} tasks - List of tasks to run.
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
*/
function throttleAll(limit, tasks) {
if (!Number.isInteger(limit) || limit < 1) {
throw new TypeError(`Expected \`limit\` to be a finite number > 0, got \`${limit}\` (${typeof limit})`);
}
if (!Array.isArray(tasks) || !tasks.every(task => typeof task === `function`)) {
throw new TypeError(`Expected \`tasks\` to be a list of functions returning a promise`);
}
return new Promise((resolve, reject) => {
const result = Array(tasks.length).fill(notSettled);
const entries = tasks.entries();
const next = () => {
const {
done,
value
} = entries.next();
if (done) {
const isLast = !result.includes(notSettled);
if (isLast) resolve(
/** @type{T[]} **/
result);
return;
}
const [index, task] = value;
/**
* @param {T} x
*/
const onFulfilled = x => {
result[index] = x;
next();
};
task().then(onFulfilled, reject);
};
Array(limit).fill(0).forEach(next);
});
}
/* istanbul ignore next */

@@ -50,2 +107,4 @@

*/
async function terserMinify(input, sourceMap, minimizerOptions, extractComments) {

@@ -123,3 +182,5 @@ /**

if (condition[key] === "some") {
condition[key] = (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);

@@ -129,5 +190,11 @@ break;

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

@@ -137,5 +204,9 @@ break;

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

@@ -236,6 +307,10 @@ regex.test(comment.value);

return {
code: result.code,
code:
/** @type {string} **/
result.code,
// @ts-ignore
// eslint-disable-next-line no-undefined
map: result.map ? result.map : undefined,
map: result.map ?
/** @type {RawSourceMap} **/
result.map : undefined,
extractedComments

@@ -334,3 +409,5 @@ };

if (condition[key] === "some") {
condition[key] = (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);

@@ -340,5 +417,11 @@ break;

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

@@ -348,5 +431,9 @@ break;

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

@@ -569,6 +656,6 @@ regex.test(comment.value);

const esbuild = require("esbuild"); // Copy `swc` options
const esbuild = require("esbuild"); // Copy `esbuild` options
const esbuildOptions = buildEsbuildOptions(minimizerOptions); // Let `swc` generate a SourceMap
const esbuildOptions = buildEsbuildOptions(minimizerOptions); // Let `esbuild` generate a SourceMap

@@ -587,3 +674,12 @@ if (sourceMap) {

map: result.map ? JSON.parse(result.map) : undefined,
warnings: result.warnings ? result.warnings.map(item => item.toString()) : []
warnings: result.warnings.length > 0 ? result.warnings.map(item => {
return {
name: "Warning",
source: item.location && item.location.file,
line: item.location && item.location.line,
column: item.location && item.location.column,
plugin: item.pluginName,
message: `${item.text}${item.detail ? `\nDetails:\n${item.detail}` : ""}${item.notes.length > 0 ? `\n\nNotes:\n${item.notes.map(note => `${note.location ? `[${note.location.file}:${note.location.line}:${note.location.column}] ` : ""}${note.text}${note.location ? `\nSuggestion: ${note.location.suggestion}` : ""}${note.location ? `\nLine text:\n${note.location.lineText}\n` : ""}`).join("\n")}` : ""}`
};
}) : []
};

@@ -590,0 +686,0 @@ }

{
"name": "terser-webpack-plugin",
"version": "5.2.4",
"version": "5.2.5",
"description": "Terser plugin for webpack",

@@ -59,3 +59,2 @@ "license": "MIT",

"jest-worker": "^27.0.6",
"p-limit": "^3.1.0",
"schema-utils": "^3.1.1",

@@ -70,4 +69,4 @@ "serialize-javascript": "^6.0.0",

"@babel/preset-env": "^7.14.9",
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@commitlint/cli": "^14.1.0",
"@commitlint/config-conventional": "^14.1.0",
"@swc/core": "^1.2.82",

@@ -82,3 +81,3 @@ "@types/serialize-javascript": "^5.0.1",

"del-cli": "^3.0.1",
"esbuild": "^0.12.23",
"esbuild": "^0.13.3",
"eslint": "^7.32.0",

@@ -85,0 +84,0 @@ "eslint-config-prettier": "^8.3.0",

@@ -17,3 +17,3 @@ <div align="center">

This plugin uses [terser](https://github.com/terser-js/terser) to minify/minimize your JavaScript.
This plugin uses [terser](https://github.com/terser/terser) to minify/minimize your JavaScript.

@@ -60,11 +60,11 @@ ## Getting Started

| Name | Type | Default | Description |
| :---------------------------------------: | :-----------------------------------------------------------------------------: | :-------------------------------------------------------------: | :--------------------------------------------------------------------------- |
| **[`test`](#test)** | `String\|RegExp\|Array<String\|RegExp>` | `/\.m?js(\?.*)?$/i` | Test to match files against. |
| **[`include`](#include)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to include. |
| **[`exclude`](#exclude)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to exclude. |
| **[`parallel`](#parallel)** | `Boolean\|Number` | `true` | Use multi-process parallel running to improve the build speed. |
| **[`minify`](#minify)** | `Function` | `TerserPlugin.terserMinify` | Allows you to override default minify function. |
| **[`terserOptions`](#terseroptions)** | `Object` | [`default`](https://github.com/terser-js/terser#minify-options) | Terser [minify options](https://github.com/terser-js/terser#minify-options). |
| **[`extractComments`](#extractcomments)** | `Boolean\|String\|RegExp\|Function<(node, comment) -> Boolean\|Object>\|Object` | `true` | Whether comments shall be extracted to a separate file. |
| Name | Type | Default | Description |
| :---------------------------------------: | :-----------------------------------------------------------------------------: | :----------------------------------------------------------: | :------------------------------------------------------------------------ |
| **[`test`](#test)** | `String\|RegExp\|Array<String\|RegExp>` | `/\.m?js(\?.*)?$/i` | Test to match files against. |
| **[`include`](#include)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to include. |
| **[`exclude`](#exclude)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to exclude. |
| **[`parallel`](#parallel)** | `Boolean\|Number` | `true` | Use multi-process parallel running to improve the build speed. |
| **[`minify`](#minify)** | `Function` | `TerserPlugin.terserMinify` | Allows you to override default minify function. |
| **[`terserOptions`](#terseroptions)** | `Object` | [`default`](https://github.com/terser/terser#minify-options) | Terser [minify options](https://github.com/terser/terser#minify-options). |
| **[`extractComments`](#extractcomments)** | `Boolean\|String\|RegExp\|Function<(node, comment) -> Boolean\|Object>\|Object` | `true` | Whether comments shall be extracted to a separate file. |

@@ -193,3 +193,3 @@ ### `test`

Allows you to override default minify function.
By default plugin uses [terser](https://github.com/terser-js/terser) package.
By default plugin uses [terser](https://github.com/terser/terser) package.
Useful for using and testing unpublished versions or forks.

@@ -251,5 +251,5 @@

Type: `Object`
Default: [default](https://github.com/terser-js/terser#minify-options)
Default: [default](https://github.com/terser/terser#minify-options)
Terser [options](https://github.com/terser-js/terser#minify-options).
Terser [options](https://github.com/terser/terser#minify-options).

@@ -256,0 +256,0 @@ **webpack.config.js**

@@ -225,3 +225,3 @@ export default TerserPlugin;

* @param {string} file
* @returns {WebpackError}
* @returns {Error}
*/

@@ -233,5 +233,5 @@ private static buildWarning;

* @param {string} file
* @param {SourceMapConsumer} [sourceMap]
* @param {Compilation["requestShortener"]} [requestShortener]
* @param {SourceMapConsumer} [sourceMap]
* @returns {WebpackError}
* @returns {Error}
*/

@@ -238,0 +238,0 @@ private static buildError;

@@ -0,1 +1,2 @@

export type Task<T> = () => Promise<T>;
export type RawSourceMap = import("source-map").RawSourceMap;

@@ -18,18 +19,14 @@ export type TerserFormatOptions = import("terser").FormatOptions;

export type ExtractedComments = Array<string>;
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
/** @typedef {import("terser").FormatOptions} TerserFormatOptions */
/** @typedef {import("terser").MinifyOptions} TerserOptions */
/** @typedef {import("terser").ECMA} TerserECMA */
/** @typedef {import("./index.js").ExtractCommentsOptions} ExtractCommentsOptions */
/** @typedef {import("./index.js").ExtractCommentsFunction} ExtractCommentsFunction */
/** @typedef {import("./index.js").ExtractCommentsCondition} ExtractCommentsCondition */
/** @typedef {import("./index.js").Input} Input */
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
/** @typedef {import("./index.js").PredefinedOptions} PredefinedOptions */
/**
* @typedef {{ [key: string]: any }} CustomOptions
* @template T
* @typedef {() => Promise<T>} Task
*/
/**
* @typedef {Array<string>} ExtractedComments
* Run tasks with limited concurency.
* @template T
* @param {number} limit - Limit of tasks that run at once.
* @param {Task<T>[]} tasks - List of tasks to run.
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
*/
export function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;
/**

@@ -36,0 +33,0 @@ * @param {Input} input

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc