Skip to content

Instantly share code, notes, and snippets.

@zaosoula
Created December 27, 2023 18:11
Show Gist options
  • Save zaosoula/1366bba5a5a5eee8512b3f01f5624914 to your computer and use it in GitHub Desktop.
Save zaosoula/1366bba5a5a5eee8512b3f01f5624914 to your computer and use it in GitHub Desktop.
VITE + SVGR + SVGO: Unique svg's ids prefix
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