Skip to content

Commit 968191b

Browse files
committed
Add JSDocs
1 parent c75fc86 commit 968191b

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

.size-limit.mts

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@ import type { Configuration } from 'webpack'
44

55
const __dirname = fileURLToPath(new URL('.', import.meta.url))
66

7+
/**
8+
* An array of all possible Node environments.
9+
*/
10+
const allNodeEnvs = ['development', 'production'] as const
11+
12+
/**
13+
* Represents a specific environment for a Node.js application.
14+
*/
15+
type NodeEnv = (typeof allNodeEnvs)[number]
16+
17+
/**
18+
* Set of entry points from the `package.json` file.
19+
*/
720
const packageJsonEntryPoints = new Set<string>()
821

9-
const getPackageJsonExports = async (
22+
/**
23+
* Recursively collects entry points from the `package.json` exports field.
24+
*
25+
* @param packageJsonExports - The exports field from `package.json`.
26+
* @returns - A set of package entry points.
27+
*/
28+
const collectPackageJsonExports = async (
1029
packageJsonExports:
1130
| string
1231
| Record<string, any>
@@ -31,22 +50,37 @@ const getPackageJsonExports = async (
3150
condition !== './package.json' && condition !== 'types',
3251
)
3352
.map(([_condition, entryPoint]) => entryPoint)
34-
.map(getPackageJsonExports),
53+
.map(collectPackageJsonExports),
3554
)
3655
}
3756

3857
return packageJsonEntryPoints
3958
}
4059

60+
/**
61+
* Gets all package entry points from the `package.json` file.
62+
*
63+
* @returns A promise that resolves to an array of unique package entry points.
64+
*/
4165
const getAllPackageEntryPoints = async () => {
4266
const packageJson = await import('./package.json', { with: { type: 'json' } })
4367

44-
const packageExports = await getPackageJsonExports(packageJson.exports)
68+
const packageExports = await collectPackageJsonExports(packageJson.exports)
4569

4670
return [...new Set(packageExports)]
4771
}
4872

49-
const getAllImports = async (
73+
/**
74+
* Gets all import configurations for a given entry point.
75+
* This function dynamically imports the specified entry point and generates a size limit configuration
76+
* for each named export found within the module. It includes configurations for named imports,
77+
* wildcard imports, and the default import.
78+
*
79+
* @param entryPoint - The entry point to import.
80+
* @param index - The index of the entry point in the list.
81+
* @returns A promise that resolves to a size limit configuration object.
82+
*/
83+
const getAllImportsForEntryPoint = async (
5084
entryPoint: string,
5185
index: number,
5286
): Promise<SizeLimitConfig> => {
@@ -71,10 +105,12 @@ const getAllImports = async (
71105
])
72106
}
73107

74-
const allNodeEnvs = ['development', 'production'] as const
75-
76-
type NodeEnv = (typeof allNodeEnvs)[number]
77-
108+
/**
109+
* Sets the `NODE_ENV` for a given Webpack configuration.
110+
*
111+
* @param nodeEnv - The `NODE_ENV` to set (either 'development' or 'production').
112+
* @returns A function that modifies the Webpack configuration.
113+
*/
78114
const setNodeEnv = (nodeEnv: NodeEnv) => {
79115
const modifyWebpackConfig = ((config: Configuration) => {
80116
;(config.optimization ??= {}).nodeEnv = nodeEnv
@@ -84,11 +120,17 @@ const setNodeEnv = (nodeEnv: NodeEnv) => {
84120
return modifyWebpackConfig
85121
}
86122

123+
/**
124+
* Gets all import configurations with a specified `NODE_ENV`.
125+
*
126+
* @param nodeEnv - The `NODE_ENV` to set (either 'development' or 'production').
127+
* @returns A promise that resolves to a size limit configuration object.
128+
*/
87129
const getAllImportsWithNodeEnv = async (nodeEnv: NodeEnv) => {
88130
const allPackageEntryPoints = await getAllPackageEntryPoints()
89131

90132
const allImportsFromAllEntryPoints = (
91-
await Promise.all(allPackageEntryPoints.map(getAllImports))
133+
await Promise.all(allPackageEntryPoints.map(getAllImportsForEntryPoint))
92134
).flat()
93135

94136
const modifyWebpackConfig = setNodeEnv(nodeEnv)
@@ -104,6 +146,11 @@ const getAllImportsWithNodeEnv = async (nodeEnv: NodeEnv) => {
104146
return allImportsWithNodeEnv
105147
}
106148

149+
/**
150+
* Gets the size limit configuration for all `NODE_ENV`s.
151+
*
152+
* @returns A promise that resolves to the size limit configuration object.
153+
*/
107154
const getSizeLimitConfig = async (): Promise<SizeLimitConfig> => {
108155
const sizeLimitConfig = (
109156
await Promise.all(allNodeEnvs.map(getAllImportsWithNodeEnv))

0 commit comments

Comments
 (0)