utils.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. "use strict";
  2. /** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
  3. /** @typedef {import("terser").FormatOptions} TerserFormatOptions */
  4. /** @typedef {import("terser").MinifyOptions} TerserOptions */
  5. /** @typedef {import("terser").CompressOptions} TerserCompressOptions */
  6. /** @typedef {import("terser").ECMA} TerserECMA */
  7. /** @typedef {import("./index.js").ExtractCommentsOptions} ExtractCommentsOptions */
  8. /** @typedef {import("./index.js").ExtractCommentsFunction} ExtractCommentsFunction */
  9. /** @typedef {import("./index.js").ExtractCommentsCondition} ExtractCommentsCondition */
  10. /** @typedef {import("./index.js").Input} Input */
  11. /** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
  12. /** @typedef {import("./index.js").PredefinedOptions} PredefinedOptions */
  13. /** @typedef {import("./index.js").CustomOptions} CustomOptions */
  14. /**
  15. * @typedef {Array<string>} ExtractedComments
  16. */
  17. const notSettled = Symbol(`not-settled`);
  18. /**
  19. * @template T
  20. * @typedef {() => Promise<T>} Task
  21. */
  22. /**
  23. * Run tasks with limited concurrency.
  24. * @template T
  25. * @param {number} limit - Limit of tasks that run at once.
  26. * @param {Task<T>[]} tasks - List of tasks to run.
  27. * @returns {Promise<T[]>} A promise that fulfills to an array of the results
  28. */
  29. function throttleAll(limit, tasks) {
  30. if (!Number.isInteger(limit) || limit < 1) {
  31. throw new TypeError(`Expected \`limit\` to be a finite number > 0, got \`${limit}\` (${typeof limit})`);
  32. }
  33. if (!Array.isArray(tasks) || !tasks.every(task => typeof task === `function`)) {
  34. throw new TypeError(`Expected \`tasks\` to be a list of functions returning a promise`);
  35. }
  36. return new Promise((resolve, reject) => {
  37. const result = Array(tasks.length).fill(notSettled);
  38. const entries = tasks.entries();
  39. const next = () => {
  40. const {
  41. done,
  42. value
  43. } = entries.next();
  44. if (done) {
  45. const isLast = !result.includes(notSettled);
  46. if (isLast) resolve( /** @type{T[]} **/result);
  47. return;
  48. }
  49. const [index, task] = value;
  50. /**
  51. * @param {T} x
  52. */
  53. const onFulfilled = x => {
  54. result[index] = x;
  55. next();
  56. };
  57. task().then(onFulfilled, reject);
  58. };
  59. Array(limit).fill(0).forEach(next);
  60. });
  61. }
  62. /* istanbul ignore next */
  63. /**
  64. * @param {Input} input
  65. * @param {SourceMapInput | undefined} sourceMap
  66. * @param {PredefinedOptions & CustomOptions} minimizerOptions
  67. * @param {ExtractCommentsOptions | undefined} extractComments
  68. * @return {Promise<MinimizedResult>}
  69. */
  70. async function terserMinify(input, sourceMap, minimizerOptions, extractComments) {
  71. /**
  72. * @param {any} value
  73. * @returns {boolean}
  74. */
  75. const isObject = value => {
  76. const type = typeof value;
  77. return value != null && (type === "object" || type === "function");
  78. };
  79. /**
  80. * @param {TerserOptions & { sourceMap: undefined } & ({ output: TerserFormatOptions & { beautify: boolean } } | { format: TerserFormatOptions & { beautify: boolean } })} terserOptions
  81. * @param {ExtractedComments} extractedComments
  82. * @returns {ExtractCommentsFunction}
  83. */
  84. const buildComments = (terserOptions, extractedComments) => {
  85. /** @type {{ [index: string]: ExtractCommentsCondition }} */
  86. const condition = {};
  87. let comments;
  88. if (terserOptions.format) {
  89. ({
  90. comments
  91. } = terserOptions.format);
  92. } else if (terserOptions.output) {
  93. ({
  94. comments
  95. } = terserOptions.output);
  96. }
  97. condition.preserve = typeof comments !== "undefined" ? comments : false;
  98. if (typeof extractComments === "boolean" && extractComments) {
  99. condition.extract = "some";
  100. } else if (typeof extractComments === "string" || extractComments instanceof RegExp) {
  101. condition.extract = extractComments;
  102. } else if (typeof extractComments === "function") {
  103. condition.extract = extractComments;
  104. } else if (extractComments && isObject(extractComments)) {
  105. condition.extract = typeof extractComments.condition === "boolean" && extractComments.condition ? "some" : typeof extractComments.condition !== "undefined" ? extractComments.condition : "some";
  106. } else {
  107. // No extract
  108. // Preserve using "commentsOpts" or "some"
  109. condition.preserve = typeof comments !== "undefined" ? comments : "some";
  110. condition.extract = false;
  111. }
  112. // Ensure that both conditions are functions
  113. ["preserve", "extract"].forEach(key => {
  114. /** @type {undefined | string} */
  115. let regexStr;
  116. /** @type {undefined | RegExp} */
  117. let regex;
  118. switch (typeof condition[key]) {
  119. case "boolean":
  120. condition[key] = condition[key] ? () => true : () => false;
  121. break;
  122. case "function":
  123. break;
  124. case "string":
  125. if (condition[key] === "all") {
  126. condition[key] = () => true;
  127. break;
  128. }
  129. if (condition[key] === "some") {
  130. condition[key] = /** @type {ExtractCommentsFunction} */
  131. (astNode, comment) => (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
  132. break;
  133. }
  134. regexStr = /** @type {string} */condition[key];
  135. condition[key] = /** @type {ExtractCommentsFunction} */
  136. (astNode, comment) => new RegExp( /** @type {string} */regexStr).test(comment.value);
  137. break;
  138. default:
  139. regex = /** @type {RegExp} */condition[key];
  140. condition[key] = /** @type {ExtractCommentsFunction} */
  141. (astNode, comment) => /** @type {RegExp} */regex.test(comment.value);
  142. }
  143. });
  144. // Redefine the comments function to extract and preserve
  145. // comments according to the two conditions
  146. return (astNode, comment) => {
  147. if ( /** @type {{ extract: ExtractCommentsFunction }} */
  148. condition.extract(astNode, comment)) {
  149. const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`;
  150. // Don't include duplicate comments
  151. if (!extractedComments.includes(commentText)) {
  152. extractedComments.push(commentText);
  153. }
  154. }
  155. return /** @type {{ preserve: ExtractCommentsFunction }} */condition.preserve(astNode, comment);
  156. };
  157. };
  158. /**
  159. * @param {PredefinedOptions & TerserOptions} [terserOptions={}]
  160. * @returns {TerserOptions & { sourceMap: undefined } & { compress: TerserCompressOptions } & ({ output: TerserFormatOptions & { beautify: boolean } } | { format: TerserFormatOptions & { beautify: boolean } })}
  161. */
  162. const buildTerserOptions = (terserOptions = {}) => {
  163. // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
  164. return {
  165. ...terserOptions,
  166. compress: typeof terserOptions.compress === "boolean" ? terserOptions.compress ? {} : false : {
  167. ...terserOptions.compress
  168. },
  169. // ecma: terserOptions.ecma,
  170. // ie8: terserOptions.ie8,
  171. // keep_classnames: terserOptions.keep_classnames,
  172. // keep_fnames: terserOptions.keep_fnames,
  173. mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === "boolean" ? terserOptions.mangle : {
  174. ...terserOptions.mangle
  175. },
  176. // module: terserOptions.module,
  177. // nameCache: { ...terserOptions.toplevel },
  178. // the `output` option is deprecated
  179. ...(terserOptions.format ? {
  180. format: {
  181. beautify: false,
  182. ...terserOptions.format
  183. }
  184. } : {
  185. output: {
  186. beautify: false,
  187. ...terserOptions.output
  188. }
  189. }),
  190. parse: {
  191. ...terserOptions.parse
  192. },
  193. // safari10: terserOptions.safari10,
  194. // Ignoring sourceMap from options
  195. // eslint-disable-next-line no-undefined
  196. sourceMap: undefined
  197. // toplevel: terserOptions.toplevel
  198. };
  199. };
  200. // eslint-disable-next-line global-require
  201. const {
  202. minify
  203. } = require("terser");
  204. // Copy `terser` options
  205. const terserOptions = buildTerserOptions(minimizerOptions);
  206. // Let terser generate a SourceMap
  207. if (sourceMap) {
  208. // @ts-ignore
  209. terserOptions.sourceMap = {
  210. asObject: true
  211. };
  212. }
  213. /** @type {ExtractedComments} */
  214. const extractedComments = [];
  215. if (terserOptions.output) {
  216. terserOptions.output.comments = buildComments(terserOptions, extractedComments);
  217. } else if (terserOptions.format) {
  218. terserOptions.format.comments = buildComments(terserOptions, extractedComments);
  219. }
  220. if (terserOptions.compress) {
  221. // More optimizations
  222. if (typeof terserOptions.compress.ecma === "undefined") {
  223. terserOptions.compress.ecma = terserOptions.ecma;
  224. }
  225. // https://github.com/webpack/webpack/issues/16135
  226. if (terserOptions.ecma === 5 && typeof terserOptions.compress.arrows === "undefined") {
  227. terserOptions.compress.arrows = false;
  228. }
  229. }
  230. const [[filename, code]] = Object.entries(input);
  231. const result = await minify({
  232. [filename]: code
  233. }, terserOptions);
  234. return {
  235. code: ( /** @type {string} **/result.code),
  236. // @ts-ignore
  237. // eslint-disable-next-line no-undefined
  238. map: result.map ? ( /** @type {SourceMapInput} **/result.map) : undefined,
  239. extractedComments
  240. };
  241. }
  242. /**
  243. * @returns {string | undefined}
  244. */
  245. terserMinify.getMinimizerVersion = () => {
  246. let packageJson;
  247. try {
  248. // eslint-disable-next-line global-require
  249. packageJson = require("terser/package.json");
  250. } catch (error) {
  251. // Ignore
  252. }
  253. return packageJson && packageJson.version;
  254. };
  255. /* istanbul ignore next */
  256. /**
  257. * @param {Input} input
  258. * @param {SourceMapInput | undefined} sourceMap
  259. * @param {PredefinedOptions & CustomOptions} minimizerOptions
  260. * @param {ExtractCommentsOptions | undefined} extractComments
  261. * @return {Promise<MinimizedResult>}
  262. */
  263. async function uglifyJsMinify(input, sourceMap, minimizerOptions, extractComments) {
  264. /**
  265. * @param {any} value
  266. * @returns {boolean}
  267. */
  268. const isObject = value => {
  269. const type = typeof value;
  270. return value != null && (type === "object" || type === "function");
  271. };
  272. /**
  273. * @param {import("uglify-js").MinifyOptions & { sourceMap: undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}} uglifyJsOptions
  274. * @param {ExtractedComments} extractedComments
  275. * @returns {ExtractCommentsFunction}
  276. */
  277. const buildComments = (uglifyJsOptions, extractedComments) => {
  278. /** @type {{ [index: string]: ExtractCommentsCondition }} */
  279. const condition = {};
  280. const {
  281. comments
  282. } = uglifyJsOptions.output;
  283. condition.preserve = typeof comments !== "undefined" ? comments : false;
  284. if (typeof extractComments === "boolean" && extractComments) {
  285. condition.extract = "some";
  286. } else if (typeof extractComments === "string" || extractComments instanceof RegExp) {
  287. condition.extract = extractComments;
  288. } else if (typeof extractComments === "function") {
  289. condition.extract = extractComments;
  290. } else if (extractComments && isObject(extractComments)) {
  291. condition.extract = typeof extractComments.condition === "boolean" && extractComments.condition ? "some" : typeof extractComments.condition !== "undefined" ? extractComments.condition : "some";
  292. } else {
  293. // No extract
  294. // Preserve using "commentsOpts" or "some"
  295. condition.preserve = typeof comments !== "undefined" ? comments : "some";
  296. condition.extract = false;
  297. }
  298. // Ensure that both conditions are functions
  299. ["preserve", "extract"].forEach(key => {
  300. /** @type {undefined | string} */
  301. let regexStr;
  302. /** @type {undefined | RegExp} */
  303. let regex;
  304. switch (typeof condition[key]) {
  305. case "boolean":
  306. condition[key] = condition[key] ? () => true : () => false;
  307. break;
  308. case "function":
  309. break;
  310. case "string":
  311. if (condition[key] === "all") {
  312. condition[key] = () => true;
  313. break;
  314. }
  315. if (condition[key] === "some") {
  316. condition[key] = /** @type {ExtractCommentsFunction} */
  317. (astNode, comment) => (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
  318. break;
  319. }
  320. regexStr = /** @type {string} */condition[key];
  321. condition[key] = /** @type {ExtractCommentsFunction} */
  322. (astNode, comment) => new RegExp( /** @type {string} */regexStr).test(comment.value);
  323. break;
  324. default:
  325. regex = /** @type {RegExp} */condition[key];
  326. condition[key] = /** @type {ExtractCommentsFunction} */
  327. (astNode, comment) => /** @type {RegExp} */regex.test(comment.value);
  328. }
  329. });
  330. // Redefine the comments function to extract and preserve
  331. // comments according to the two conditions
  332. return (astNode, comment) => {
  333. if ( /** @type {{ extract: ExtractCommentsFunction }} */
  334. condition.extract(astNode, comment)) {
  335. const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`;
  336. // Don't include duplicate comments
  337. if (!extractedComments.includes(commentText)) {
  338. extractedComments.push(commentText);
  339. }
  340. }
  341. return /** @type {{ preserve: ExtractCommentsFunction }} */condition.preserve(astNode, comment);
  342. };
  343. };
  344. /**
  345. * @param {PredefinedOptions & import("uglify-js").MinifyOptions} [uglifyJsOptions={}]
  346. * @returns {import("uglify-js").MinifyOptions & { sourceMap: undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}}
  347. */
  348. const buildUglifyJsOptions = (uglifyJsOptions = {}) => {
  349. // eslint-disable-next-line no-param-reassign
  350. delete minimizerOptions.ecma;
  351. // eslint-disable-next-line no-param-reassign
  352. delete minimizerOptions.module;
  353. // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
  354. return {
  355. ...uglifyJsOptions,
  356. // warnings: uglifyJsOptions.warnings,
  357. parse: {
  358. ...uglifyJsOptions.parse
  359. },
  360. compress: typeof uglifyJsOptions.compress === "boolean" ? uglifyJsOptions.compress : {
  361. ...uglifyJsOptions.compress
  362. },
  363. mangle: uglifyJsOptions.mangle == null ? true : typeof uglifyJsOptions.mangle === "boolean" ? uglifyJsOptions.mangle : {
  364. ...uglifyJsOptions.mangle
  365. },
  366. output: {
  367. beautify: false,
  368. ...uglifyJsOptions.output
  369. },
  370. // Ignoring sourceMap from options
  371. // eslint-disable-next-line no-undefined
  372. sourceMap: undefined
  373. // toplevel: uglifyJsOptions.toplevel
  374. // nameCache: { ...uglifyJsOptions.toplevel },
  375. // ie8: uglifyJsOptions.ie8,
  376. // keep_fnames: uglifyJsOptions.keep_fnames,
  377. };
  378. };
  379. // eslint-disable-next-line global-require, import/no-extraneous-dependencies
  380. const {
  381. minify
  382. } = require("uglify-js");
  383. // Copy `uglify-js` options
  384. const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions);
  385. // Let terser generate a SourceMap
  386. if (sourceMap) {
  387. // @ts-ignore
  388. uglifyJsOptions.sourceMap = true;
  389. }
  390. /** @type {ExtractedComments} */
  391. const extractedComments = [];
  392. // @ts-ignore
  393. uglifyJsOptions.output.comments = buildComments(uglifyJsOptions, extractedComments);
  394. const [[filename, code]] = Object.entries(input);
  395. const result = await minify({
  396. [filename]: code
  397. }, uglifyJsOptions);
  398. return {
  399. code: result.code,
  400. // eslint-disable-next-line no-undefined
  401. map: result.map ? JSON.parse(result.map) : undefined,
  402. errors: result.error ? [result.error] : [],
  403. warnings: result.warnings || [],
  404. extractedComments
  405. };
  406. }
  407. /**
  408. * @returns {string | undefined}
  409. */
  410. uglifyJsMinify.getMinimizerVersion = () => {
  411. let packageJson;
  412. try {
  413. // eslint-disable-next-line global-require, import/no-extraneous-dependencies
  414. packageJson = require("uglify-js/package.json");
  415. } catch (error) {
  416. // Ignore
  417. }
  418. return packageJson && packageJson.version;
  419. };
  420. /* istanbul ignore next */
  421. /**
  422. * @param {Input} input
  423. * @param {SourceMapInput | undefined} sourceMap
  424. * @param {PredefinedOptions & CustomOptions} minimizerOptions
  425. * @return {Promise<MinimizedResult>}
  426. */
  427. async function swcMinify(input, sourceMap, minimizerOptions) {
  428. /**
  429. * @param {PredefinedOptions & import("@swc/core").JsMinifyOptions} [swcOptions={}]
  430. * @returns {import("@swc/core").JsMinifyOptions & { sourceMap: undefined } & { compress: import("@swc/core").TerserCompressOptions }}
  431. */
  432. const buildSwcOptions = (swcOptions = {}) => {
  433. // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
  434. return {
  435. ...swcOptions,
  436. compress: typeof swcOptions.compress === "boolean" ? swcOptions.compress ? {} : false : {
  437. ...swcOptions.compress
  438. },
  439. mangle: swcOptions.mangle == null ? true : typeof swcOptions.mangle === "boolean" ? swcOptions.mangle : {
  440. ...swcOptions.mangle
  441. },
  442. // ecma: swcOptions.ecma,
  443. // keep_classnames: swcOptions.keep_classnames,
  444. // keep_fnames: swcOptions.keep_fnames,
  445. // module: swcOptions.module,
  446. // safari10: swcOptions.safari10,
  447. // toplevel: swcOptions.toplevel
  448. // eslint-disable-next-line no-undefined
  449. sourceMap: undefined
  450. };
  451. };
  452. // eslint-disable-next-line import/no-extraneous-dependencies, global-require
  453. const swc = require("@swc/core");
  454. // Copy `swc` options
  455. const swcOptions = buildSwcOptions(minimizerOptions);
  456. // Let `swc` generate a SourceMap
  457. if (sourceMap) {
  458. // @ts-ignore
  459. swcOptions.sourceMap = true;
  460. }
  461. if (swcOptions.compress) {
  462. // More optimizations
  463. if (typeof swcOptions.compress.ecma === "undefined") {
  464. swcOptions.compress.ecma = swcOptions.ecma;
  465. }
  466. // https://github.com/webpack/webpack/issues/16135
  467. if (swcOptions.ecma === 5 && typeof swcOptions.compress.arrows === "undefined") {
  468. swcOptions.compress.arrows = false;
  469. }
  470. }
  471. const [[filename, code]] = Object.entries(input);
  472. const result = await swc.minify(code, swcOptions);
  473. let map;
  474. if (result.map) {
  475. map = JSON.parse(result.map);
  476. // TODO workaround for swc because `filename` is not preset as in `swc` signature as for `terser`
  477. map.sources = [filename];
  478. delete map.sourcesContent;
  479. }
  480. return {
  481. code: result.code,
  482. map
  483. };
  484. }
  485. /**
  486. * @returns {string | undefined}
  487. */
  488. swcMinify.getMinimizerVersion = () => {
  489. let packageJson;
  490. try {
  491. // eslint-disable-next-line global-require, import/no-extraneous-dependencies
  492. packageJson = require("@swc/core/package.json");
  493. } catch (error) {
  494. // Ignore
  495. }
  496. return packageJson && packageJson.version;
  497. };
  498. /* istanbul ignore next */
  499. /**
  500. * @param {Input} input
  501. * @param {SourceMapInput | undefined} sourceMap
  502. * @param {PredefinedOptions & CustomOptions} minimizerOptions
  503. * @return {Promise<MinimizedResult>}
  504. */
  505. async function esbuildMinify(input, sourceMap, minimizerOptions) {
  506. /**
  507. * @param {PredefinedOptions & import("esbuild").TransformOptions} [esbuildOptions={}]
  508. * @returns {import("esbuild").TransformOptions}
  509. */
  510. const buildEsbuildOptions = (esbuildOptions = {}) => {
  511. // eslint-disable-next-line no-param-reassign
  512. delete esbuildOptions.ecma;
  513. if (esbuildOptions.module) {
  514. // eslint-disable-next-line no-param-reassign
  515. esbuildOptions.format = "esm";
  516. }
  517. // eslint-disable-next-line no-param-reassign
  518. delete esbuildOptions.module;
  519. // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
  520. return {
  521. minify: true,
  522. legalComments: "inline",
  523. ...esbuildOptions,
  524. sourcemap: false
  525. };
  526. };
  527. // eslint-disable-next-line import/no-extraneous-dependencies, global-require
  528. const esbuild = require("esbuild");
  529. // Copy `esbuild` options
  530. const esbuildOptions = buildEsbuildOptions(minimizerOptions);
  531. // Let `esbuild` generate a SourceMap
  532. if (sourceMap) {
  533. esbuildOptions.sourcemap = true;
  534. esbuildOptions.sourcesContent = false;
  535. }
  536. const [[filename, code]] = Object.entries(input);
  537. esbuildOptions.sourcefile = filename;
  538. const result = await esbuild.transform(code, esbuildOptions);
  539. return {
  540. code: result.code,
  541. // eslint-disable-next-line no-undefined
  542. map: result.map ? JSON.parse(result.map) : undefined,
  543. warnings: result.warnings.length > 0 ? result.warnings.map(item => {
  544. const plugin = item.pluginName ? `\nPlugin Name: ${item.pluginName}` : "";
  545. 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}` : "";
  546. 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")}` : "";
  547. return `${item.text} [${item.id}]${plugin}${location}${item.detail ? `\nDetails:\n${item.detail}` : ""}${notes}`;
  548. }) : []
  549. };
  550. }
  551. /**
  552. * @returns {string | undefined}
  553. */
  554. esbuildMinify.getMinimizerVersion = () => {
  555. let packageJson;
  556. try {
  557. // eslint-disable-next-line global-require, import/no-extraneous-dependencies
  558. packageJson = require("esbuild/package.json");
  559. } catch (error) {
  560. // Ignore
  561. }
  562. return packageJson && packageJson.version;
  563. };
  564. /**
  565. * @template T
  566. * @param fn {(function(): any) | undefined}
  567. * @returns {function(): T}
  568. */
  569. function memoize(fn) {
  570. let cache = false;
  571. /** @type {T} */
  572. let result;
  573. return () => {
  574. if (cache) {
  575. return result;
  576. }
  577. result = /** @type {function(): any} */fn();
  578. cache = true;
  579. // Allow to clean up memory for fn
  580. // and all dependent resources
  581. // eslint-disable-next-line no-undefined, no-param-reassign
  582. fn = undefined;
  583. return result;
  584. };
  585. }
  586. module.exports = {
  587. throttleAll,
  588. memoize,
  589. terserMinify,
  590. uglifyJsMinify,
  591. swcMinify,
  592. esbuildMinify
  593. };