Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

terser-webpack-plugin

Package Overview
Dependencies
Maintainers
3
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

terser-webpack-plugin - npm Package Compare versions

Comparing version 5.3.6 to 5.3.7

244

dist/index.js
"use strict";
const path = require("path");
const os = require("os");
const {

@@ -11,13 +9,9 @@ TraceMap,

} = require("@jridgewell/trace-mapping");
const {
validate
} = require("schema-utils");
const serialize = require("serialize-javascript");
const {
Worker
} = require("jest-worker");
const {

@@ -30,24 +24,15 @@ throttleAll,

} = require("./utils");
const schema = require("./options.json");
const {
minify
} = require("./minify");
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").Compilation} Compilation */
/** @typedef {import("webpack").WebpackError} WebpackError */
/** @typedef {import("webpack").Asset} Asset */
/** @typedef {import("./utils.js").TerserECMA} TerserECMA */
/** @typedef {import("./utils.js").TerserOptions} TerserOptions */
/** @typedef {import("jest-worker").Worker} JestWorker */
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */

@@ -183,4 +168,2 @@

*/
class TerserPlugin {

@@ -191,16 +174,11 @@ /**

constructor(options) {
validate(
/** @type {Schema} */
schema, options || {}, {
validate( /** @type {Schema} */schema, options || {}, {
name: "Terser Plugin",
baseDataPath: "options"
}); // TODO make `minimizer` option instead `minify` and `terserOptions` in the next major release, also rename `terserMinify` to `terserMinimize`
});
// TODO make `minimizer` option instead `minify` and `terserOptions` in the next major release, also rename `terserMinify` to `terserMinimize`
const {
minify =
/** @type {MinimizerImplementation<T>} */
terserMinify,
terserOptions =
/** @type {MinimizerOptions<T>} */
{},
minify = /** @type {MinimizerImplementation<T>} */terserMinify,
terserOptions = /** @type {MinimizerOptions<T>} */{},
test = /\.[cm]?js(\?.*)?$/i,

@@ -212,2 +190,3 @@ extractComments = true,

} = options || {};
/**

@@ -217,3 +196,2 @@ * @private

*/
this.options = {

@@ -231,2 +209,3 @@ test,

}
/**

@@ -237,4 +216,2 @@ * @private

*/
static isSourceMap(input) {

@@ -245,2 +222,3 @@ // All required options for `new TraceMap(...options)`

}
/**

@@ -252,4 +230,2 @@ * @private

*/
static buildWarning(warning, file) {

@@ -266,2 +242,3 @@ /**

}
/**

@@ -275,4 +252,2 @@ * @private

*/
static buildError(error, file, sourceMap, requestShortener) {

@@ -283,3 +258,2 @@ /**

let builtError;
if (typeof error === "string") {

@@ -290,3 +264,2 @@ builtError = new Error(`${file} from Terser plugin\n${error}`);

}
if (error.line) {

@@ -297,3 +270,2 @@ const original = sourceMap && originalPositionFor(sourceMap, {

});
if (original && original.source && requestShortener) {

@@ -304,3 +276,2 @@ 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 = 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")}` : ""}`);

@@ -310,3 +281,2 @@ builtError.file = file;

}
if (error.stack) {

@@ -317,3 +287,2 @@ builtError = new Error(`${file} from Terser plugin\n${typeof error.message !== "undefined" ? error.message : ""}\n${error.stack}`);

}
builtError = new Error(`${file} from Terser plugin\n${error.message}`);

@@ -323,2 +292,3 @@ builtError.file = file;

}
/**

@@ -329,4 +299,2 @@ * @private

*/
static getAvailableNumberOfCores(parallel) {

@@ -340,2 +308,3 @@ // In some cases cpus() returns undefined

}
/**

@@ -349,4 +318,2 @@ * @private

*/
async optimize(compiler, compilation, assets, optimizeOptions) {

@@ -358,17 +325,15 @@ const cache = compilation.getCache("TerserWebpackPlugin");

info
} =
/** @type {Asset} */
compilation.getAsset(name);
if ( // Skip double minimize assets from child compilation
info.minimized || // Skip minimizing for extracted comments assets
} = /** @type {Asset} */compilation.getAsset(name);
if (
// Skip double minimize assets from child compilation
info.minimized ||
// Skip minimizing for extracted comments assets
info.extractedComments) {
return false;
}
if (!compiler.webpack.ModuleFilenameHelpers.matchObject.bind( // eslint-disable-next-line no-undefined
if (!compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined, this.options)(name)) {
return false;
}
return true;

@@ -379,4 +344,3 @@ }).map(async name => {

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

@@ -386,7 +350,5 @@ const eTag = cache.getLazyHashedEtag(source);

const output = await cacheItem.getPromise();
if (!output) {
numberOfAssets += 1;
}
return {

@@ -400,21 +362,16 @@ name,

}));
if (assetsForMinify.length === 0) {
return;
}
/** @type {undefined | (() => MinimizerWorker<T>)} */
let getWorker;
/** @type {undefined | MinimizerWorker<T>} */
let initializedWorker;
/** @type {undefined | number} */
let numberOfWorkers;
if (optimizeOptions.availableNumberOfCores > 0) {
// Do not create unnecessary workers when the number of files is less than the available cores, it saves memory
numberOfWorkers = Math.min(numberOfAssets, optimizeOptions.availableNumberOfCores); // eslint-disable-next-line consistent-return
numberOfWorkers = Math.min(numberOfAssets, optimizeOptions.availableNumberOfCores);
// eslint-disable-next-line consistent-return
getWorker = () => {

@@ -424,26 +381,21 @@ if (initializedWorker) {

}
initializedWorker = /** @type {MinimizerWorker<T>} */
initializedWorker =
/** @type {MinimizerWorker<T>} */
new Worker(require.resolve("./minify"), {
numWorkers: numberOfWorkers,
enableWorkerThreads: true
}); // https://github.com/facebook/jest/issues/8872#issuecomment-524822081
});
// https://github.com/facebook/jest/issues/8872#issuecomment-524822081
const workerStdout = initializedWorker.getStdout();
if (workerStdout) {
workerStdout.on("data", chunk => process.stdout.write(chunk));
}
const workerStderr = initializedWorker.getStderr();
if (workerStderr) {
workerStderr.on("data", chunk => process.stderr.write(chunk));
}
return initializedWorker;
};
}
const {

@@ -454,9 +406,7 @@ SourceMapSource,

} = compiler.webpack.sources;
/** @typedef {{ extractedCommentsSource : import("webpack").sources.RawSource, commentsFilename: string }} ExtractedCommentsInfo */
/** @type {Map<string, ExtractedCommentsInfo>} */
const allExtractedComments = new Map();
const scheduledTasks = [];
for (const asset of assetsForMinify) {

@@ -473,7 +423,5 @@ scheduledTasks.push(async () => {

} = asset;
if (!output) {
let input;
/** @type {SourceMapInput | undefined} */
let inputSourceMap;

@@ -485,23 +433,17 @@ const {

input = sourceFromInputSource;
if (map) {
if (!TerserPlugin.isSourceMap(map)) {
compilation.warnings.push(
/** @type {WebpackError} */
compilation.warnings.push( /** @type {WebpackError} */
new Error(`${name} contains invalid source map`));
} else {
inputSourceMap =
/** @type {SourceMapInput} */
map;
inputSourceMap = /** @type {SourceMapInput} */map;
}
}
if (Buffer.isBuffer(input)) {
input = input.toString();
}
/**
* @type {InternalOptions<T>}
*/
const options = {

@@ -514,3 +456,4 @@ name,

// @ts-ignore https://github.com/Microsoft/TypeScript/issues/10727
options: { ...this.options.minimizer.options
options: {
...this.options.minimizer.options
}

@@ -520,3 +463,2 @@ },

};
if (typeof options.minimizer.options.module === "undefined") {

@@ -531,7 +473,5 @@ if (typeof info.javascriptModule !== "undefined") {

}
if (typeof options.minimizer.options.ecma === "undefined") {
options.minimizer.options.ecma = TerserPlugin.getEcmaVersion(compiler.options.output.environment || {});
}
try {

@@ -541,19 +481,17 @@ output = await (getWorker ? getWorker().transform(serialize(options)) : minify(options));

const hasSourceMap = inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap);
compilation.errors.push(
/** @type {WebpackError} */
TerserPlugin.buildError(error, name, hasSourceMap ? new TraceMap(
/** @type {SourceMapInput} */
inputSourceMap) : // eslint-disable-next-line no-undefined
undefined, // eslint-disable-next-line no-undefined
compilation.errors.push( /** @type {WebpackError} */
TerserPlugin.buildError(error, name, hasSourceMap ? new TraceMap( /** @type {SourceMapInput} */inputSourceMap) :
// eslint-disable-next-line no-undefined
undefined,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined));
return;
}
if (typeof output.code === "undefined") {
compilation.errors.push( /** @type {WebpackError} */
if (typeof output.code === "undefined") {
compilation.errors.push(
/** @type {WebpackError} */
new Error(`${name} from Terser plugin\nMinimizer doesn't return result`));
return;
}
if (output.warnings && output.warnings.length > 0) {

@@ -566,3 +504,2 @@ output.warnings = output.warnings.map(

}
if (output.errors && output.errors.length > 0) {

@@ -574,13 +511,10 @@ const hasSourceMap = inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap);

*/
item => TerserPlugin.buildError(item, name, hasSourceMap ? new TraceMap(
/** @type {SourceMapInput} */
inputSourceMap) : // eslint-disable-next-line no-undefined
undefined, // eslint-disable-next-line no-undefined
item => TerserPlugin.buildError(item, name, hasSourceMap ? new TraceMap( /** @type {SourceMapInput} */inputSourceMap) :
// eslint-disable-next-line no-undefined
undefined,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined));
}
let shebang;
if (
/** @type {ExtractCommentsObject} */
if ( /** @type {ExtractCommentsObject} */
this.options.extractComments.banner !== false && output.extractedComments && output.extractedComments.length > 0 && output.code.startsWith("#!")) {

@@ -591,14 +525,9 @@ const firstNewlinePosition = output.code.indexOf("\n");

}
if (output.map) {
output.source = new SourceMapSource(output.code, name, output.map, input,
/** @type {SourceMapInput} */
inputSourceMap, true);
output.source = new SourceMapSource(output.code, name, output.map, input, /** @type {SourceMapInput} */inputSourceMap, true);
} else {
output.source = new RawSource(output.code);
}
if (output.extractedComments && output.extractedComments.length > 0) {
const commentsFilename =
/** @type {ExtractCommentsObject} */
const commentsFilename = /** @type {ExtractCommentsObject} */
this.options.extractComments.filename || "[file].LICENSE.txt[query]";

@@ -608,3 +537,2 @@ let query = "";

const querySplit = filename.indexOf("?");
if (querySplit >= 0) {

@@ -614,3 +542,2 @@ query = filename.slice(querySplit);

}
const lastSlashIndex = filename.lastIndexOf("/");

@@ -624,15 +551,12 @@ const basename = lastSlashIndex === -1 ? filename : filename.slice(lastSlashIndex + 1);

output.commentsFilename = compilation.getPath(commentsFilename, data);
let banner; // Add a banner to the original file
let banner;
if (
/** @type {ExtractCommentsObject} */
// Add a banner to the original file
if ( /** @type {ExtractCommentsObject} */
this.options.extractComments.banner !== false) {
banner =
/** @type {ExtractCommentsObject} */
banner = /** @type {ExtractCommentsObject} */
this.options.extractComments.banner || `For license information please see ${path.relative(path.dirname(name), output.commentsFilename).replace(/\\/g, "/")}`;
if (typeof banner === "function") {
banner = banner(output.commentsFilename);
}
if (banner) {

@@ -642,7 +566,5 @@ output.source = new ConcatSource(shebang ? `${shebang}\n` : "", `/*! ${banner} */\n`, output.source);

}
const extractedCommentsString = output.extractedComments.sort().join("\n\n");
output.extractedCommentsSource = new RawSource(`${extractedCommentsString}\n`);
}
await cacheItem.storePromise({

@@ -656,21 +578,14 @@ source: output.source,

}
if (output.warnings && output.warnings.length > 0) {
for (const warning of output.warnings) {
compilation.warnings.push(
/** @type {WebpackError} */
warning);
compilation.warnings.push( /** @type {WebpackError} */warning);
}
}
if (output.errors && output.errors.length > 0) {
for (const error of output.errors) {
compilation.errors.push(
/** @type {WebpackError} */
error);
compilation.errors.push( /** @type {WebpackError} */error);
}
}
/** @type {Record<string, any>} */
const newInfo = {

@@ -682,4 +597,5 @@ minimized: true

extractedCommentsSource
} = output; // Write extracted comments to commentsFilename
} = output;
// Write extracted comments to commentsFilename
if (extractedCommentsSource) {

@@ -697,18 +613,12 @@ const {

}
compilation.updateAsset(name, source, newInfo);
});
}
const limit = getWorker && numberOfAssets > 0 ?
/** @type {number} */
numberOfWorkers : scheduledTasks.length;
const limit = getWorker && numberOfAssets > 0 ? /** @type {number} */numberOfWorkers : scheduledTasks.length;
await throttleAll(limit, scheduledTasks);
if (initializedWorker) {
await initializedWorker.end();
}
/** @typedef {{ source: import("webpack").sources.Source, commentsFilename: string, from: string }} ExtractedCommentsInfoWIthFrom */
await Array.from(allExtractedComments).sort().reduce(

@@ -721,4 +631,3 @@ /**

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

@@ -729,3 +638,2 @@ const {

} = value;
if (previous && previous.commentsFilename === commentsFilename) {

@@ -740,12 +648,6 @@ const {

let source = await cache.getPromise(name, eTag);
if (!source) {
source = new ConcatSource(Array.from(new Set([...
/** @type {string}*/
prevSource.source().split("\n\n"), ...
/** @type {string}*/
extractedCommentsSource.source().split("\n\n")])).join("\n\n"));
source = new ConcatSource(Array.from(new Set([... /** @type {string}*/prevSource.source().split("\n\n"), ... /** @type {string}*/extractedCommentsSource.source().split("\n\n")])).join("\n\n"));
await cache.storePromise(name, eTag, source);
}
compilation.updateAsset(commentsFilename, source);

@@ -758,5 +660,3 @@ return {

}
const existingAsset = compilation.getAsset(commentsFilename);
if (existingAsset) {

@@ -769,3 +669,2 @@ return {

}
compilation.emitAsset(commentsFilename, extractedCommentsSource, {

@@ -779,6 +678,5 @@ extractedComments: true

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

@@ -789,4 +687,2 @@ * @private

*/
static getEcmaVersion(environment) {

@@ -796,11 +692,11 @@ // ES 6th

return 2015;
} // ES 11th
}
// ES 11th
if (environment.bigIntLiteral || environment.dynamicImport) {
return 2020;
}
return 5;
}
/**

@@ -810,4 +706,2 @@ * @param {Compiler} compiler

*/
apply(compiler) {

@@ -837,13 +731,7 @@ const pluginName = this.constructor.name;

formatFlag
}) => minimized ?
/** @type {Function} */
green(
/** @type {Function} */
formatFlag("minimized")) : "");
}) => minimized ? /** @type {Function} */green( /** @type {Function} */formatFlag("minimized")) : "");
});
});
}
}
TerserPlugin.terserMinify = terserMinify;

