Skip to content

Commit c75fc86

Browse files
committed
Refactor to get all exports
1 parent becd2e3 commit c75fc86

File tree

1 file changed

+63
-27
lines changed

1 file changed

+63
-27
lines changed

.size-limit.mts

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,47 @@
1-
import path from 'node:path'
21
import { fileURLToPath } from 'node:url'
32
import type { Check, SizeLimitConfig } from 'size-limit'
3+
import type { Configuration } from 'webpack'
44

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

7+
const packageJsonEntryPoints = new Set<string>()
8+
9+
const getPackageJsonExports = async (
10+
packageJsonExports:
11+
| string
12+
| Record<string, any>
13+
| null
14+
| typeof import('./package.json').exports,
15+
) => {
16+
if (typeof packageJsonExports === 'string') {
17+
packageJsonEntryPoints.add(
18+
packageJsonExports.startsWith('./')
19+
? packageJsonExports
20+
: `./${packageJsonExports}`,
21+
)
22+
23+
return packageJsonEntryPoints
24+
}
25+
26+
if (typeof packageJsonExports === 'object' && packageJsonExports !== null) {
27+
await Promise.all(
28+
Object.entries(packageJsonExports)
29+
.filter(
30+
([condition]) =>
31+
condition !== './package.json' && condition !== 'types',
32+
)
33+
.map(([_condition, entryPoint]) => entryPoint)
34+
.map(getPackageJsonExports),
35+
)
36+
}
37+
38+
return packageJsonEntryPoints
39+
}
40+
741
const getAllPackageEntryPoints = async () => {
842
const packageJson = await import('./package.json', { with: { type: 'json' } })
943

10-
const packageExports = Object.entries(packageJson.exports['.'])
11-
.filter(([condition]) => condition !== 'types')
12-
.map(([_condition, entryPoint]) =>
13-
path.isAbsolute(entryPoint) ? `./${entryPoint}` : entryPoint,
14-
)
44+
const packageExports = await getPackageJsonExports(packageJson.exports)
1545

1646
return [...new Set(packageExports)]
1747
}
@@ -20,63 +50,69 @@ const getAllImports = async (
2050
entryPoint: string,
2151
index: number,
2252
): Promise<SizeLimitConfig> => {
23-
const allNamedImports = await import(entryPoint)
53+
const allNamedImports: typeof import('./src/index') = await import(entryPoint)
2454

2555
return Object.keys(allNamedImports)
2656
.map<Check>((namedImport) => ({
2757
path: entryPoint,
28-
name: `import { ${namedImport} } from "${entryPoint}"`,
58+
name: `${index + 1}. import { ${namedImport} } from "${entryPoint}"`,
2959
import: `{ ${namedImport} }`,
30-
modifyWebpackConfig: (config) => {
31-
config.optimization.nodeEnv = 'development'
32-
return config
33-
},
3460
}))
3561
.concat([
3662
{
3763
path: entryPoint,
38-
name: `import * from "${entryPoint}"`,
64+
name: `${index + 1}. import * from "${entryPoint}"`,
3965
import: '*',
4066
},
4167
{
4268
path: entryPoint,
43-
name: `import "${entryPoint}"`,
69+
name: `${index + 1}. import "${entryPoint}"`,
4470
},
4571
])
4672
}
4773

48-
const setNodeEnv = (
49-
nodeEnv: 'development' | 'production',
50-
): Check['modifyWebpackConfig'] => {
51-
return (config) => {
52-
config.optimization.nodeEnv = nodeEnv
74+
const allNodeEnvs = ['development', 'production'] as const
75+
76+
type NodeEnv = (typeof allNodeEnvs)[number]
77+
78+
const setNodeEnv = (nodeEnv: NodeEnv) => {
79+
const modifyWebpackConfig = ((config: Configuration) => {
80+
;(config.optimization ??= {}).nodeEnv = nodeEnv
5381
return config
54-
}
82+
}) satisfies Check['modifyWebpackConfig']
83+
84+
return modifyWebpackConfig
5585
}
5686

57-
const getAllImportsWithNodeEnv = async (
58-
nodeEnv: 'development' | 'production',
59-
) => {
87+
const getAllImportsWithNodeEnv = async (nodeEnv: NodeEnv) => {
6088
const allPackageEntryPoints = await getAllPackageEntryPoints()
6189

6290
const allImportsFromAllEntryPoints = (
6391
await Promise.all(allPackageEntryPoints.map(getAllImports))
6492
).flat()
6593

66-
const allImportsWithNodeEnv = allImportsFromAllEntryPoints.map(
94+
const modifyWebpackConfig = setNodeEnv(nodeEnv)
95+
96+
const allImportsWithNodeEnv = allImportsFromAllEntryPoints.map<Check>(
6797
(importsFromEntryPoint) => ({
6898
...importsFromEntryPoint,
6999
name: `${importsFromEntryPoint.name} ('${nodeEnv}' mode)`,
70-
modifyWebpackConfig: setNodeEnv(nodeEnv),
100+
modifyWebpackConfig,
71101
}),
72102
)
73103

74104
return allImportsWithNodeEnv
75105
}
76106

77-
const allNodeEnvs = ['development', 'production'] as const
107+
const getSizeLimitConfig = async (): Promise<SizeLimitConfig> => {
108+
const sizeLimitConfig = (
109+
await Promise.all(allNodeEnvs.map(getAllImportsWithNodeEnv))
110+
).flat()
111+
112+
return sizeLimitConfig
113+
}
78114

79115
const sizeLimitConfig: Promise<SizeLimitConfig> = (async () =>
80-
(await Promise.all(allNodeEnvs.map(getAllImportsWithNodeEnv))).flat())()
116+
await getSizeLimitConfig())()
81117

82118
export default sizeLimitConfig

0 commit comments

Comments
 (0)