Vite plugin to add svg as inline data (#1072)

* add vite plugin to add svg as inline data

* Add node types package
This commit is contained in:
Ajay Bura 2023-01-15 09:52:58 +05:30 committed by GitHub
parent 9a34daa2bc
commit 38bbc1c6f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 7 deletions

16
viteSvgLoader.ts Normal file
View file

@ -0,0 +1,16 @@
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;
},
});