From 926a7416563bed40fa5b9862acb7126d4e3ba69c Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Wed, 1 Feb 2023 21:42:40 +0100 Subject: [PATCH] inline svg --- vite.config.js | 17 ----------------- vite.config.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 17 deletions(-) delete mode 100644 vite.config.js create mode 100644 vite.config.ts diff --git a/vite.config.js b/vite.config.js deleted file mode 100644 index 646ccb2..0000000 --- a/vite.config.js +++ /dev/null @@ -1,17 +0,0 @@ -import {defineConfig} from "vite"; - -export default defineConfig({ - plugins: [ - // splitVendorChunkPlugin(), - // visualizer(), - ], - build: { - sourcemap: true, - rollupOptions: { - input: { - "index": 'index.html', - "canvastest": 'canvastest.html', - }, - } - } -}) diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..44cdd49 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,41 @@ +import {defineConfig, Plugin} from "vite"; +import {readFileSync} from "fs"; + +function svgToDataURL(svgStr: string): string { + const encoded = encodeURIComponent(svgStr) + .replace(/'/g, '%27') + .replace(/"/g, '%22') + + const header = 'data:image/svg+xml,' + return header + encoded +} + +function customSvgLoader() { + return { + name: 'vite-svg-patch-plugin', + transform: function (code, id) { + if (id.endsWith('.svg')) { + const extractedSvg = readFileSync(id, 'utf8'); + return `export default '${svgToDataURL(extractedSvg)}'`; + } + return code; + } + } as Plugin; +} + +export default defineConfig({ + plugins: [ + // splitVendorChunkPlugin(), + // visualizer(), + customSvgLoader() + ], + build: { + sourcemap: true, + rollupOptions: { + input: { + "index": 'index.html', + "canvastest": 'canvastest.html', + }, + } + } +})