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

Add size-limit config #2173

Draft
wants to merge 8 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
25 changes: 17 additions & 8 deletions .github/workflows/size.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
name: Bundle Size
name: size

on: [pull_request]
on:
pull_request:
branches:
- master

permissions:
pull-requests: write

jobs:
build:
size:
name: Check compressed size
runs-on: ubuntu-latest

env:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v4
- uses: EskiMojo14/size-limit-action@v2
with:
fetch-depth: 1
- uses: preactjs/compressed-size-action@v2
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
directory: .
github_token: ${{ secrets.GITHUB_TOKEN }}
build_script: build
package_manager: yarn
size_margin: non-zero
127 changes: 127 additions & 0 deletions .size-limit.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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/react-redux.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: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"test": "vitest --run --typecheck",
"test:watch": "vitest --watch",
"type-tests": "tsc --noEmit -p tsconfig.test.json",
"size": "size-limit",
"coverage": "codecov"
},
"peerDependencies": {
Expand Down Expand Up @@ -79,10 +80,12 @@
"@babel/preset-typescript": "^7.24.7",
"@microsoft/api-extractor": "^7.47.0",
"@reduxjs/toolkit": "^2.2.5",
"@size-limit/file": "^11.1.6",
"@size-limit/webpack": "^11.1.6",
"@testing-library/dom": "^10.1.0",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^16.0.0",
"@types/node": "^20.14.2",
"@types/node": "^22.9.0",
"@types/prop-types": "^15.7.12",
"@types/react": "18.3.3",
"@types/react-dom": "^18.3.0",
Expand All @@ -102,6 +105,7 @@
"react-test-renderer": "18.3.1",
"redux": "^5.0.1",
"rimraf": "^5.0.7",
"size-limit": "^11.1.6",
"tsup": "7.0.0",
"typescript": "^5.5.4",
"typescript-eslint": "^7.12.0",
Expand Down
Loading
Loading