From 1a78268dc3fd28a42d53bfc7f791d1a461df5681 Mon Sep 17 00:00:00 2001 From: Brett Kyle Date: Thu, 3 Aug 2023 12:39:19 +0100 Subject: [PATCH] Refactor code to move filesize to workflow package --- .github/workflows/scripts/comments.mjs | 6 ++- .github/workflows/scripts/package.json | 3 +- package-lock.json | 3 +- shared/stats/package.json | 1 - shared/stats/src/index.mjs | 51 +++++++------------------- 5 files changed, 22 insertions(+), 42 deletions(-) diff --git a/.github/workflows/scripts/comments.mjs b/.github/workflows/scripts/comments.mjs index a79edfcd5d..ed599bc722 100644 --- a/.github/workflows/scripts/comments.mjs +++ b/.github/workflows/scripts/comments.mjs @@ -1,5 +1,6 @@ import { readFile } from 'node:fs/promises' +import { filesize } from 'filesize' import { getFileSizes, getStats, modulePaths } from 'govuk-frontend-stats' /** @@ -94,7 +95,10 @@ export async function commentStats ( // File sizes const fileSizeTitle = '### File sizes' const fileSizes = await getFileSizes() - const fileSizeRows = Object.entries(fileSizes).map(([key, value]) => [key, String(value)]) + const fileSizeRows = Object.entries(fileSizes) + .map(([key, value]) => { + return [`[${key}](https://github.com/alphagov/govuk-frontend/blob/${githubActionContext.commit}/${key})`, filesize(value.size, { base: 2 })] + }) const fileSizeHeaders = ['File', 'Size'] const fileSizeTable = renderTable(fileSizeHeaders, fileSizeRows) const fileSizeText = [fileSizeTitle, fileSizeTable].join('\n') diff --git a/.github/workflows/scripts/package.json b/.github/workflows/scripts/package.json index 9a8be0ed4c..a0127c4a5f 100644 --- a/.github/workflows/scripts/package.json +++ b/.github/workflows/scripts/package.json @@ -8,6 +8,7 @@ }, "license": "MIT", "dependencies": { - "govuk-frontend-stats": "*" + "govuk-frontend-stats": "*", + "filesize": "^10.0.8" } } diff --git a/package-lock.json b/package-lock.json index c775c57063..dd7da87754 100644 --- a/package-lock.json +++ b/package-lock.json @@ -75,6 +75,7 @@ "name": "govuk-frontend-workflow-scripts", "license": "MIT", "dependencies": { + "filesize": "^10.0.8", "govuk-frontend-stats": "*" }, "engines": { @@ -11687,7 +11688,6 @@ "version": "10.0.8", "resolved": "https://registry.npmjs.org/filesize/-/filesize-10.0.8.tgz", "integrity": "sha512-/ylBrxZ1GAjgh2CEemKKLwTtmXfWqTtN1jRl6iqLwnMEucUX5cmaCCUPGstQOHVCcK9uYL6o1cPNakLQU2sasQ==", - "dev": true, "engines": { "node": ">= 10.4.0" } @@ -27809,7 +27809,6 @@ }, "devDependencies": { "@rollup/plugin-node-resolve": "^15.1.0", - "filesize": "^10.0.8", "govuk-frontend": "*", "rollup": "^3.26.3", "rollup-plugin-visualizer": "^5.9.2" diff --git a/shared/stats/package.json b/shared/stats/package.json index 329cb15ba7..5581438334 100644 --- a/shared/stats/package.json +++ b/shared/stats/package.json @@ -17,7 +17,6 @@ }, "devDependencies": { "@rollup/plugin-node-resolve": "^15.1.0", - "filesize": "^10.0.8", "govuk-frontend": "*", "rollup": "^3.26.3", "rollup-plugin-visualizer": "^5.9.2" diff --git a/shared/stats/src/index.mjs b/shared/stats/src/index.mjs index 57c29b81e8..629e578fb2 100644 --- a/shared/stats/src/index.mjs +++ b/shared/stats/src/index.mjs @@ -1,7 +1,6 @@ import { stat } from 'fs/promises' import { join, parse } from 'path' -import { filesize } from 'filesize' import { paths, pkg } from 'govuk-frontend-config' import { getComponentNames, filterPath, getYaml } from 'govuk-frontend-lib/files' @@ -14,17 +13,13 @@ const componentNamesWithJavaScript = await getComponentNames((componentName, com /** * Target files for file size analysis */ -const filesForAnalysis = { - dist: { - js: join(paths.root, `dist/govuk-frontend-${pkg.version}.min.js`), - css: join(paths.root, `dist/govuk-frontend-${pkg.version}.min.css`) - }, - package: { - esModule: join(paths.package, 'dist/govuk/all.mjs'), - esModuleBundle: join(paths.package, 'dist/govuk/all.bundle.mjs'), - umdBundle: join(paths.package, 'dist/govuk/all.bundle.js') - } -} +const filesForAnalysis = [ + `dist/govuk-frontend-${pkg.version}.min.js`, + `dist/govuk-frontend-${pkg.version}.min.css`, + 'packages/govuk-frontend/dist/govuk/all.mjs', + 'packages/govuk-frontend/dist/govuk/all.bundle.mjs', + 'packages/govuk-frontend/dist/govuk/all.bundle.js' +] /** * Package options @@ -74,37 +69,19 @@ export async function getStats (modulePath) { /** * Returns file sizes of key files * - * @returns {Promise<{[key: string]: string | number | any[] | FileSizeObject}>} - File names and size + * @returns {Promise<{[key: string]: Promise}>} - File names and size */ export async function getFileSizes () { - const [distJs, distCSS, esModule, esModuleBundle, umdBundle] = await Promise.all([ - stat(filesForAnalysis.dist.js), - stat(filesForAnalysis.dist.css), - stat(filesForAnalysis.package.esModule), - stat(filesForAnalysis.package.esModuleBundle), - stat(filesForAnalysis.package.umdBundle) - ]) + /** @type { { [key: string]: Promise } } */ + const result = {} - return { - 'Minified release JS': filesize(distJs.size, { base: 2 }), - 'Minified release CSS': filesize(distCSS.size, { base: 2 }), - 'Package ES Module': filesize(esModule.size, { base: 2 }), - 'Package ES Module Bundle': filesize(esModuleBundle.size, { base: 2 }), - 'Package UMD Bundle': filesize(umdBundle.size, { base: 2 }) + for (const filename of filesForAnalysis) { + result[filename] = stat(join(paths.root, filename)) } + + return result } /** * @typedef {{ [modulePath: string]: { rendered: number } }} ModulesList */ - -/** - * Filesize return object - * https://github.com/avoidwork/filesize.js/blob/c9fff4f777d65ac85dd14e22fa2f0a62dac166e2/types/filesize.d.ts#L20 - * - * @typedef {object} FileSizeObject - * @property {any} value - filesize value - * @property {any} symbol - filesize symbol - * @property {number} exponent - filesize exponent - * @property {string} unit - filesize unit - */