Skip to content

Commit 28f3382

Browse files
committed
Update size-limit workflow and configuration
1 parent db6f2c5 commit 28f3382

File tree

3 files changed

+86
-129
lines changed

3 files changed

+86
-129
lines changed

.github/workflows/size.yml

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1-
name: size
2-
on:
3-
pull_request:
4-
branches:
5-
- master
6-
- 'feature/infinite-query-integration'
7-
permissions:
8-
pull-requests: write
1+
name: Check Bundle-Size
2+
3+
on: [push, pull_request, workflow_dispatch]
4+
95
jobs:
106
size:
117
runs-on: ubuntu-latest
8+
name: Check Bundle-Size
9+
10+
strategy:
11+
matrix:
12+
node: ['22.x']
13+
1214
env:
1315
CI_JOB_NUMBER: 1
1416
steps:
1517
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
19+
with:
20+
node-version: ${{ matrix.node }}
21+
cache: 'yarn'
22+
check-latest: true
23+
24+
- run: yarn install
25+
1626
- uses: EskiMojo14/size-limit-action@v2
27+
id: size
28+
continue-on-error: true
29+
1730
with:
1831
directory: packages/toolkit
1932
github_token: ${{ secrets.GITHUB_TOKEN }}
2033
build_script: build
2134
package_manager: yarn
2235
size_margin: non-zero
36+
37+
- name: Run size-limit locally
38+
if: ${{ success() && steps.size.outcome == 'failure' }}
39+
working-directory: packages/toolkit
40+
run: |
41+
yarn run build
42+
yarn run size

packages/toolkit/.size-limit.mts

+57-120
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,69 @@
11
import type { Check, SizeLimitConfig } from 'size-limit'
22
import type { Configuration } from 'webpack'
3+
import packageJson from './package.json' with { type: 'json' }
34

45
/**
56
* An array of all possible Node environments.
67
*/
78
const allNodeEnvs = ['production'] as const
89

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 = [
76-
'./dist/redux-toolkit.modern.mjs',
77-
'./dist/react/redux-toolkit-react.modern.mjs',
78-
'./dist/query/rtk-query.modern.mjs',
79-
'./dist/query/react/rtk-query-react.modern.mjs',
80-
]
81-
82-
const allImportsFromAllEntryPoints = (
83-
await Promise.all(allPackageEntryPoints.map(getAllImportsForEntryPoint))
84-
).flat()
85-
86-
const modifyWebpackConfig = setNodeEnv(nodeEnv)
87-
88-
const allImportsWithNodeEnv = allImportsFromAllEntryPoints.map<Check>(
89-
(importsFromEntryPoint) => ({
90-
...importsFromEntryPoint,
91-
name: `${importsFromEntryPoint.name} ('${nodeEnv}' mode)`,
92-
modifyWebpackConfig,
10+
const allPackageEntryPoints = [
11+
'./dist/redux-toolkit.modern.mjs',
12+
'./dist/react/redux-toolkit-react.modern.mjs',
13+
'./dist/query/rtk-query.modern.mjs',
14+
'./dist/query/react/rtk-query-react.modern.mjs',
15+
] as const
16+
17+
const dependencies = Object.keys(packageJson.dependencies ?? {})
18+
19+
const sizeLimitConfig: SizeLimitConfig = (
20+
await Promise.all(
21+
allNodeEnvs.flatMap((nodeEnv) => {
22+
const modifyWebpackConfig = ((config: Configuration) => {
23+
;(config.optimization ??= {}).nodeEnv = nodeEnv
24+
25+
return config
26+
}) satisfies Check['modifyWebpackConfig']
27+
28+
return allPackageEntryPoints.map(async (entryPoint, index) => {
29+
const allNamedImports = Object.keys(await import(entryPoint)).filter(
30+
(namedImport) => namedImport !== 'default',
31+
)
32+
33+
const sizeLimitConfigWithDependencies = allNamedImports
34+
.map<Check>((namedImport, namedImportIndex) => ({
35+
path: entryPoint,
36+
name: `${index + 1}-${namedImportIndex + 1}. import { ${namedImport} } from "${entryPoint}" ('${nodeEnv}' mode)`,
37+
import: `{ ${namedImport} }`,
38+
modifyWebpackConfig,
39+
}))
40+
.concat([
41+
{
42+
path: entryPoint,
43+
name: `${index + 1}-${allNamedImports.length + 1}. import * from "${entryPoint}" ('${nodeEnv}' mode)`,
44+
import: '*',
45+
modifyWebpackConfig,
46+
},
47+
{
48+
path: entryPoint,
49+
name: `${index + 1}-${allNamedImports.length + 2}. import "${entryPoint}" ('${nodeEnv}' mode)`,
50+
modifyWebpackConfig,
51+
},
52+
])
53+
54+
const sizeLimitConfigWithoutDependencies =
55+
sizeLimitConfigWithDependencies.map((check) => ({
56+
...check,
57+
name: `${check.name} (excluding dependencies)`,
58+
ignore: dependencies,
59+
}))
60+
61+
return sizeLimitConfigWithDependencies.concat(
62+
sizeLimitConfigWithoutDependencies,
63+
)
64+
})
9365
}),
9466
)
95-
96-
return allImportsWithNodeEnv
97-
}
98-
99-
/**
100-
* Gets the size limit configuration for all `NODE_ENV`s.
101-
*
102-
* @returns A promise that resolves to the size limit configuration object.
103-
*/
104-
const getSizeLimitConfig = async (): Promise<SizeLimitConfig> => {
105-
const packageJson = (
106-
await import('./package.json', { with: { type: 'json' } })
107-
).default
108-
109-
const sizeLimitConfig = (
110-
await Promise.all(allNodeEnvs.map(getAllImportsWithNodeEnv))
111-
).flat()
112-
113-
if ('dependencies' in packageJson) {
114-
const dependencies = Object.keys(packageJson.dependencies ?? {})
115-
116-
const sizeLimitConfigWithoutDependencies = sizeLimitConfig.map<Check>(
117-
(check) => ({
118-
...check,
119-
name: `${check.name} (excluding dependencies)`,
120-
ignore: dependencies,
121-
}),
122-
)
123-
124-
return sizeLimitConfigWithoutDependencies
125-
}
126-
127-
return sizeLimitConfig
128-
}
129-
130-
const sizeLimitConfig: Promise<SizeLimitConfig> = getSizeLimitConfig()
67+
).flat()
13168

13269
export default sizeLimitConfig

packages/toolkit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"test:watch": "vitest --watch",
116116
"type-tests": "yarn tsc -p tsconfig.test.json --noEmit",
117117
"prepack": "yarn build",
118-
"size": "size-limit"
118+
"size": "size-limit --config $INIT_CWD/.size-limit.mts"
119119
},
120120
"files": [
121121
"dist/",

0 commit comments

Comments
 (0)