Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Oct 18, 2023
1 parent 415ee55 commit 2eee0ec
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,7 @@ async function fileToBuiltUrl(
}

if (file.endsWith('.svg')) {
const stringContent = content.toString()
// If the SVG contains some text, any transformation is unsafe, and given that double quotes would then
// need to be escaped, the gain to use a data URI would be ridiculous if not negative
if (stringContent.includes('<text')) {
url = `data:image/svg+xml;base64,${content.toString('base64')}`
} else {
url = svgToDataURL(stringContent)
}
url = svgToDataURL(content)
} else {
const mimeType = mrmime.lookup(file) ?? 'application/octet-stream'
// base64 inlined as a string
Expand Down Expand Up @@ -442,16 +435,26 @@ export async function urlToBuiltUrl(
}

// Inspired by https:/iconify/iconify/blob/main/packages/utils/src/svg/url.ts
function svgToDataURL(svg: string): string {
return (
'data:image/svg+xml,' +
svg
.trim()
.replaceAll(/\s+/g, ' ')
.replaceAll('"', "'")
.replaceAll('%', '%25')
.replaceAll('#', '%23')
.replaceAll('<', '%3c')
.replaceAll('>', '%3e')
)
function svgToDataURL(content: Buffer): string {
const stringContent = content.toString()
// If the SVG contains some text, any transformation is unsafe, and given that double quotes would then
// need to be escaped, the gain to use a data URI would be ridiculous if not negative
if (stringContent.includes('<text')) {
return `data:image/svg+xml;base64,${content.toString('base64')}`
} else {
return (
'data:image/svg+xml,' +
stringContent
.trim()
.replaceAll('"', "'")
.replaceAll('%', '%25')
.replaceAll('#', '%23')
.replaceAll('<', '%3c')
.replaceAll('>', '%3e')
// Spaces are not valid in srcset it has some use cases
// it can make the uncompressed URI slightly higher than base64, but will compress way better
// https:/vitejs/vite/pull/14643#issuecomment-1766288673
.replaceAll(/\s+/g, '%20')
)
}
}

0 comments on commit 2eee0ec

Please sign in to comment.