SpiriitLabs / vite-plugin-svg-spritemap

Vite plugin to generate svg spritemap
MIT License
47 stars 4 forks source link

`__spritemap` Prefix is Not Replaced During Build #34

Closed 4everTonyStark closed 8 months ago

4everTonyStark commented 9 months ago

When runnning the "basic" demo project locally with vite dev, all SVGs load as expected. However, the use and img examples do not load after running vite build followed by vite preview. Examining the built index.html file shows that the __spritemap prefix was not replaced with the name of the output assets/spritemap.<HASH>.svg file.

It also seems that the __spritemap prefix is not replaced in React JSX components upon build, although the plugin works when running vite dev. My React component is the following:

export default function SVGIcon({name, style}) {
  return (
    <svg
      xmlns='http://www.w3.org/2000/svg'
      xmlnsXlink='http://www.w3.org/1999/xlink'
      style={{margin: '0 auto', aspectRatio: 1, ...style}}
    >
      <use xlinkHref={`__spritemap#sprite-${name}`} />
    </svg>
  );
}
4everTonyStark commented 8 months ago

After reading through the library more today, I've discovered the cause of the issue and the fix for it: the 'build' plugin's transform regex is looking for __spritemap with a leading / characater, which I was missing in both my HTML and React component. Adding the leading slash character makes the library replace the /__spritemap string properly in builds for both HTML and React components. As such, can the README and "basic" demo project be updated to add the leading slash accordingly?

Applelo commented 8 months ago

Yes you right, my bad on this one. I will push a readme fix on the next version update.

Applelo commented 8 months ago

Thanks for the issue, it was causing also a bug on Vue compilation x). It is now fix on 2.2.0 ^^