From e64bd0740b44aed5cfaf67e5c37a1c56ed4442f4 Mon Sep 17 00:00:00 2001 From: James Ross Date: Mon, 26 Feb 2024 14:42:42 +0000 Subject: [PATCH] fix: better assetsInlineLimit runtime type checking (#10154) * fix: string assetsInlineLimit * fix: better handle NaN values for `assetsInlineLimit` * chore: prettier * chore: simplify for requested changes * chore: update changeset * chore: remove tests * chore: simplify function * Apply suggestions from code review --------- Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --- .changeset/late-bears-collect.md | 5 +++++ packages/astro/src/core/build/plugins/util.ts | 17 ++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 .changeset/late-bears-collect.md diff --git a/.changeset/late-bears-collect.md b/.changeset/late-bears-collect.md new file mode 100644 index 000000000000..f2bcd193fd7a --- /dev/null +++ b/.changeset/late-bears-collect.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes an issue where `config.vite.build.assetsInlineLimit` could not be set as a function. diff --git a/packages/astro/src/core/build/plugins/util.ts b/packages/astro/src/core/build/plugins/util.ts index 7297a1f6f26b..07b18a8870ce 100644 --- a/packages/astro/src/core/build/plugins/util.ts +++ b/packages/astro/src/core/build/plugins/util.ts @@ -75,14 +75,13 @@ export function shouldInlineAsset( assetPath: string, assetsInlineLimit: NonNullable ) { - if (typeof assetsInlineLimit === 'number') { - return Buffer.byteLength(assetContent) < assetsInlineLimit; - } - - const result = assetsInlineLimit(assetPath, Buffer.from(assetContent)); - if (result != null) { - return result; + if (typeof assetsInlineLimit === 'function') { + const result = assetsInlineLimit(assetPath, Buffer.from(assetContent)); + if (result != null) { + return result; + } else { + return Buffer.byteLength(assetContent) < 4096; // Fallback to 4096kb by default (same as Vite) + } } - - return Buffer.byteLength(assetContent) < 4096; // Fallback to 4096kb by default (same as Vite) + return Buffer.byteLength(assetContent) < Number(assetsInlineLimit); }