| | |
| | | */ |
| | | |
| | | /** |
| | | * @typedef {Array<string>} ExtractedComments |
| | | * @typedef {string[]} ExtractedComments |
| | | */ |
| | | |
| | | const notSettled = Symbol("not-settled"); |
| | |
| | | }; |
| | | |
| | | /** |
| | | * @param {import("terser").MinifyOptions & { sourceMap: import("terser").SourceMapOptions | undefined } & ({ output: import("terser").FormatOptions & { beautify: boolean } } | { format: import("terser").FormatOptions & { beautify: boolean } })} terserOptions terser options |
| | | * @param {import("terser").MinifyOptions & { sourceMap: import("terser").SourceMapOptions | undefined } & ({ output: import("terser").FormatOptions & { beautify: boolean } } | { format: import("terser").FormatOptions & { beautify: boolean } })} terserOptions terser options |
| | | * @param {ExtractedComments} extractedComments extracted comments |
| | | * @returns {ExtractCommentsFunction} function to extract comments |
| | | */ |
| | |
| | | } |
| | | regexStr = /** @type {string} */condition[key]; |
| | | condition[key] = /** @type {ExtractCommentsFunction} */ |
| | | (astNode, comment) => new RegExp( /** @type {string} */regexStr).test(comment.value); |
| | | (astNode, comment) => new RegExp(/** @type {string} */regexStr).test(comment.value); |
| | | break; |
| | | default: |
| | | regex = /** @type {RegExp} */condition[key]; |
| | |
| | | // 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}`; |
| | | |
| | |
| | | } = require("terser")); |
| | | } catch (err) { |
| | | return { |
| | | errors: [( /** @type {Error} */err)] |
| | | errors: [(/** @type {Error} */err)] |
| | | }; |
| | | } |
| | | |
| | |
| | | [filename]: code |
| | | }, terserOptions); |
| | | return { |
| | | code: ( /** @type {string} * */result.code), |
| | | map: result.map ? ( /** @type {RawSourceMap} * */result.map) : undefined, |
| | | code: (/** @type {string} * */result.code), |
| | | map: result.map ? (/** @type {RawSourceMap} * */result.map) : undefined, |
| | | extractedComments |
| | | }; |
| | | } |
| | |
| | | }; |
| | | |
| | | /** |
| | | * @param {import("uglify-js").MinifyOptions & { sourceMap: boolean | import("uglify-js").SourceMapOptions | undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}} uglifyJsOptions uglify-js options |
| | | * @param {import("uglify-js").MinifyOptions & { sourceMap: boolean | import("uglify-js").SourceMapOptions | undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean } }} uglifyJsOptions uglify-js options |
| | | * @param {ExtractedComments} extractedComments extracted comments |
| | | * @returns {ExtractCommentsFunction} extract comments function |
| | | */ |
| | |
| | | } |
| | | regexStr = /** @type {string} */condition[key]; |
| | | condition[key] = /** @type {ExtractCommentsFunction} */ |
| | | (astNode, comment) => new RegExp( /** @type {string} */regexStr).test(comment.value); |
| | | (astNode, comment) => new RegExp(/** @type {string} */regexStr).test(comment.value); |
| | | break; |
| | | default: |
| | | regex = /** @type {RegExp} */condition[key]; |
| | |
| | | // 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}`; |
| | | |
| | |
| | | |
| | | /** |
| | | * @param {PredefinedOptions<import("uglify-js").MinifyOptions> & import("uglify-js").MinifyOptions=} uglifyJsOptions uglify-js options |
| | | * @returns {import("uglify-js").MinifyOptions & { sourceMap: boolean | import("uglify-js").SourceMapOptions | undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean }}} uglify-js options |
| | | * @returns {import("uglify-js").MinifyOptions & { sourceMap: boolean | import("uglify-js").SourceMapOptions | undefined } & { output: import("uglify-js").OutputOptions & { beautify: boolean } }} uglify-js options |
| | | */ |
| | | const buildUglifyJsOptions = (uglifyJsOptions = {}) => { |
| | | if (typeof uglifyJsOptions.ecma !== "undefined") { |
| | |
| | | } = require("uglify-js")); |
| | | } catch (err) { |
| | | return { |
| | | errors: [( /** @type {Error} */err)] |
| | | errors: [(/** @type {Error} */err)] |
| | | }; |
| | | } |
| | | |
| | |
| | | swc = require("@swc/core"); |
| | | } catch (err) { |
| | | return { |
| | | errors: [( /** @type {Error} */err)] |
| | | errors: [(/** @type {Error} */err)] |
| | | }; |
| | | } |
| | | |
| | |
| | | esbuild = require("esbuild"); |
| | | } catch (err) { |
| | | return { |
| | | errors: [( /** @type {Error} */err)] |
| | | errors: [(/** @type {Error} */err)] |
| | | }; |
| | | } |
| | | |
| | |
| | | */ |
| | | esbuildMinify.supportsWorkerThreads = () => false; |
| | | |
| | | /* istanbul ignore next */ |
| | | /** |
| | | * @param {Input} input input |
| | | * @param {RawSourceMap=} sourceMap source map |
| | | * @param {CustomOptions=} minimizerOptions options |
| | | * @returns {Promise<MinimizedResult>} minimized result |
| | | */ |
| | | async function jsonMinify(input, sourceMap, minimizerOptions) { |
| | | const options = /** @type {{ replacer?: Parameters<typeof JSON.stringify>[1], space?: Parameters<typeof JSON.stringify>[2] }} */ |
| | | minimizerOptions; |
| | | const [[, code]] = Object.entries(input); |
| | | const result = JSON.stringify(JSON.parse(code), options.replacer, options.space); |
| | | return { |
| | | code: result |
| | | }; |
| | | } |
| | | jsonMinify.getMinimizerVersion = () => "1.0.0"; |
| | | jsonMinify.supportsWorker = () => false; |
| | | jsonMinify.supportsWorkerThreads = () => false; |
| | | |
| | | /** |
| | | * @template T |
| | | * @typedef {() => T} FunctionReturning |
| | |
| | | } |
| | | module.exports = { |
| | | esbuildMinify, |
| | | jsonMinify, |
| | | memoize, |
| | | swcMinify, |
| | | terserMinify, |