question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support inlining SVG assets

See original GitHub issue

I was asked to open another issue for this.

Describe the bug

Vite doesn’t inline svg files when the documentation says it would.

Reproduction

https://bitbucket.org/andylizi/test-vite-svg-inline/

Expected behavior

Actual behavior

  • logo.svg is not inlined.

System Info

  • vite version: v1.0.0-rc.13
  • Operating System: Windows 10 (64-bit)
  • Node version: v14.8.0

Related code

https://github.com/vitejs/vite/blob/480367b83a5d418e76a4a6bccd004abb97413c22/src/node/build/buildPluginAsset.ts#L60-L63

Preferred solution

Adding support for svg inlining would be great. Unfortunately extra steps are required to do it properly, as #1197 mentioned: Probably Don’t Base64 SVG and Optimizing SVGs in data URIs.

Alternative solution

Document this behavior in config.ts so users wouldn’t be surprised by this.

Workaround

Rename .svg to uppercase .SVG. This isn’t ideal but it works for now.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:30
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
cslecourscommented, May 21, 2021

While this is in the process of getting fixed, here is my current patchy solution (leveraging webpack’s svg-inline-loader 😅 )

Add svgLoader() to your plugins array and you’re good to go!

import { getExtractedSVG } from "svg-inline-loader"
import type { Plugin } from "rollup"
import fs from "fs"

//TODO: remove this once https://github.com/vitejs/vite/pull/2909 gets merged
export const svgLoader: (options?: {
  classPrefix?: string
  idPrefix?: string
  removeSVGTagAttrs?: boolean
  warnTags?: boolean
  removeTags?: boolean
  warnTagAttrs?: boolean
  removingTagAttrs?: boolean
}) => Plugin = (options?: {}) => {
  return {
    name: "vite-svg-patch-plugin",
    transform: function (code, id) {
      if (
        id.endsWith(".svg")
      ) {
        const extractedSvg = fs.readFileSync(id, "utf8")
        return `export default '${getExtractedSVG(extractedSvg, options)}'`
      }
      return code
    }
  }
}
3reactions
ByteAtATimecommented, Aug 24, 2022

Any updates on this? Currently, I have a few websites that load multiple SVG images, which make them load pretty slowly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inline SVG... Cached - CSS-Tricks
While inline SVG icons is convenient for style individual elements within the icons, it does not separate presentation from content. In other ...
Read more >
The Best Way to Embed SVG on HTML (2021) - Vecta.io
3. Using inline SVG in HTML5 ; Browser support, Supported across all major browsers that support SVG (IE9+). ; Alt and title attributes,...
Read more >
How to Embed, Inline, and Reference SVG Files in Svelte
The @rollup/plugin-image is the most popular of the plugins, and can support more formats than just SVGs (also supports JPG, PNG, etc.), but ......
Read more >
Inline SVG doesn't works - WordPress.org
The inline mode doesn't works as expected, I need it to do some animations based on ... https://ps.w.org/svg-support/assets/screenshot-2.png?rev=1860006.
Read more >
Inlining svgs - HubSpot Community
This would be for both css svg assets such as custom bullet images and for. ... inlining SVGs, but it's evident that SVG...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found