123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- "use strict";
- const notSettled = Symbol(`not-settled`);
- 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( result);
- return;
- }
- const [index, task] = value;
-
- const onFulfilled = x => {
- result[index] = x;
- next();
- };
- task().then(onFulfilled, reject);
- };
- Array(limit).fill(0).forEach(next);
- });
- }
- async function terserMinify(input, sourceMap, minimizerOptions, extractComments) {
-
- const isObject = value => {
- const type = typeof value;
- return value != null && (type === "object" || type === "function");
- };
-
- const buildComments = (terserOptions, extractedComments) => {
-
- 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 {
-
-
- condition.preserve = typeof comments !== "undefined" ? comments : "some";
- condition.extract = false;
- }
-
- ["preserve", "extract"].forEach(key => {
-
- let regexStr;
-
- 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] =
- (astNode, comment) => (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
- break;
- }
- regexStr = condition[key];
- condition[key] =
- (astNode, comment) => new RegExp( regexStr).test(comment.value);
- break;
- default:
- regex = condition[key];
- condition[key] =
- (astNode, comment) => regex.test(comment.value);
- }
- });
-
-
- return (astNode, comment) => {
- if (
- condition.extract(astNode, comment)) {
- const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`;
-
- if (!extractedComments.includes(commentText)) {
- extractedComments.push(commentText);
- }
- }
- return condition.preserve(astNode, comment);
- };
- };
-
- const buildTerserOptions = (terserOptions = {}) => {
-
- return {
- ...terserOptions,
- compress: typeof terserOptions.compress === "boolean" ? terserOptions.compress ? {} : false : {
- ...terserOptions.compress
- },
-
-
-
-
- mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === "boolean" ? terserOptions.mangle : {
- ...terserOptions.mangle
- },
-
-
-
- ...(terserOptions.format ? {
- format: {
- beautify: false,
- ...terserOptions.format
- }
- } : {
- output: {
- beautify: false,
- ...terserOptions.output
- }
- }),
- parse: {
- ...terserOptions.parse
- },
-
-
-
- sourceMap: undefined
-
- };
- };
-
- const {
- minify
- } = require("terser");
-
- const terserOptions = buildTerserOptions(minimizerOptions);
-
- if (sourceMap) {
-
- terserOptions.sourceMap = {
- asObject: true
- };
- }
-
- const extractedComments = [];
- if (terserOptions.output) {
- terserOptions.output.comments = buildComments(terserOptions, extractedComments);
- } else if (terserOptions.format) {
- terserOptions.format.comments = buildComments(terserOptions, extractedComments);
- }
- if (terserOptions.compress) {
-
- if (typeof terserOptions.compress.ecma === "undefined") {
- terserOptions.compress.ecma = terserOptions.ecma;
- }
-
- if (terserOptions.ecma === 5 && typeof terserOptions.compress.arrows === "undefined") {
- terserOptions.compress.arrows = false;
- }
- }
- const [[filename, code]] = Object.entries(input);
- const result = await minify({
- [filename]: code
- }, terserOptions);
- return {
- code: ( result.code),
-
-
- map: result.map ? ( result.map) : undefined,
- extractedComments
- };
- }
- terserMinify.getMinimizerVersion = () => {
- let packageJson;
- try {
-
- packageJson = require("terser/package.json");
- } catch (error) {
-
- }
- return packageJson && packageJson.version;
- };
- async function uglifyJsMinify(input, sourceMap, minimizerOptions, extractComments) {
-
- const isObject = value => {
- const type = typeof value;
- return value != null && (type === "object" || type === "function");
- };
-
- const buildComments = (uglifyJsOptions, extractedComments) => {
-
- 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 {
-
-
- condition.preserve = typeof comments !== "undefined" ? comments : "some";
- condition.extract = false;
- }
-
- ["preserve", "extract"].forEach(key => {
-
- let regexStr;
-
- 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] =
- (astNode, comment) => (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
- break;
- }
- regexStr = condition[key];
- condition[key] =
- (astNode, comment) => new RegExp( regexStr).test(comment.value);
- break;
- default:
- regex = condition[key];
- condition[key] =
- (astNode, comment) => regex.test(comment.value);
- }
- });
-
-
- return (astNode, comment) => {
- if (
- condition.extract(astNode, comment)) {
- const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`;
-
- if (!extractedComments.includes(commentText)) {
- extractedComments.push(commentText);
- }
- }
- return condition.preserve(astNode, comment);
- };
- };
-
- const buildUglifyJsOptions = (uglifyJsOptions = {}) => {
-
- delete minimizerOptions.ecma;
-
- delete minimizerOptions.module;
-
- return {
- ...uglifyJsOptions,
-
- 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
- },
-
-
- sourceMap: undefined
-
-
-
-
- };
- };
-
- const {
- minify
- } = require("uglify-js");
-
- const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions);
-
- if (sourceMap) {
-
- uglifyJsOptions.sourceMap = true;
- }
-
- const extractedComments = [];
-
- uglifyJsOptions.output.comments = buildComments(uglifyJsOptions, extractedComments);
- const [[filename, code]] = Object.entries(input);
- const result = await minify({
- [filename]: code
- }, uglifyJsOptions);
- return {
- code: result.code,
-
- map: result.map ? JSON.parse(result.map) : undefined,
- errors: result.error ? [result.error] : [],
- warnings: result.warnings || [],
- extractedComments
- };
- }
- uglifyJsMinify.getMinimizerVersion = () => {
- let packageJson;
- try {
-
- packageJson = require("uglify-js/package.json");
- } catch (error) {
-
- }
- return packageJson && packageJson.version;
- };
- async function swcMinify(input, sourceMap, minimizerOptions) {
-
- const buildSwcOptions = (swcOptions = {}) => {
-
- 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
- },
-
-
-
-
-
-
-
- sourceMap: undefined
- };
- };
-
- const swc = require("@swc/core");
-
- const swcOptions = buildSwcOptions(minimizerOptions);
-
- if (sourceMap) {
-
- swcOptions.sourceMap = true;
- }
- if (swcOptions.compress) {
-
- if (typeof swcOptions.compress.ecma === "undefined") {
- swcOptions.compress.ecma = swcOptions.ecma;
- }
-
- if (swcOptions.ecma === 5 && typeof swcOptions.compress.arrows === "undefined") {
- 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);
-
- map.sources = [filename];
- delete map.sourcesContent;
- }
- return {
- code: result.code,
- map
- };
- }
- swcMinify.getMinimizerVersion = () => {
- let packageJson;
- try {
-
- packageJson = require("@swc/core/package.json");
- } catch (error) {
-
- }
- return packageJson && packageJson.version;
- };
- async function esbuildMinify(input, sourceMap, minimizerOptions) {
-
- const buildEsbuildOptions = (esbuildOptions = {}) => {
-
- delete esbuildOptions.ecma;
- if (esbuildOptions.module) {
-
- esbuildOptions.format = "esm";
- }
-
- delete esbuildOptions.module;
-
- return {
- minify: true,
- legalComments: "inline",
- ...esbuildOptions,
- sourcemap: false
- };
- };
-
- const esbuild = require("esbuild");
-
- const esbuildOptions = buildEsbuildOptions(minimizerOptions);
-
- 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,
-
- map: result.map ? JSON.parse(result.map) : undefined,
- warnings: result.warnings.length > 0 ? result.warnings.map(item => {
- 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
- }
- return packageJson && packageJson.version;
- };
- /**
- * @template T
- * @param fn {(function(): any) | undefined}
- * @returns {function(): T}
- */
- function memoize(fn) {
- let cache = false;
- /** @type {T} */
- let result;
- return () => {
- if (cache) {
- return result;
- }
- result = /** @type {function(): any} */fn();
- cache = true;
- // Allow to clean up memory for fn
- // and all dependent resources
- // eslint-disable-next-line no-undefined, no-param-reassign
- fn = undefined;
- return result;
- };
- }
- module.exports = {
- throttleAll,
- memoize,
- terserMinify,
- uglifyJsMinify,
- swcMinify,
- esbuildMinify
- };
|