From 1ea1c84a197717c3b7d845549c0ec7c556e33a49 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Mon, 25 Jan 2021 14:22:52 -0500 Subject: [PATCH] feat: allow .svg files to be inlined Base64 is unnecessary for SVG files: https://github.com/vitejs/vite/issues/1197\#issuecomment-738780169 Also using `Buffer.byteLength` instead of character length when comparing with the `assetsInlineLimit` option. --- packages/vite/src/node/plugins/asset.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 0f86defc276b10..fc077ef53bcfa4 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -171,11 +171,13 @@ async function fileToBuiltUrl( let url if ( config.build.lib || - (!file.endsWith('.svg') && - content.length < Number(config.build.assetsInlineLimit)) + Buffer.byteLength(content) < config.build.assetsInlineLimit ) { - // base64 inlined as a string - url = `data:${mime.getType(file)};base64,${content.toString('base64')}` + // svgs can be inlined without base64 + url = file.endsWith('.svg') + ? `data:image/svg+xml;utf8,${content}` + : // base64 inlined as a string + `data:${mime.getType(file)};base64,${content.toString('base64')}` } else { // emit as asset // rollup supports `import.meta.ROLLUP_FILE_URL_*`, but it generates code