@@ -4,9 +4,28 @@ import type { Configuration } from 'webpack'
4
4
5
5
const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) )
6
6
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
+ */
7
20
const packageJsonEntryPoints = new Set < string > ( )
8
21
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 (
10
29
packageJsonExports :
11
30
| string
12
31
| Record < string , any >
@@ -31,22 +50,37 @@ const getPackageJsonExports = async (
31
50
condition !== './package.json' && condition !== 'types' ,
32
51
)
33
52
. map ( ( [ _condition , entryPoint ] ) => entryPoint )
34
- . map ( getPackageJsonExports ) ,
53
+ . map ( collectPackageJsonExports ) ,
35
54
)
36
55
}
37
56
38
57
return packageJsonEntryPoints
39
58
}
40
59
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
+ */
41
65
const getAllPackageEntryPoints = async ( ) => {
42
66
const packageJson = await import ( './package.json' , { with : { type : 'json' } } )
43
67
44
- const packageExports = await getPackageJsonExports ( packageJson . exports )
68
+ const packageExports = await collectPackageJsonExports ( packageJson . exports )
45
69
46
70
return [ ...new Set ( packageExports ) ]
47
71
}
48
72
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 (
50
84
entryPoint : string ,
51
85
index : number ,
52
86
) : Promise < SizeLimitConfig > => {
@@ -71,10 +105,12 @@ const getAllImports = async (
71
105
] )
72
106
}
73
107
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
+ */
78
114
const setNodeEnv = ( nodeEnv : NodeEnv ) => {
79
115
const modifyWebpackConfig = ( ( config : Configuration ) => {
80
116
; ( config . optimization ??= { } ) . nodeEnv = nodeEnv
@@ -84,11 +120,17 @@ const setNodeEnv = (nodeEnv: NodeEnv) => {
84
120
return modifyWebpackConfig
85
121
}
86
122
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
+ */
87
129
const getAllImportsWithNodeEnv = async ( nodeEnv : NodeEnv ) => {
88
130
const allPackageEntryPoints = await getAllPackageEntryPoints ( )
89
131
90
132
const allImportsFromAllEntryPoints = (
91
- await Promise . all ( allPackageEntryPoints . map ( getAllImports ) )
133
+ await Promise . all ( allPackageEntryPoints . map ( getAllImportsForEntryPoint ) )
92
134
) . flat ( )
93
135
94
136
const modifyWebpackConfig = setNodeEnv ( nodeEnv )
@@ -104,6 +146,11 @@ const getAllImportsWithNodeEnv = async (nodeEnv: NodeEnv) => {
104
146
return allImportsWithNodeEnv
105
147
}
106
148
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
+ */
107
154
const getSizeLimitConfig = async ( ) : Promise < SizeLimitConfig > => {
108
155
const sizeLimitConfig = (
109
156
await Promise . all ( allNodeEnvs . map ( getAllImportsWithNodeEnv ) )
0 commit comments