Skip to content

Commit

Permalink
Allow custom file patterns for file size analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Aug 16, 2023
1 parent 1b1b188 commit 3542a06
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 41 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/scripts/comments.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile } from 'node:fs/promises'

import { getFileSizes, getStats, modulePaths } from '@govuk-frontend/stats'
import { getFileSizes, getStats, modulePaths } from '@govuk-frontend/lib'

Check failure on line 3 in .github/workflows/scripts/comments.mjs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Module '"@govuk-frontend/lib"' has no exported member 'getFileSizes'.

Check failure on line 3 in .github/workflows/scripts/comments.mjs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Module '"@govuk-frontend/lib"' has no exported member 'getStats'.

Check failure on line 3 in .github/workflows/scripts/comments.mjs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Module '"@govuk-frontend/lib"' has no exported member 'modulePaths'.
import { filesize } from 'filesize'

/**
Expand Down Expand Up @@ -79,7 +79,11 @@ export async function commentStats(

// File sizes
const fileSizeTitle = '### File sizes'
const fileSizes = await getFileSizes()
const distFileSizes = await getFileSizes('dist/**/*(!(*.map))')
const packageFileSizes = await getFileSizes(
'packages/govuk-frontend/dist/govuk/*(!(*.map))'
)
const fileSizes = distFileSizes.concat(packageFileSizes)
const fileSizeRows = Object.entries(fileSizes).map(([key, value]) => {
return [key, String(filesize(value.size, { base: 2 }))]
})
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"license": "MIT",
"dependencies": {
"@govuk-frontend/stats": "*",
"@govuk-frontend/lib": "*",
"filesize": "^10.0.8"
}
}
9 changes: 2 additions & 7 deletions package-lock.json

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

21 changes: 20 additions & 1 deletion shared/lib/files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { readFile } = require('fs/promises')
const { readFile, stat } = require('fs/promises')
const { parse, relative, basename } = require('path')

const { paths } = require('@govuk-frontend/config')
Expand Down Expand Up @@ -45,6 +45,24 @@ async function getDirectories(directoryPath) {
return listing.map((directoryPath) => basename(directoryPath)).sort()
}

/**
* Get file sizes
*
* @returns {Promise<{[key: string]: import('fs').Stats}>} - File names and size
*/
async function getFileSizes(directoryPath, ignore) {

Check failure on line 53 in shared/lib/files.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Parameter 'directoryPath' implicitly has an 'any' type.

Check failure on line 53 in shared/lib/files.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Parameter 'ignore' implicitly has an 'any' type.
const filesForAnalysis = await getListing(directoryPath, { ignore })

const result = {}

for (const filename of filesForAnalysis) {
const stats = await stat(filename)
result[filename] = stats

Check failure on line 60 in shared/lib/files.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
}

return result

Check failure on line 63 in shared/lib/files.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Type '{}' is not assignable to type '{ [key: string]: Stats; }'.
}

/**
* Directory listing array filter
* Returns true for files matching every pattern
Expand Down Expand Up @@ -84,6 +102,7 @@ async function getYaml(configPath) {
module.exports = {
filterPath,
getDirectories,
getFileSizes,
getListing,
getYaml,
mapPathTo
Expand Down
31 changes: 1 addition & 30 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 { paths, pkg } from '@govuk-frontend/config'
import { paths } from '@govuk-frontend/config'
import { getComponentNamesFiltered } from '@govuk-frontend/lib/components'
import { filterPath, getYaml } from '@govuk-frontend/lib/files'

Expand All @@ -14,17 +13,6 @@ const componentNamesWithJavaScript = await getComponentNamesFiltered(
{ moduleRoot: paths.stats }
)

/**
* Target files for file size analysis
*/
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 @@ -73,23 +61,6 @@ export async function getStats(modulePath) {
return { total, modules, moduleCount }
}

/**
* Returns file sizes of key files
*
* @returns {Promise<{[key: string]: import('fs').Stats}>} - File names and size
*/
export async function getFileSizes() {
/** @type { { [key: string]: import('fs').Stats } } */
const result = {}

for (const filename of filesForAnalysis) {
const stats = await stat(join(paths.root, filename))
result[filename] = stats
}

return result
}

/**
* @typedef {{ [modulePath: string]: { rendered: number } }} ModulesList
*/

0 comments on commit 3542a06

Please sign in to comment.