vite-plugin-svg-sprite icon indicating copy to clipboard operation
vite-plugin-svg-sprite copied to clipboard

Option to customize export

Open jods4 opened this issue 3 years ago • 2 comments

Nice SVG plugin! Do you think you could add an option so that the module template could be changed?

In my case I'd like to export a ready-to-use Vue component, with a few bells and whistles attached, instead of just the SVD id.

Maybe a moduleTemplate(symbol: string, id: string) option with the current template as default value?

const options = {
   moduleTemplate(symbol, id) {
     return `import addSymbol from 'vite-plugin-svg-sprite/es/runtime';
        addSymbol(${symbol}, ${id});
        export default ${id};`
   }
}

jods4 avatar May 09 '21 17:05 jods4

It could be:

const options = {
   moduleTemplate({
     symbol, id, codeAddSymbol
   }) {
     return `${codeAddSymbol};
        export default ${id};`
   }
}

addSymbol is an internal function and should not be exposed to users.

meowtec avatar May 10 '21 11:05 meowtec

The tricky part is making sure import are in the right position (I need to add some) but that would work as well.

This is an advanced scenario that's only present in one place (your build definition), so I wouldn't mind if I had to rewrite the template slightly after a major release.

jods4 avatar May 10 '21 12:05 jods4