@@ -850,0 +738,0 @@ TerserPlugin.uglifyJsMinify = uglifyJsMinify;

"use strict";
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
/** @typedef {import("./index.js").CustomOptions} CustomOptions */

@@ -27,2 +26,3 @@

}
/**

@@ -32,4 +32,2 @@ * @param {string} options

*/
async function transform(options) {

@@ -44,2 +42,3 @@ // 'use strict' => this === undefined (Clean Scope)

* */
// eslint-disable-next-line no-new-func

@@ -49,3 +48,2 @@ new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);

}
module.exports = {

@@ -52,0 +50,0 @@ minify,

"use strict";
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
/** @typedef {import("terser").FormatOptions} TerserFormatOptions */
/** @typedef {import("terser").MinifyOptions} TerserOptions */
/** @typedef {import("terser").CompressOptions} TerserCompressOptions */
/** @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 {import("./index.js").CustomOptions} CustomOptions */

@@ -30,3 +19,5 @@

*/
const notSettled = Symbol(`not-settled`);
/**

@@ -38,3 +29,3 @@ * @template T

/**
* Run tasks with limited concurency.
* Run tasks with limited concurrency.
* @template T

@@ -45,3 +36,2 @@ * @param {number} limit - Limit of tasks that run at once.

*/
function throttleAll(limit, tasks) {

@@ -51,11 +41,8 @@ if (!Number.isInteger(limit) || limit < 1) {

}
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 = () => {

@@ -66,16 +53,12 @@ const {

} = entries.next();
if (done) {
const isLast = !result.includes(notSettled);
if (isLast) resolve(
/** @type{T[]} **/
result);
if (isLast) resolve( /** @type{T[]} **/result);
return;
}
const [index, task] = value;
const [index, task] = value;
/**
* @param {T} x
*/
const onFulfilled = x => {

@@ -85,11 +68,9 @@ result[index] = x;

};
task().then(onFulfilled, reject);
};
Array(limit).fill(0).forEach(next);
});
}
/* istanbul ignore next */
/**

@@ -102,4 +83,2 @@ * @param {Input} input

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

@@ -114,2 +93,3 @@ /**

};
/**

@@ -120,4 +100,2 @@ * @param {TerserOptions & { sourceMap: undefined } & ({ output: TerserFormatOptions & { beautify: boolean } } | { format: TerserFormatOptions & { beautify: boolean } })} terserOptions

*/
const buildComments = (terserOptions, extractedComments) => {

@@ -127,3 +105,2 @@ /** @type {{ [index: string]: ExtractCommentsCondition }} */

let comments;
if (terserOptions.format) {

@@ -138,5 +115,3 @@ ({

}
condition.preserve = typeof comments !== "undefined" ? comments : false;
if (typeof extractComments === "boolean" && extractComments) {

@@ -155,5 +130,5 @@ condition.extract = "some";

condition.extract = false;
} // Ensure that both conditions are functions
}
// Ensure that both conditions are functions
["preserve", "extract"].forEach(key => {

@@ -163,5 +138,3 @@ /** @type {undefined | string} */

/** @type {undefined | RegExp} */
let regex;
switch (typeof condition[key]) {

@@ -171,54 +144,33 @@ case "boolean":

break;
case "function":
break;
case "string":
if (condition[key] === "all") {
condition[key] = () => true;
break;
}
if (condition[key] === "some") {
condition[key] =
/** @type {ExtractCommentsFunction} */
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);
regexStr = /** @type {string} */condition[key];
condition[key] = /** @type {ExtractCommentsFunction} */
(astNode, comment) => new RegExp( /** @type {string} */regexStr).test(comment.value);
break;
default:
regex =
/** @type {RegExp} */
condition[key];
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);
}
}); // Redefine the comments function to extract and preserve
// Redefine the comments function to extract and preserve
// comments according to the two conditions
return (astNode, comment) => {
if (
/** @type {{ extract: ExtractCommentsFunction }} */
if ( /** @type {{ extract: ExtractCommentsFunction }} */
condition.extract(astNode, comment)) {
const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`;
// Don't include duplicate comments
if (!extractedComments.includes(commentText)) {

@@ -228,9 +180,7 @@ extractedComments.push(commentText);

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

@@ -240,8 +190,8 @@ * @param {PredefinedOptions & TerserOptions} [terserOptions={}]

*/
const buildTerserOptions = (terserOptions = {}) => {
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return { ...terserOptions,
compress: typeof terserOptions.compress === "boolean" ? terserOptions.compress ? {} : false : { ...terserOptions.compress
return {
...terserOptions,
compress: typeof terserOptions.compress === "boolean" ? terserOptions.compress ? {} : false : {
...terserOptions.compress
},

@@ -252,3 +202,4 @@ // ecma: terserOptions.ecma,

// keep_fnames: terserOptions.keep_fnames,
mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === "boolean" ? terserOptions.mangle : { ...terserOptions.mangle
mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === "boolean" ? terserOptions.mangle : {
...terserOptions.mangle
},

@@ -269,3 +220,4 @@ // module: terserOptions.module,

}),
parse: { ...terserOptions.parse
parse: {
...terserOptions.parse
},

@@ -275,15 +227,15 @@ // safari10: terserOptions.safari10,

// eslint-disable-next-line no-undefined
sourceMap: undefined // toplevel: terserOptions.toplevel
sourceMap: undefined
// toplevel: terserOptions.toplevel
};
}; // eslint-disable-next-line global-require
};
// eslint-disable-next-line global-require
const {
minify
} = require("terser"); // Copy `terser` options
} = require("terser");
// Copy `terser` options
const terserOptions = buildTerserOptions(minimizerOptions);
const terserOptions = buildTerserOptions(minimizerOptions); // Let terser generate a SourceMap
// Let terser generate a SourceMap
if (sourceMap) {

@@ -295,7 +247,5 @@ // @ts-ignore

}
/** @type {ExtractedComments} */
const extractedComments = [];
if (terserOptions.output) {

@@ -306,3 +256,2 @@ terserOptions.output.comments = buildComments(terserOptions, extractedComments);

}
if (terserOptions.compress) {

@@ -312,5 +261,5 @@ // More optimizations

terserOptions.compress.ecma = terserOptions.ecma;
} // https://github.com/webpack/webpack/issues/16135
}
// https://github.com/webpack/webpack/issues/16135
if (terserOptions.ecma === 5 && typeof terserOptions.compress.arrows === "undefined") {

@@ -320,3 +269,2 @@ terserOptions.compress.arrows = false;

}
const [[filename, code]] = Object.entries(input);

@@ -327,31 +275,25 @@ const result = await minify({

return {
code:
/** @type {string} **/
result.code,
code: /** @type {string} **/result.code,
// @ts-ignore
// eslint-disable-next-line no-undefined
map: result.map ?
/** @type {SourceMapInput} **/
result.map : undefined,
map: result.map ? /** @type {SourceMapInput} **/result.map : undefined,
extractedComments
};
}
/**
* @returns {string | undefined}
*/
terserMinify.getMinimizerVersion = () => {
let packageJson;
try {
// eslint-disable-next-line global-require
packageJson = require("terser/package.json");
} catch (error) {// Ignore
} catch (error) {
// Ignore
}
return packageJson && packageJson.version;
};
/* istanbul ignore next */
/**

@@ -364,4 +306,2 @@ * @param {Input} input

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

@@ -376,2 +316,3 @@ /**

};
/**

@@ -382,4 +323,2 @@ * @param {import("uglify-js").MinifyOptions & { sourceMap: undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}} uglifyJsOptions

*/
const buildComments = (uglifyJsOptions, extractedComments) => {

@@ -392,3 +331,2 @@ /** @type {{ [index: string]: ExtractCommentsCondition }} */

condition.preserve = typeof comments !== "undefined" ? comments : false;
if (typeof extractComments === "boolean" && extractComments) {

@@ -407,5 +345,5 @@ condition.extract = "some";

condition.extract = false;
} // Ensure that both conditions are functions
}
// Ensure that both conditions are functions
["preserve", "extract"].forEach(key => {

@@ -415,5 +353,3 @@ /** @type {undefined | string} */

/** @type {undefined | RegExp} */
let regex;
switch (typeof condition[key]) {

@@ -423,54 +359,33 @@ case "boolean":

break;
case "function":
break;
case "string":
if (condition[key] === "all") {
condition[key] = () => true;
break;
}
if (condition[key] === "some") {
condition[key] =
/** @type {ExtractCommentsFunction} */
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);
regexStr = /** @type {string} */condition[key];
condition[key] = /** @type {ExtractCommentsFunction} */
(astNode, comment) => new RegExp( /** @type {string} */regexStr).test(comment.value);
break;
default:
regex =
/** @type {RegExp} */
condition[key];
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);
}
}); // Redefine the comments function to extract and preserve
// Redefine the comments function to extract and preserve
// comments according to the two conditions
return (astNode, comment) => {
if (
/** @type {{ extract: ExtractCommentsFunction }} */
if ( /** @type {{ extract: ExtractCommentsFunction }} */
condition.extract(astNode, comment)) {
const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`;
// Don't include duplicate comments
if (!extractedComments.includes(commentText)) {

@@ -480,9 +395,7 @@ extractedComments.push(commentText);

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

@@ -492,17 +405,20 @@ * @param {PredefinedOptions & import("uglify-js").MinifyOptions} [uglifyJsOptions={}]

*/
const buildUglifyJsOptions = (uglifyJsOptions = {}) => {
// eslint-disable-next-line no-param-reassign
delete minimizerOptions.ecma; // eslint-disable-next-line no-param-reassign
delete minimizerOptions.ecma;
// eslint-disable-next-line no-param-reassign
delete minimizerOptions.module;
delete minimizerOptions.module; // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return { ...uglifyJsOptions,
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return {
...uglifyJsOptions,
// warnings: uglifyJsOptions.warnings,
parse: { ...uglifyJsOptions.parse
parse: {
...uglifyJsOptions.parse
},
compress: typeof uglifyJsOptions.compress === "boolean" ? uglifyJsOptions.compress : { ...uglifyJsOptions.compress
compress: typeof uglifyJsOptions.compress === "boolean" ? uglifyJsOptions.compress : {
...uglifyJsOptions.compress
},
mangle: uglifyJsOptions.mangle == null ? true : typeof uglifyJsOptions.mangle === "boolean" ? uglifyJsOptions.mangle : { ...uglifyJsOptions.mangle
mangle: uglifyJsOptions.mangle == null ? true : typeof uglifyJsOptions.mangle === "boolean" ? uglifyJsOptions.mangle : {
...uglifyJsOptions.mangle
},

@@ -515,18 +431,19 @@ output: {

// eslint-disable-next-line no-undefined
sourceMap: undefined // toplevel: uglifyJsOptions.toplevel
sourceMap: undefined
// toplevel: uglifyJsOptions.toplevel
// nameCache: { ...uglifyJsOptions.toplevel },
// ie8: uglifyJsOptions.ie8,
// keep_fnames: uglifyJsOptions.keep_fnames,
};
}; // eslint-disable-next-line global-require, import/no-extraneous-dependencies
};
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const {
minify
} = require("uglify-js"); // Copy `uglify-js` options
} = require("uglify-js");
// Copy `uglify-js` options
const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions);
const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions); // Let terser generate a SourceMap
// Let terser generate a SourceMap
if (sourceMap) {

@@ -536,7 +453,7 @@ // @ts-ignore

}
/** @type {ExtractedComments} */
const extractedComments = [];
const extractedComments = []; // @ts-ignore
// @ts-ignore
uglifyJsOptions.output.comments = buildComments(uglifyJsOptions, extractedComments);

@@ -556,20 +473,18 @@ const [[filename, code]] = Object.entries(input);

}
/**
* @returns {string | undefined}
*/
uglifyJsMinify.getMinimizerVersion = () => {
let packageJson;
try {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
packageJson = require("uglify-js/package.json");
} catch (error) {// Ignore
} catch (error) {
// Ignore
}
return packageJson && packageJson.version;
};
/* istanbul ignore next */
/**

@@ -581,4 +496,2 @@ * @param {Input} input

*/
async function swcMinify(input, sourceMap, minimizerOptions) {

@@ -591,6 +504,9 @@ /**

// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return { ...swcOptions,
compress: typeof swcOptions.compress === "boolean" ? swcOptions.compress ? {} : false : { ...swcOptions.compress
return {
...swcOptions,
compress: typeof swcOptions.compress === "boolean" ? swcOptions.compress ? {} : false : {
...swcOptions.compress
},
mangle: swcOptions.mangle == null ? true : typeof swcOptions.mangle === "boolean" ? swcOptions.mangle : { ...swcOptions.mangle
mangle: swcOptions.mangle == null ? true : typeof swcOptions.mangle === "boolean" ? swcOptions.mangle : {
...swcOptions.mangle
},

@@ -606,10 +522,10 @@ // ecma: swcOptions.ecma,

};
}; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
};
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
const swc = require("@swc/core");
// Copy `swc` options
const swcOptions = buildSwcOptions(minimizerOptions);
const swc = require("@swc/core"); // Copy `swc` options
const swcOptions = buildSwcOptions(minimizerOptions); // Let `swc` generate a SourceMap
// Let `swc` generate a SourceMap
if (sourceMap) {

@@ -619,3 +535,2 @@ // @ts-ignore

}
if (swcOptions.compress) {

@@ -625,5 +540,5 @@ // More optimizations

swcOptions.compress.ecma = swcOptions.ecma;
} // https://github.com/webpack/webpack/issues/16135
}
// https://github.com/webpack/webpack/issues/16135
if (swcOptions.ecma === 5 && typeof swcOptions.compress.arrows === "undefined") {

@@ -633,14 +548,12 @@ swcOptions.compress.arrows = false;

}
const [[filename, code]] = Object.entries(input);
const result = await swc.minify(code, swcOptions);
let map;
if (result.map) {
map = JSON.parse(result.map); // TODO workaround for swc because `filename` is not preset as in `swc` signature as for `terser`
map = JSON.parse(result.map);
// TODO workaround for swc because `filename` is not preset as in `swc` signature as for `terser`
map.sources = [filename];
delete map.sourcesContent;
}
return {

@@ -651,20 +564,18 @@ code: result.code,

}
/**
* @returns {string | undefined}
*/
swcMinify.getMinimizerVersion = () => {
let packageJson;
try {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
packageJson = require("@swc/core/package.json");
} catch (error) {// Ignore
} catch (error) {
// Ignore
}
return packageJson && packageJson.version;
};
/* istanbul ignore next */
/**

@@ -676,4 +587,2 @@ * @param {Input} input

*/
async function esbuildMinify(input, sourceMap, minimizerOptions) {

@@ -687,11 +596,11 @@ /**

delete esbuildOptions.ecma;
if (esbuildOptions.module) {
// eslint-disable-next-line no-param-reassign
esbuildOptions.format = "esm";
} // eslint-disable-next-line no-param-reassign
}
// eslint-disable-next-line no-param-reassign
delete esbuildOptions.module;
delete esbuildOptions.module; // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return {

@@ -703,10 +612,11 @@ minify: true,

};
}; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
};
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
const esbuild = require("esbuild");
const esbuild = require("esbuild"); // Copy `esbuild` options
// Copy `esbuild` options
const esbuildOptions = buildEsbuildOptions(minimizerOptions);
const esbuildOptions = buildEsbuildOptions(minimizerOptions); // Let `esbuild` generate a SourceMap
// Let `esbuild` generate a SourceMap
if (sourceMap) {

@@ -716,3 +626,2 @@ esbuildOptions.sourcemap = true;

}
const [[filename, code]] = Object.entries(input);

@@ -726,30 +635,23 @@ esbuildOptions.sourcefile = filename;

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")}` : ""}`
};
const plugin = item.pluginName ? `\nPlugin Name: ${item.pluginName}` : "";
const location = item.location ? `\n\n${item.location.file}:${item.location.line}:${item.location.column}:\n ${item.location.line} | ${item.location.lineText}\n\nSuggestion: ${item.location.suggestion}` : "";
const notes = 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")}` : "";
return `${item.text} [${item.id}]${plugin}${location}${item.detail ? `\nDetails:\n${item.detail}` : ""}${notes}`;
}) : []
};
}
/**
* @returns {string | undefined}
*/
esbuildMinify.getMinimizerVersion = () => {
let packageJson;
try {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
packageJson = require("esbuild/package.json");
} catch (error) {// Ignore
} catch (error) {
// Ignore
}
return packageJson && packageJson.version;
};
module.exports = {

@@ -756,0 +658,0 @@ throttleAll,

{
"name": "terser-webpack-plugin",
"version": "5.3.6",
"version": "5.3.7",
"description": "Terser plugin for webpack",

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

"lint:js": "eslint --cache .",
"lint:spelling": "cspell \"**/*.*\"",
"lint:types": "tsc --pretty --noEmit",

@@ -59,17 +60,18 @@ "lint": "npm-run-all -l -p \"lint:**\"",

"dependencies": {
"@jridgewell/trace-mapping": "^0.3.14",
"@jridgewell/trace-mapping": "^0.3.17",
"jest-worker": "^27.4.5",
"schema-utils": "^3.1.1",
"serialize-javascript": "^6.0.0",
"terser": "^5.14.1"
"serialize-javascript": "^6.0.1",
"terser": "^5.16.5"
},
"devDependencies": {
"@babel/cli": "^7.18.6",
"@babel/core": "^7.18.6",
"@babel/preset-env": "^7.18.6",
"@commitlint/cli": "^17.0.3",
"@commitlint/config-conventional": "^17.0.3",
"@swc/core": "^1.2.220",
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@swc/core": "^1.3.37",
"@types/node": "^18.14.4",
"@types/serialize-javascript": "^5.0.2",
"@types/uglify-js": "^3.16.0",
"@types/uglify-js": "^3.17.1",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",

@@ -79,2 +81,3 @@ "babel-jest": "^28.1.2",

"cross-env": "^7.0.3",
"cspell": "^6.27.0",
"del": "^6.0.0",

@@ -84,15 +87,15 @@ "del-cli": "^3.0.1",

"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.4",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"file-loader": "^6.2.0",
"husky": "^7.0.2",
"jest": "^27.5.1",
"lint-staged": "^13.0.3",
"memfs": "^3.4.7",
"lint-staged": "^13.1.2",
"memfs": "^3.4.13",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"prettier": "^2.8.4",
"standard-version": "^9.3.1",
"typescript": "^4.7.4",
"uglify-js": "^3.16.1",
"webpack": "^5.73.0",
"typescript": "^4.9.5",
"uglify-js": "^3.17.4",
"webpack": "^5.75.0",
"webpack-cli": "^4.10.0",

@@ -99,0 +102,0 @@ "worker-loader": "^3.0.8"

@@ -23,3 +23,3 @@ export type Task<T> = () => Promise<T>;

/**
* Run tasks with limited concurency.
* Run tasks with limited concurrency.
* @template T

@@ -26,0 +26,0 @@ * @param {number} limit - Limit of tasks that run at once.

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc