123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.esbuildMinify = esbuildMinify;
- exports.swcMinify = swcMinify;
- exports.terserMinify = terserMinify;
- exports.throttleAll = throttleAll;
- exports.uglifyJsMinify = uglifyJsMinify;
- /** @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
- */
- /**
- * @typedef {Array<string>} ExtractedComments
- */
- 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 */
- /**
- * @param {Input} input
- * @param {RawSourceMap | undefined} sourceMap
- * @param {PredefinedOptions & CustomOptions} minimizerOptions
- * @param {ExtractCommentsOptions | undefined} extractComments
- * @return {Promise<MinimizedResult>}
- */
- async function terserMinify(input, sourceMap, minimizerOptions, extractComments) {
- /**
- * @param {any} value
- * @returns {boolean}
- */
- const isObject = value => {
- const type = typeof value;
- return value != null && (type === "object" || type === "function");
- };
- /**
- * @param {TerserOptions & { sourceMap: undefined } & ({ output: TerserFormatOptions & { beautify: boolean } } | { format: TerserFormatOptions & { beautify: boolean } })} terserOptions
- * @param {ExtractedComments} extractedComments
- * @returns {ExtractCommentsFunction}
- */
- const buildComments = (terserOptions, extractedComments) => {
- /** @type {{ [index: string]: ExtractCommentsCondition }} */
- const condition = {};
- let comments;
- if (terserOptions.format) {
- ({
- comments
- } = terserOptions.format);
- } else if (terserOptions.output) {
- ({
- comments
- } = terserOptions.output);
- }
- condition.preserve = typeof comments !== "undefined" ? comments : false;
- if (typeof extractComments === "boolean" && extractComments) {
- condition.extract = "some";
- } else if (typeof extractComments === "string" || extractComments instanceof RegExp) {
- condition.extract = extractComments;
- } else if (typeof extractComments === "function") {
- condition.extract = extractComments;
- } else if (extractComments && isObject(extractComments)) {
- condition.extract = typeof extractComments.condition === "boolean" && extractComments.condition ? "some" : typeof extractComments.condition !== "undefined" ? extractComments.condition : "some";
- } else {
- // No extract
- // Preserve using "commentsOpts" or "some"
- condition.preserve = typeof comments !== "undefined" ? comments : "some";
- condition.extract = false;
- } // Ensure that both conditions are functions
- ["preserve", "extract"].forEach(key => {
- /** @type {undefined | string} */
- let regexStr;
- /** @type {undefined | RegExp} */
- let regex;
- switch (typeof condition[key]) {
- case "boolean":
- condition[key] = condition[key] ? () => true : () => false;
- break;
- case "function":
- break;
- case "string":
- if (condition[key] === "all") {
- condition[key] = () => true;
- break;
- }
- if (condition[key] === "some") {
- 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);
- break;
- default:
- regex =
- /** @type {RegExp} */
- condition[key];
- condition[key] =
- /** @type {ExtractCommentsFunction} */
- (astNode, comment) =>
- /** @type {RegExp} */
- regex.test(comment.value);
- }
- }); // Redefine the comments function to extract and preserve
- // comments according to the two conditions
- return (astNode, comment) => {
- if (
- /** @type {{ extract: ExtractCommentsFunction }} */
- condition.extract(astNode, comment)) {
- const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
- if (!extractedComments.includes(commentText)) {
- extractedComments.push(commentText);
- }
- }
- return (
- /** @type {{ preserve: ExtractCommentsFunction }} */
- condition.preserve(astNode, comment)
- );
- };
- };
- /**
- * @param {PredefinedOptions & TerserOptions} [terserOptions={}]
- * @returns {TerserOptions & { sourceMap: undefined } & ({ output: TerserFormatOptions & { beautify: boolean } } | { format: TerserFormatOptions & { beautify: boolean } })}
- */
- 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 : { ...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
- },
- // 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
- };
- }; // eslint-disable-next-line global-require
- const {
- minify
- } = require("terser"); // Copy `terser` options
- const terserOptions = buildTerserOptions(minimizerOptions); // Let terser generate a SourceMap
- if (sourceMap) {
- // @ts-ignore
- terserOptions.sourceMap = {
- asObject: true
- };
- }
- /** @type {ExtractedComments} */
- const extractedComments = [];
- if (terserOptions.output) {
- terserOptions.output.comments = buildComments(terserOptions, extractedComments);
- } else if (terserOptions.format) {
- terserOptions.format.comments = buildComments(terserOptions, extractedComments);
- }
- const [[filename, code]] = Object.entries(input);
- const result = await minify({
- [filename]: code
- }, terserOptions);
- return {
- code:
- /** @type {string} **/
- result.code,
- // @ts-ignore
- // eslint-disable-next-line no-undefined
- map: result.map ?
- /** @type {RawSourceMap} **/
- 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
- }
- return packageJson && packageJson.version;
- };
- /* istanbul ignore next */
- /**
- * @param {Input} input
- * @param {RawSourceMap | undefined} sourceMap
- * @param {PredefinedOptions & CustomOptions} minimizerOptions
- * @param {ExtractCommentsOptions | undefined} extractComments
- * @return {Promise<MinimizedResult>}
- */
- async function uglifyJsMinify(input, sourceMap, minimizerOptions, extractComments) {
- /**
- * @param {any} value
- * @returns {boolean}
- */
- const isObject = value => {
- const type = typeof value;
- return value != null && (type === "object" || type === "function");
- };
- /**
- * @param {import("uglify-js").MinifyOptions & { sourceMap: undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}} uglifyJsOptions
- * @param {ExtractedComments} extractedComments
- * @returns {ExtractCommentsFunction}
- */
- const buildComments = (uglifyJsOptions, extractedComments) => {
- /** @type {{ [index: string]: ExtractCommentsCondition }} */
- const condition = {};
- const {
- comments
- } = uglifyJsOptions.output;
- condition.preserve = typeof comments !== "undefined" ? comments : false;
- if (typeof extractComments === "boolean" && extractComments) {
- condition.extract = "some";
- } else if (typeof extractComments === "string" || extractComments instanceof RegExp) {
- condition.extract = extractComments;
- } else if (typeof extractComments === "function") {
- condition.extract = extractComments;
- } else if (extractComments && isObject(extractComments)) {
- condition.extract = typeof extractComments.condition === "boolean" && extractComments.condition ? "some" : typeof extractComments.condition !== "undefined" ? extractComments.condition : "some";
- } else {
- // No extract
- // Preserve using "commentsOpts" or "some"
- condition.preserve = typeof comments !== "undefined" ? comments : "some";
- condition.extract = false;
- } // Ensure that both conditions are functions
- ["preserve", "extract"].forEach(key => {
- /** @type {undefined | string} */
- let regexStr;
- /** @type {undefined | RegExp} */
- let regex;
- switch (typeof condition[key]) {
- case "boolean":
- condition[key] = condition[key] ? () => true : () => false;
- break;
- case "function":
- break;
- case "string":
- if (condition[key] === "all") {
- condition[key] = () => true;
- break;
- }
- if (condition[key] === "some") {
- 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);
- break;
- default:
- regex =
- /** @type {RegExp} */
- condition[key];
- condition[key] =
- /** @type {ExtractCommentsFunction} */
- (astNode, comment) =>
- /** @type {RegExp} */
- regex.test(comment.value);
- }
- }); // Redefine the comments function to extract and preserve
- // comments according to the two conditions
- return (astNode, comment) => {
- if (
- /** @type {{ extract: ExtractCommentsFunction }} */
- condition.extract(astNode, comment)) {
- const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
- if (!extractedComments.includes(commentText)) {
- extractedComments.push(commentText);
- }
- }
- return (
- /** @type {{ preserve: ExtractCommentsFunction }} */
- condition.preserve(astNode, comment)
- );
- };
- };
- /**
- * @param {PredefinedOptions & import("uglify-js").MinifyOptions} [uglifyJsOptions={}]
- * @returns {import("uglify-js").MinifyOptions & { sourceMap: undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}}
- */
- const buildUglifyJsOptions = (uglifyJsOptions = {}) => {
- // eslint-disable-next-line no-param-reassign
- delete minimizerOptions.ecma; // eslint-disable-next-line no-param-reassign
- delete minimizerOptions.module; // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
- return { ...uglifyJsOptions,
- // warnings: uglifyJsOptions.warnings,
- parse: { ...uglifyJsOptions.parse
- },
- compress: typeof uglifyJsOptions.compress === "boolean" ? uglifyJsOptions.compress : { ...uglifyJsOptions.compress
- },
- mangle: uglifyJsOptions.mangle == null ? true : typeof uglifyJsOptions.mangle === "boolean" ? uglifyJsOptions.mangle : { ...uglifyJsOptions.mangle
- },
- output: {
- beautify: false,
- ...uglifyJsOptions.output
- },
- // Ignoring sourceMap from options
- // eslint-disable-next-line no-undefined
- 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
- const {
- minify
- } = require("uglify-js"); // Copy `uglify-js` options
- const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions); // Let terser generate a SourceMap
- if (sourceMap) {
- // @ts-ignore
- uglifyJsOptions.sourceMap = true;
- }
- /** @type {ExtractedComments} */
- const extractedComments = []; // @ts-ignore
- uglifyJsOptions.output.comments = buildComments(uglifyJsOptions, extractedComments);
- const [[filename, code]] = Object.entries(input);
- const result = await minify({
- [filename]: code
- }, uglifyJsOptions);
- return {
- code: result.code,
- // eslint-disable-next-line no-undefined
- map: result.map ? JSON.parse(result.map) : undefined,
- errors: result.error ? [result.error] : [],
- warnings: result.warnings || [],
- extractedComments
- };
- }
- /**
- * @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
- }
- return packageJson && packageJson.version;
- };
- /* istanbul ignore next */
- /**
- * @param {Input} input
- * @param {RawSourceMap | undefined} sourceMap
- * @param {PredefinedOptions & CustomOptions} minimizerOptions
- * @return {Promise<MinimizedResult>}
- */
- async function swcMinify(input, sourceMap, minimizerOptions) {
- /**
- * @param {PredefinedOptions & import("@swc/core").JsMinifyOptions} [swcOptions={}]
- * @returns {import("@swc/core").JsMinifyOptions & { sourceMap: undefined }}
- */
- const buildSwcOptions = (swcOptions = {}) => {
- // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
- return { ...swcOptions,
- compress: typeof swcOptions.compress === "boolean" ? swcOptions.compress : { ...swcOptions.compress
- },
- mangle: swcOptions.mangle == null ? true : typeof swcOptions.mangle === "boolean" ? swcOptions.mangle : { ...swcOptions.mangle
- },
- // ecma: swcOptions.ecma,
- // keep_classnames: swcOptions.keep_classnames,
- // keep_fnames: swcOptions.keep_fnames,
- // module: swcOptions.module,
- // safari10: swcOptions.safari10,
- // toplevel: swcOptions.toplevel
- // eslint-disable-next-line no-undefined
- sourceMap: undefined
- };
- }; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
- const swc = require("@swc/core"); // Copy `swc` options
- const swcOptions = buildSwcOptions(minimizerOptions); // Let `swc` generate a SourceMap
- if (sourceMap) {
- // @ts-ignore
- swcOptions.sourceMap = true;
- }
- 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.sources = [filename];
- delete map.sourcesContent;
- }
- return {
- code: result.code,
- map
- };
- }
- /**
- * @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
- }
- return packageJson && packageJson.version;
- };
- /* istanbul ignore next */
- /**
- * @param {Input} input
- * @param {RawSourceMap | undefined} sourceMap
- * @param {PredefinedOptions & CustomOptions} minimizerOptions
- * @return {Promise<MinimizedResult>}
- */
- async function esbuildMinify(input, sourceMap, minimizerOptions) {
- /**
- * @param {PredefinedOptions & import("esbuild").TransformOptions} [esbuildOptions={}]
- * @returns {import("esbuild").TransformOptions}
- */
- const buildEsbuildOptions = (esbuildOptions = {}) => {
- // eslint-disable-next-line no-param-reassign
- delete esbuildOptions.ecma;
- if (esbuildOptions.module) {
- // eslint-disable-next-line no-param-reassign
- esbuildOptions.format = "esm";
- } // eslint-disable-next-line no-param-reassign
- delete esbuildOptions.module; // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
- return {
- minify: true,
- legalComments: "inline",
- ...esbuildOptions,
- sourcemap: false
- };
- }; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
- const esbuild = require("esbuild"); // Copy `esbuild` options
- const esbuildOptions = buildEsbuildOptions(minimizerOptions); // Let `esbuild` generate a SourceMap
- if (sourceMap) {
- esbuildOptions.sourcemap = true;
- esbuildOptions.sourcesContent = false;
- }
- const [[filename, code]] = Object.entries(input);
- esbuildOptions.sourcefile = filename;
- const result = await esbuild.transform(code, esbuildOptions);
- return {
- code: result.code,
- // eslint-disable-next-line no-undefined
- map: result.map ? JSON.parse(result.map) : undefined,
- 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")}` : ""}`
- };
- }) : []
- };
- }
- /**
- * @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
- }
- return packageJson && packageJson.version;
- };
|