|
| 1 | +// @ts-check |
| 2 | +import path from 'node:path' |
| 3 | +import { markdownTable } from 'markdown-table' |
| 4 | +import prettyBytes from 'pretty-bytes' |
| 5 | +import { readFile, readdir } from 'node:fs/promises' |
| 6 | +import { existsSync } from 'node:fs' |
| 7 | + |
| 8 | +const currDir = path.resolve('temp/size') |
| 9 | +const prevDir = path.resolve('temp/size-prev') |
| 10 | +let output = '## Compare Size\n' |
| 11 | + |
| 12 | +run() |
| 13 | + |
| 14 | +async function run() { |
| 15 | + await processExports() |
| 16 | + await renderBaseline() |
| 17 | + await renderFiles() |
| 18 | + |
| 19 | + process.stdout.write(output) |
| 20 | +} |
| 21 | + |
| 22 | +async function renderFiles() { |
| 23 | + /** |
| 24 | + * |
| 25 | + * @param {string[]} files |
| 26 | + * @returns {string[]} |
| 27 | + */ |
| 28 | + const filterFiles = files => files.filter(file => !file.startsWith('_')) |
| 29 | + |
| 30 | + const curr = filterFiles(await readdir(currDir)) |
| 31 | + const prev = filterFiles(await readdir(prevDir)) |
| 32 | + const fileList = new Set([...curr, ...prev]) |
| 33 | + |
| 34 | + /** @type string[][] */ |
| 35 | + const rows = [] |
| 36 | + for (const file of fileList) { |
| 37 | + const currPath = path.resolve(currDir, file) |
| 38 | + const prevPath = path.resolve(prevDir, file) |
| 39 | + |
| 40 | + let curr |
| 41 | + if (existsSync(currPath)) curr = await importJSON(currPath) |
| 42 | + let prev |
| 43 | + if (existsSync(prevPath)) prev = await importJSON(prevPath) |
| 44 | + |
| 45 | + const fileName = curr?.file || prev?.file |
| 46 | + |
| 47 | + if (!curr) { |
| 48 | + rows.push([`~~${fileName}~~`]) |
| 49 | + } else |
| 50 | + rows.push([ |
| 51 | + fileName, |
| 52 | + `${prettyBytes(curr.size)}${getDiff(curr.size, prev?.size)}`, |
| 53 | + `${prettyBytes(curr.gzip)}${getDiff(curr.gzip, prev?.gzip)}`, |
| 54 | + `${prettyBytes(curr.brotli)}${getDiff(curr.brotli, prev?.brotli)}` |
| 55 | + ]) |
| 56 | + } |
| 57 | + |
| 58 | + output += '### Bundles\n\n' |
| 59 | + output += markdownTable([['File', 'Size', 'Gzip', 'Brotli'], ...rows]) |
| 60 | + output += '\n\n' |
| 61 | +} |
| 62 | + |
| 63 | +async function renderBaseline() { |
| 64 | + const curr = await importJSON(path.resolve(currDir, '_baseline.json')) |
| 65 | + const prev = await importJSON(path.resolve(prevDir, '_baseline.json')) |
| 66 | + output += `### Baseline\n\n` |
| 67 | + output += markdownTable([ |
| 68 | + ['Size', 'Gzip', 'Brotli'], |
| 69 | + [ |
| 70 | + `${prettyBytes(curr.size)}${getDiff(curr.size, prev?.size)}`, |
| 71 | + `${prettyBytes(curr.gzip)}${getDiff(curr.gzip, prev?.gzip)}`, |
| 72 | + `${prettyBytes(curr.brotli)}${getDiff(curr.brotli, prev?.brotli)}` |
| 73 | + ] |
| 74 | + ]) |
| 75 | + output += '\n\n' |
| 76 | +} |
| 77 | + |
| 78 | +async function processExports() { |
| 79 | + const curr = await importJSON(path.resolve(currDir, '_exports.json')) |
| 80 | + const prev = await importJSON(path.resolve(prevDir, '_exports.json')) |
| 81 | + output += '\n### Exports\n\n' |
| 82 | + output += renderExports(curr, prev) |
| 83 | + output += `\n\n<details>\n<summary>Show full exports</summary>\n\n${renderExports( |
| 84 | + curr |
| 85 | + )}\n\n</details>\n\n` |
| 86 | +} |
| 87 | + |
| 88 | +/** @typedef {Record<string, import('@sxzz/export-size').ExportsInfo>} ExportMap */ |
| 89 | + |
| 90 | +/** |
| 91 | + * |
| 92 | + * @param {ExportMap} exports |
| 93 | + * @param {ExportMap} [prev] |
| 94 | + * @returns |
| 95 | + */ |
| 96 | +function renderExports(exports, prev) { |
| 97 | + const data = Object.entries(exports) |
| 98 | + .map( |
| 99 | + /** @returns {any} */ |
| 100 | + ([key, exp]) => { |
| 101 | + const prevExport = prev?.[key] |
| 102 | + const diffBundled = getDiff(exp.bundled, prevExport?.bundled) |
| 103 | + const diffMinified = getDiff(exp.minified, prevExport?.minified) |
| 104 | + const diffMinzipped = getDiff(exp.minzipped, prevExport?.minzipped) |
| 105 | + if (prev && !diffBundled && !diffMinified && !diffMinzipped) { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + return [ |
| 110 | + exp.name, |
| 111 | + `${prettyBytes(exp.bundled)}${diffBundled}`, |
| 112 | + `${prettyBytes(exp.minified)}${diffMinified}`, |
| 113 | + `${prettyBytes(exp.minzipped)}${diffMinzipped}` |
| 114 | + ] |
| 115 | + } |
| 116 | + ) |
| 117 | + .filter(Boolean) |
| 118 | + if (data.length === 0) return 'No changes' |
| 119 | + return markdownTable([['Name', 'Size', 'Minified', 'Brotli'], ...data]) |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @param {string} path |
| 124 | + */ |
| 125 | +async function importJSON(path) { |
| 126 | + return (await import(path, { assert: { type: 'json' } })).default |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * |
| 131 | + * @param {number} curr |
| 132 | + * @param {number} [prev] |
| 133 | + */ |
| 134 | +function getDiff(curr, prev) { |
| 135 | + if (prev === undefined) return '' |
| 136 | + const diff = curr - prev |
| 137 | + if (diff === 0) return '' |
| 138 | + const sign = diff > 0 ? '+' : '' |
| 139 | + return ` (**${sign}${prettyBytes(diff)}**)` |
| 140 | +} |
0 commit comments