-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Backport: ci(bundle-size): initial version #9419
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
Merged
+168
−263
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
.turbo | ||
dist | ||
dist-ssr | ||
dist-bundle-check | ||
examples/*/build | ||
node_modules | ||
public/dist | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { build } from 'esbuild'; | ||
import { writeFileSync, statSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
// Bundle size limits in bytes | ||
const LIMIT = 510 * 1024; | ||
|
||
interface BundleResult { | ||
size: number; | ||
path: string; | ||
condition: string; | ||
} | ||
|
||
async function bundleForNode(): Promise<BundleResult> { | ||
const outfile = join(process.cwd(), 'dist-bundle-check', 'node.js'); | ||
const metafile = join(process.cwd(), 'dist-bundle-check', 'node-meta.json'); | ||
|
||
const result = await build({ | ||
entryPoints: [join(process.cwd(), 'src', 'index.ts')], | ||
bundle: true, | ||
platform: 'node', | ||
target: 'es2020', | ||
format: 'esm', | ||
outfile, | ||
metafile: true, | ||
minify: true, | ||
treeShaking: true, | ||
external: ['arktype', 'effect', '@valibot/to-json-schema'], | ||
}); | ||
writeFileSync(metafile, JSON.stringify(result.metafile, null, 2)); | ||
|
||
const size = statSync(outfile).size; | ||
return { size, path: outfile, condition: 'node' }; | ||
} | ||
|
||
async function bundleForBrowser(): Promise<BundleResult> { | ||
const outfile = join(process.cwd(), 'dist-bundle-check', 'browser.js'); | ||
const metafile = join( | ||
process.cwd(), | ||
'dist-bundle-check', | ||
'browser-meta.json', | ||
); | ||
|
||
const result = await build({ | ||
entryPoints: [join(process.cwd(), 'src', 'index.ts')], | ||
bundle: true, | ||
platform: 'browser', | ||
target: 'es2020', | ||
format: 'esm', | ||
outfile, | ||
metafile: true, | ||
minify: true, | ||
treeShaking: true, | ||
conditions: ['browser'], | ||
external: ['arktype', 'effect', '@valibot/to-json-schema'], | ||
}); | ||
writeFileSync(metafile, JSON.stringify(result.metafile, null, 2)); | ||
|
||
const size = statSync(outfile).size; | ||
return { size, path: outfile, condition: 'browser' }; | ||
} | ||
|
||
function formatSize(bytes: number): string { | ||
return `${(bytes / 1024).toFixed(2)} KB`; | ||
} | ||
|
||
function checkSize(result: BundleResult, limit: number): boolean { | ||
const passed = result.size <= limit; | ||
const status = passed ? '✅' : '❌'; | ||
const percentage = ((result.size / limit) * 100).toFixed(1); | ||
|
||
console.log( | ||
`${status} ${result.condition.padEnd(10)} ${formatSize(result.size).padEnd(12)} (${percentage}% of ${formatSize(limit)} limit)`, | ||
); | ||
|
||
return passed; | ||
} | ||
|
||
async function main() { | ||
console.log('📦 Checking bundle sizes...\n'); | ||
|
||
try { | ||
const [nodeResult, browserResult] = await Promise.all([ | ||
bundleForNode(), | ||
bundleForBrowser(), | ||
]); | ||
|
||
console.log('Bundle sizes:'); | ||
const nodePass = checkSize(nodeResult, LIMIT); | ||
const browserPass = checkSize(browserResult, LIMIT); | ||
|
||
console.log('\n---'); | ||
|
||
console.log('📦 Bundle size check complete.'); | ||
console.log( | ||
'Upload dist-bundle-check/*.json files to https://esbuild.github.io/analyze/ for detailed analysis.', | ||
); | ||
|
||
console.log('\n---'); | ||
|
||
if (nodePass && browserPass) { | ||
console.log('✅ All bundle size checks passed!'); | ||
process.exit(0); | ||
} else { | ||
console.log('❌ Bundle size check failed!'); | ||
console.log('\nTo fix this, either:'); | ||
console.log('1. Reduce the bundle size by optimizing code'); | ||
console.log( | ||
'2. Update the limit at https://github.com/vercel/ai/settings/variables/actions/BUNDLE_SIZE_LIMIT_KB', | ||
); | ||
process.exit(1); | ||
} | ||
} catch (error) { | ||
console.error('Error during bundle size check:', error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
main(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message suggests updating a GitHub Actions variable
BUNDLE_SIZE_LIMIT_KB
, but the script uses a hardcoded limit and doesn't read from any environment variable, making this guidance ineffective and misleading.View Details
📝 Patch Details
Analysis
Bundle size check error message references non-functional GitHub Actions variable
What fails: The
check-bundle-size.ts
script error message at line 109 instructs users to updateBUNDLE_SIZE_LIMIT_KB
at https://github.com/vercel/ai/settings/variables/actions/BUNDLE_SIZE_LIMIT_KB, but the script hardcodes the limit at line 6 (const LIMIT = 510 * 1024;
) and never reads from environment variables.How to reproduce:
Result: When a bundle size check fails, users follow the error message guidance to update the GitHub Actions variable, but the script continues failing because it never reads that variable. This wastes developer time troubleshooting why their configuration change has no effect.
Expected: The script should read from
process.env.BUNDLE_SIZE_LIMIT_KB
and the CI workflow should pass to the script, making the error message's guidance functional.Fix: Modified the script to read from environment variable with fallback to hardcoded default, and updated the CI workflow to pass the GitHub Actions variable per GitHub Actions variables documentation.