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: better assetsInlineLimit runtime type checking #10154

Merged
merged 8 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions .changeset/late-bears-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improves runtime type-checking with the vite.build.assetsInlineLimit configuration option, which could result in errors when passing invalid options like strings. Astro now automatically casts this to a number to match the Vite behaviour.
13 changes: 7 additions & 6 deletions packages/astro/src/core/build/plugins/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ export function shouldInlineAsset(
assetPath: string,
assetsInlineLimit: NonNullable<BuildOptions['assetsInlineLimit']>
) {
if (typeof assetsInlineLimit === 'number') {
return Buffer.byteLength(assetContent) < assetsInlineLimit;
if (typeof assetsInlineLimit === 'function') {
const result = assetsInlineLimit(assetPath, Buffer.from(assetContent));
if (result != null) {
return result;
}
}

const result = assetsInlineLimit(assetPath, Buffer.from(assetContent));
if (result != null) {
return result;
if (typeof assetsInlineLimit === 'number' || typeof assetsInlineLimit === 'string') {
return Buffer.byteLength(assetContent) < Number(assetsInlineLimit);
}

return Buffer.byteLength(assetContent) < 4096; // Fallback to 4096kb by default (same as Vite)
Expand Down
Loading