cinny/viteSvgLoader.ts
Ajay Bura 38bbc1c6f5
Vite plugin to add svg as inline data (#1072)
* add vite plugin to add svg as inline data

* Add node types package
2023-01-15 09:52:58 +05:30

16 lines
519 B
TypeScript

import svgToMiniDataURI from 'mini-svg-data-uri';
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 = (): Plugin => ({
name: 'vite-svg-patch-plugin',
transform: (code, id) => {
if (id.endsWith('.svg')) {
const extractedSvg = fs.readFileSync(id, 'utf8');
const datauri = svgToMiniDataURI.toSrcset(extractedSvg);
return `export default "${datauri}"`;
}
return code;
},
});