-
Notifications
You must be signed in to change notification settings - Fork 329
/
index.mjs
79 lines (67 loc) · 2.16 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { join, parse } from 'path'
import { paths } from '@govuk-frontend/config'
import { getComponentNamesFiltered } from '@govuk-frontend/lib/components'
import { filterPath, getYaml } from '@govuk-frontend/lib/files'
import { filesize } from 'filesize'
/**
* Components with JavaScript
*/
const componentNamesWithJavaScript = await getComponentNamesFiltered(
(componentName, componentFiles) =>
componentFiles.some(filterPath([`**/${componentName}.mjs`])),
{ moduleRoot: paths.stats }
)
/**
* Package options
*
* @satisfies {import('@govuk-frontend/lib/names').PackageOptions}
*/
export const packageOptions = {
type: 'module',
modulePath: 'all.mjs',
moduleRoot: paths.stats
}
/**
* Rollup input paths
*/
export const modulePaths = [packageOptions.modulePath].concat(
componentNamesWithJavaScript.map(
(componentName) => `components/${componentName}/${componentName}.mjs`
)
)
/**
* Rollup module stats by path
*
* @param {string} modulePath - Rollup input path
* @returns {Promise<[string, { bundled: string; minified: string }]>} Rollup module stats
*/
export async function getStats(modulePath) {
const { dir, name } = parse(modulePath)
// Totals from Rollup `npm run build:stats` YAML output
const [bundled, minified] = await Promise.all([
getStatsByYaml(modulePath, `${dir}/${name}.yaml`),
getStatsByYaml(modulePath, `${dir}/${name}.min.yaml`)
])
return [modulePath, { bundled, minified }]
}
/**
* Rollup module stats by YAML path
*
* @param {string} modulePath - Rollup input path
* @param {string} statsPath - Rollup stats output path
* @returns {Promise<string>} Total size (formatted)
*/
async function getStatsByYaml(modulePath, statsPath) {
const { base } = parse(modulePath)
const stats = /** @type {Record<string, ModulesList> | undefined} */ (
await getYaml(join(paths.stats, `dist/${statsPath}`)).catch(() => undefined)
)
// Modules total size
const total = Object.values(stats?.[base] ?? {})
.map(({ rendered }) => rendered)
.reduce((total, rendered) => total + rendered, 0)
return `${filesize(total, { base: 2 })}`
}
/**
* @typedef {{ [modulePath: string]: { rendered: number } }} ModulesList
*/