Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix size-limit config #4415

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
with:
directory: packages/toolkit
github_token: ${{ secrets.GITHUB_TOKEN }}
build_script: build-only
build_script: build
package_manager: yarn
size_margin: non-zero
22 changes: 0 additions & 22 deletions .yarn/patches/size-limit-npm-11.0.1-05996e44e7.patch

This file was deleted.

208 changes: 0 additions & 208 deletions packages/toolkit/.size-limit.cjs

This file was deleted.

132 changes: 132 additions & 0 deletions packages/toolkit/.size-limit.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import type { Check, SizeLimitConfig } from 'size-limit'
import type { Configuration } from 'webpack'

/**
* An array of all possible Node environments.
*/
const allNodeEnvs = ['production'] as const

/**
* Represents a specific environment for a Node.js application.
*/
type NodeEnv = (typeof allNodeEnvs)[number]

/**
* Gets all import configurations for a given entry point.
* This function dynamically imports the specified entry point and
* generates a size limit configuration for each named export found
* within the module. It includes configurations for named imports,
* wildcard imports, and the default import.
*
* @param entryPoint - The entry point to import.
* @param index - The index of the entry point in the list.
* @returns A promise that resolves to a size limit configuration object.
*/
const getAllImportsForEntryPoint = async (
entryPoint: string,
index: number,
): Promise<SizeLimitConfig> => {
const allNamedImports = Object.keys(await import(entryPoint)).filter(
(namedImport) => namedImport !== 'default',
)

return allNamedImports
.map<Check>((namedImport) => ({
path: entryPoint,
name: `${index + 1}. import { ${namedImport} } from "${entryPoint}"`,
import: `{ ${namedImport} }`,
}))
.concat([
{
path: entryPoint,
name: `${index + 1}. import * from "${entryPoint}"`,
import: '*',
},
{
path: entryPoint,
name: `${index + 1}. import "${entryPoint}"`,
},
])
}

/**
* Sets the `NODE_ENV` for a given Webpack configuration.
*
* @param nodeEnv - The `NODE_ENV` to set (either 'development' or 'production').
* @returns A function that modifies the Webpack configuration.
*/
const setNodeEnv = (nodeEnv: NodeEnv) => {
const modifyWebpackConfig = ((config: Configuration) => {
;(config.optimization ??= {}).nodeEnv = nodeEnv

return config
}) satisfies Check['modifyWebpackConfig']

return modifyWebpackConfig
}

/**
* Gets all import configurations with a specified `NODE_ENV`.
*
* @param nodeEnv - The `NODE_ENV` to set (either 'development' or 'production').
* @returns A promise that resolves to a size limit configuration object.
*/
const getAllImportsWithNodeEnv = async (nodeEnv: NodeEnv) => {
const allPackageEntryPoints = [
'./dist/redux-toolkit.modern.mjs',
'./dist/react/redux-toolkit-react.modern.mjs',
'./dist/query/rtk-query.modern.mjs',
'./dist/query/react/rtk-query-react.modern.mjs',
]

const allImportsFromAllEntryPoints = (
await Promise.all(allPackageEntryPoints.map(getAllImportsForEntryPoint))
).flat()

const modifyWebpackConfig = setNodeEnv(nodeEnv)

const allImportsWithNodeEnv = allImportsFromAllEntryPoints.map<Check>(
(importsFromEntryPoint) => ({
...importsFromEntryPoint,
name: `${importsFromEntryPoint.name} ('${nodeEnv}' mode)`,
modifyWebpackConfig,
}),
)

return allImportsWithNodeEnv
}

/**
* Gets the size limit configuration for all `NODE_ENV`s.
*
* @returns A promise that resolves to the size limit configuration object.
*/
const getSizeLimitConfig = async (): Promise<SizeLimitConfig> => {
const packageJson = (
await import('./package.json', { with: { type: 'json' } })
).default

const sizeLimitConfig = (
await Promise.all(allNodeEnvs.map(getAllImportsWithNodeEnv))
).flat()

if ('dependencies' in packageJson) {
const dependencies = Object.keys(packageJson.dependencies ?? {})

const sizeLimitConfigWithoutDependencies = sizeLimitConfig.map<Check>(
(check) => ({
...check,
name: `${check.name} (excluding dependencies)`,
ignore: dependencies,
}),
)

return sizeLimitConfigWithoutDependencies
}

return sizeLimitConfig
}

const sizeLimitConfig: Promise<SizeLimitConfig> = getSizeLimitConfig()

export default sizeLimitConfig
6 changes: 3 additions & 3 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"@babel/helper-module-imports": "^7.24.7",
"@microsoft/api-extractor": "^7.13.2",
"@phryneas/ts-version": "^1.0.2",
"@size-limit/file": "^11.0.1",
"@size-limit/webpack": "^11.0.1",
"@size-limit/file": "^11.1.6",
"@size-limit/webpack": "^11.1.6",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.1.5",
"@types/babel__core": "^7.20.5",
Expand Down Expand Up @@ -91,7 +91,7 @@
"prettier": "^3.2.5",
"query-string": "^7.0.1",
"rimraf": "^3.0.2",
"size-limit": "^11.0.1",
"size-limit": "^11.1.6",
"tslib": "^1.10.0",
"tsup": "^8.2.3",
"tsx": "^4.19.0",
Expand Down
Loading