Created
December 27, 2023 18:11
-
-
Save zaosoula/1366bba5a5a5eee8512b3f01f5624914 to your computer and use it in GitHub Desktop.
VITE + SVGR + SVGO: Unique svg's ids prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineConfig } from 'vite'; | |
import react from '@vitejs/plugin-react'; | |
import svgr from "vite-plugin-svgr"; | |
import { customAlphabet } from 'nanoid'; | |
const prefixList = new Set(); | |
const nanoid = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 10); | |
const uniquePrefix = () => { | |
let prefix; | |
do { | |
prefix = nanoid(); | |
} while (prefixList.has(prefix)); | |
return prefix; | |
}; | |
// https://vitejs.dev/config/ | |
export default defineConfig({ | |
plugins: [react(), svgr({ | |
svgrOptions: { | |
plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"], | |
svgoConfig: { | |
plugins: [{ name: 'prefixIds', params: { prefix: uniquePrefix } }] | |
} | |
} | |
})], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment