Skip to content

Commit ad80297

Browse files
committed
Make minor adjustments to optimize the code in .size-limit.mts
1 parent 2e928f9 commit ad80297

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

packages/toolkit/.size-limit.mts

+12-20
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,26 @@ const packageJsonEntryPoints = new Set<string>()
2020
* Recursively collects entry points from the `package.json` exports field.
2121
*
2222
* @param packageJsonExports - The exports field from `package.json`.
23-
* @returns - A set of package entry points.
23+
* @returns A set of package entry points.
2424
*/
25-
const collectPackageJsonExports = async (
25+
const collectPackageJsonExports = (
2626
packageJsonExports:
2727
| string
2828
| Record<string, any>
2929
| null
3030
| typeof import('./package.json').exports,
3131
) => {
32-
if (typeof packageJsonExports === 'string') {
32+
if (
33+
typeof packageJsonExports === 'string' &&
34+
packageJsonExports.endsWith('js')
35+
) {
3336
packageJsonEntryPoints.add(
3437
packageJsonExports.startsWith('./')
3538
? packageJsonExports
3639
: `./${packageJsonExports}`,
3740
)
38-
39-
return packageJsonEntryPoints
40-
}
41-
42-
if (typeof packageJsonExports === 'object' && packageJsonExports !== null) {
43-
await Promise.all(
44-
Object.entries(packageJsonExports)
45-
.filter(
46-
([condition]) =>
47-
condition !== './package.json' && condition !== 'types',
48-
)
49-
.map(([_condition, entryPoint]) => entryPoint)
50-
.map(collectPackageJsonExports),
51-
)
41+
} else if (packageJsonExports && typeof packageJsonExports === 'object') {
42+
Object.values(packageJsonExports).forEach(collectPackageJsonExports)
5243
}
5344

5445
return packageJsonEntryPoints
@@ -62,7 +53,7 @@ const collectPackageJsonExports = async (
6253
const getAllPackageEntryPoints = async () => {
6354
const packageJson = await import('./package.json', { with: { type: 'json' } })
6455

65-
const packageExports = await collectPackageJsonExports(packageJson.exports)
56+
const packageExports = collectPackageJsonExports(packageJson.exports)
6657

6758
return [...packageExports]
6859
}
@@ -82,9 +73,9 @@ const getAllImportsForEntryPoint = async (
8273
entryPoint: string,
8374
index: number,
8475
): Promise<SizeLimitConfig> => {
85-
const allNamedImports: typeof import('./src/index') = await import(entryPoint)
76+
const allNamedImports = Object.keys(await import(entryPoint))
8677

87-
return Object.keys(allNamedImports)
78+
return allNamedImports
8879
.map<Check>((namedImport) => ({
8980
path: entryPoint,
9081
name: `${index + 1}. import { ${namedImport} } from "${entryPoint}"`,
@@ -112,6 +103,7 @@ const getAllImportsForEntryPoint = async (
112103
const setNodeEnv = (nodeEnv: NodeEnv) => {
113104
const modifyWebpackConfig = ((config: Configuration) => {
114105
;(config.optimization ??= {}).nodeEnv = nodeEnv
106+
115107
return config
116108
}) satisfies Check['modifyWebpackConfig']
117109

0 commit comments

Comments
 (0)