|
1 | 1 | import type { Check, SizeLimitConfig } from 'size-limit' |
2 | 2 | import type { Configuration } from 'webpack' |
| 3 | +import packageJson from './package.json' with { type: 'json' } |
3 | 4 |
|
4 | 5 | /** |
5 | 6 | * An array of all possible Node environments. |
6 | 7 | */ |
7 | 8 | const allNodeEnvs = ['production'] as const |
8 | 9 |
|
9 | | -/** |
10 | | - * Represents a specific environment for a Node.js application. |
11 | | - */ |
12 | | -type NodeEnv = (typeof allNodeEnvs)[number] |
13 | | - |
14 | | -/** |
15 | | - * Gets all import configurations for a given entry point. |
16 | | - * This function dynamically imports the specified entry point and |
17 | | - * generates a size limit configuration for each named export found |
18 | | - * within the module. It includes configurations for named imports, |
19 | | - * wildcard imports, and the default import. |
20 | | - * |
21 | | - * @param entryPoint - The entry point to import. |
22 | | - * @param index - The index of the entry point in the list. |
23 | | - * @returns A promise that resolves to a size limit configuration object. |
24 | | - */ |
25 | | -const getAllImportsForEntryPoint = async ( |
26 | | - entryPoint: string, |
27 | | - index: number, |
28 | | -): Promise<SizeLimitConfig> => { |
29 | | - const allNamedImports = Object.keys(await import(entryPoint)).filter( |
30 | | - (namedImport) => namedImport !== 'default', |
31 | | - ) |
32 | | - |
33 | | - return allNamedImports |
34 | | - .map<Check>((namedImport) => ({ |
35 | | - path: entryPoint, |
36 | | - name: `${index + 1}. import { ${namedImport} } from "${entryPoint}"`, |
37 | | - import: `{ ${namedImport} }`, |
38 | | - })) |
39 | | - .concat([ |
40 | | - { |
41 | | - path: entryPoint, |
42 | | - name: `${index + 1}. import * from "${entryPoint}"`, |
43 | | - import: '*', |
44 | | - }, |
45 | | - { |
46 | | - path: entryPoint, |
47 | | - name: `${index + 1}. import "${entryPoint}"`, |
48 | | - }, |
49 | | - ]) |
50 | | -} |
51 | | - |
52 | | -/** |
53 | | - * Sets the `NODE_ENV` for a given Webpack configuration. |
54 | | - * |
55 | | - * @param nodeEnv - The `NODE_ENV` to set (either 'development' or 'production'). |
56 | | - * @returns A function that modifies the Webpack configuration. |
57 | | - */ |
58 | | -const setNodeEnv = (nodeEnv: NodeEnv) => { |
59 | | - const modifyWebpackConfig = ((config: Configuration) => { |
60 | | - ;(config.optimization ??= {}).nodeEnv = nodeEnv |
61 | | - |
62 | | - return config |
63 | | - }) satisfies Check['modifyWebpackConfig'] |
64 | | - |
65 | | - return modifyWebpackConfig |
66 | | -} |
67 | | - |
68 | | -/** |
69 | | - * Gets all import configurations with a specified `NODE_ENV`. |
70 | | - * |
71 | | - * @param nodeEnv - The `NODE_ENV` to set (either 'development' or 'production'). |
72 | | - * @returns A promise that resolves to a size limit configuration object. |
73 | | - */ |
74 | | -const getAllImportsWithNodeEnv = async (nodeEnv: NodeEnv) => { |
75 | | - const allPackageEntryPoints = ['./dist/react-redux.mjs'] |
76 | | - |
77 | | - const allImportsFromAllEntryPoints = ( |
78 | | - await Promise.all(allPackageEntryPoints.map(getAllImportsForEntryPoint)) |
79 | | - ).flat() |
80 | | - |
81 | | - const modifyWebpackConfig = setNodeEnv(nodeEnv) |
82 | | - |
83 | | - const allImportsWithNodeEnv = allImportsFromAllEntryPoints.map<Check>( |
84 | | - (importsFromEntryPoint) => ({ |
85 | | - ...importsFromEntryPoint, |
86 | | - name: `${importsFromEntryPoint.name} ('${nodeEnv}' mode)`, |
87 | | - modifyWebpackConfig, |
| 10 | +const allPackageEntryPoints = ['./dist/react-redux.mjs'] as const |
| 11 | + |
| 12 | +const dependencies = Object.keys(packageJson.dependencies ?? {}) |
| 13 | + |
| 14 | +const sizeLimitConfig: SizeLimitConfig = ( |
| 15 | + await Promise.all( |
| 16 | + allNodeEnvs.flatMap((nodeEnv) => { |
| 17 | + const modifyWebpackConfig = ((config: Configuration) => { |
| 18 | + ;(config.optimization ??= {}).nodeEnv = nodeEnv |
| 19 | + |
| 20 | + return config |
| 21 | + }) satisfies Check['modifyWebpackConfig'] |
| 22 | + |
| 23 | + return allPackageEntryPoints.map(async (entryPoint, index) => { |
| 24 | + const allNamedImports = Object.keys(await import(entryPoint)).filter( |
| 25 | + (namedImport) => namedImport !== 'default', |
| 26 | + ) |
| 27 | + |
| 28 | + const sizeLimitConfigWithDependencies = allNamedImports |
| 29 | + .map<Check>((namedImport, namedImportIndex) => ({ |
| 30 | + path: entryPoint, |
| 31 | + name: `${index + 1}-${namedImportIndex + 1}. import { ${namedImport} } from "${entryPoint}" ('${nodeEnv}' mode)`, |
| 32 | + import: `{ ${namedImport} }`, |
| 33 | + modifyWebpackConfig, |
| 34 | + })) |
| 35 | + .concat([ |
| 36 | + { |
| 37 | + path: entryPoint, |
| 38 | + name: `${index + 1}-${allNamedImports.length + 1}. import * from "${entryPoint}" ('${nodeEnv}' mode)`, |
| 39 | + import: '*', |
| 40 | + modifyWebpackConfig, |
| 41 | + }, |
| 42 | + { |
| 43 | + path: entryPoint, |
| 44 | + name: `${index + 1}-${allNamedImports.length + 2}. import "${entryPoint}" ('${nodeEnv}' mode)`, |
| 45 | + modifyWebpackConfig, |
| 46 | + }, |
| 47 | + ]) |
| 48 | + |
| 49 | + const sizeLimitConfigWithoutDependencies = |
| 50 | + sizeLimitConfigWithDependencies.map((check) => ({ |
| 51 | + ...check, |
| 52 | + name: `${check.name} (excluding dependencies)`, |
| 53 | + ignore: dependencies, |
| 54 | + })) |
| 55 | + |
| 56 | + return sizeLimitConfigWithDependencies.concat( |
| 57 | + sizeLimitConfigWithoutDependencies, |
| 58 | + ) |
| 59 | + }) |
88 | 60 | }), |
89 | 61 | ) |
90 | | - |
91 | | - return allImportsWithNodeEnv |
92 | | -} |
93 | | - |
94 | | -/** |
95 | | - * Gets the size limit configuration for all `NODE_ENV`s. |
96 | | - * |
97 | | - * @returns A promise that resolves to the size limit configuration object. |
98 | | - */ |
99 | | -const getSizeLimitConfig = async (): Promise<SizeLimitConfig> => { |
100 | | - const packageJson = ( |
101 | | - await import('./package.json', { with: { type: 'json' } }) |
102 | | - ).default |
103 | | - |
104 | | - const sizeLimitConfig = ( |
105 | | - await Promise.all(allNodeEnvs.map(getAllImportsWithNodeEnv)) |
106 | | - ).flat() |
107 | | - |
108 | | - if ('dependencies' in packageJson) { |
109 | | - const dependencies = Object.keys(packageJson.dependencies ?? {}) |
110 | | - |
111 | | - const sizeLimitConfigWithoutDependencies = sizeLimitConfig.map<Check>( |
112 | | - (check) => ({ |
113 | | - ...check, |
114 | | - name: `${check.name} (excluding dependencies)`, |
115 | | - ignore: dependencies, |
116 | | - }), |
117 | | - ) |
118 | | - |
119 | | - return sizeLimitConfigWithoutDependencies |
120 | | - } |
121 | | - |
122 | | - return sizeLimitConfig |
123 | | -} |
124 | | - |
125 | | -const sizeLimitConfig: Promise<SizeLimitConfig> = getSizeLimitConfig() |
| 62 | +).flat() |
126 | 63 |
|
127 | 64 | export default sizeLimitConfig |
0 commit comments