diff --git a/lib/svgo.d.ts b/lib/svgo.d.ts index 9f17e03..d46f7e3 100644 --- a/lib/svgo.d.ts +++ b/lib/svgo.d.ts @@ -85,6 +85,7 @@ export type Config = { type Output = { data: string; + passes: number; ast?: XastRoot; error?: unknown; }; diff --git a/lib/svgo.js b/lib/svgo.js index 7c43eb2..c078f58 100644 --- a/lib/svgo.js +++ b/lib/svgo.js @@ -193,7 +193,7 @@ function optimizeResolved(input, config, resolvedPlugins) { } else { console.warn(error); } - return { data: input, error: error }; + return { data: input, error: error, passes: 0 }; } if (config.datauri) { @@ -201,6 +201,7 @@ function optimizeResolved(input, config, resolvedPlugins) { } return { data: output, + passes: info.passNumber + 1, ast: ast, }; } diff --git a/lib/svgo/coa.js b/lib/svgo/coa.js index 97c8ffa..125f269 100644 --- a/lib/svgo/coa.js +++ b/lib/svgo/coa.js @@ -484,7 +484,7 @@ function processSVGData(config, info, data, output, input) { throw error; } } - var resultFileSize = Buffer.byteLength(result.data, 'utf8'), + const resultFileSize = Buffer.byteLength(result.data, 'utf8'), processingTime = Date.now() - startTime; return writeOutput(input, output, result.data).then( @@ -493,7 +493,9 @@ function processSVGData(config, info, data, output, input) { if (input) { console.log(`\n${path.basename(input)}:`); } - printTimeInfo(processingTime); + console.log( + `Done in ${processingTime} ms (${result.passes === 1 ? '1 pass' : `${result.passes} passes`})`, + ); printProfitInfo(prevFileSize, resultFileSize); } }, @@ -529,15 +531,6 @@ async function writeOutput(input, output, data) { .catch((error) => checkWriteFileError(input, output, data, error)); } -/** - * Write time taken to optimize. - * - * @param {number} time time in milliseconds. - */ -function printTimeInfo(time) { - console.log(`Done in ${time} ms!`); -} - /** * Write optimizing stats in a human-readable format. * diff --git a/test/regression.js b/test/regression.js index c656ce9..273f73f 100644 --- a/test/regression.js +++ b/test/regression.js @@ -18,7 +18,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); const width = 960; const height = 720; -/** @type {Map} */ +/** @type {Map} */ const stats = new Map(); /** @type {import('playwright').PageScreenshotOptions} */ @@ -35,6 +35,7 @@ async function performTests(options) { let mismatched = 0; let passed = 0; const notOptimized = new Set(); + let totalPasses = 0; let totalInputSize = 0; let totalCompression = 0; let totalPixelMismatches = 0; @@ -135,10 +136,15 @@ async function performTests(options) { `Total Compression: ${totalCompression} bytes (${toFixed((totalCompression / totalInputSize) * 100, 2)}%)`, ); console.info(`Total Pixel Mismatches: ${totalPixelMismatches}`); + console.info( + `Total Passes: ${totalPasses} (${toFixed(totalPasses / (mismatched + passed), 2)} average)`, + ); // Write statistics. const statArray = [ - ['Name', 'Orig Len', 'Opt Len', 'Reduction', 'Pixels'].join('\t'), + ['Name', 'Orig Len', 'Opt Len', 'Passes', 'Reduction', 'Pixels'].join( + '\t', + ), ]; const sortedKeys = []; for (const key of stats.keys()) { @@ -152,7 +158,11 @@ async function performTests(options) { const orig = fileStats.lengthOrig; const opt = fileStats.lengthOpt; const reduction = orig - opt; - statArray.push([name, orig, opt, reduction, fileStats.pixels].join('\t')); + statArray.push( + [name, orig, opt, fileStats.passes, reduction, fileStats.pixels].join( + '\t', + ), + ); } if (options.log) { @@ -209,6 +219,8 @@ async function performTests(options) { if (optimized.error) { notOptimized.add(name.substring(1)); } + fileStats.passes = optimized.passes; + totalPasses += optimized.passes; totalInputSize += file.length; totalCompression += file.length - optimized.data.length; res.setHeader('Content-Type', 'image/svg+xml'); @@ -228,6 +240,7 @@ async function performTests(options) { stats.set(name.replace(/\\/g, '/'), { lengthOrig: 0, lengthOpt: 0, + passes: 0, pixels: -1, }), );