Skip to content

Commit

Permalink
Refactor code to move filesize to workflow package
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Aug 3, 2023
1 parent 47db941 commit 28fff27
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 42 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/scripts/comments.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile } from 'node:fs/promises'

import { filesize } from 'filesize'
import { getFileSizes, getStats, modulePaths } from 'govuk-frontend-stats'

/**
Expand Down Expand Up @@ -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, String(filesize(value.size, { base: 2 }))]
})
const fileSizeHeaders = ['File', 'Size']
const fileSizeTable = renderTable(fileSizeHeaders, fileSizeRows)
const fileSizeText = [fileSizeTitle, fileSizeTable].join('\n')
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"license": "MIT",
"dependencies": {
"govuk-frontend-stats": "*"
"govuk-frontend-stats": "*",
"filesize": "^10.0.8"
}
}
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion shared/stats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 15 additions & 37 deletions shared/stats/src/index.mjs
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -74,37 +69,20 @@ 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]: import('fs').Stats}>} - 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]: import('fs').Stats } } */
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) {
const stats = await stat(join(paths.root, filename))
result[filename] = stats
}

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
*/

0 comments on commit 28fff27

Please sign in to comment.