From 146e8fda046619f1cf076db34ca66768f5c29632 Mon Sep 17 00:00:00 2001 From: pheralb Date: Wed, 27 Aug 2025 19:29:55 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Implement=20SVG=20optim?= =?UTF-8?q?ization=20utility=20and=20update=20getSource=20function=20to=20?= =?UTF-8?q?use=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/templates/getSource.ts | 5 ++++- src/utils/optimizeSvg.ts | 37 +++++++++++++++++++++++++++++++++++++ src/utils/prefixSvgIds.ts | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/utils/optimizeSvg.ts diff --git a/src/templates/getSource.ts b/src/templates/getSource.ts index 6e8e859..58e3b20 100644 --- a/src/templates/getSource.ts +++ b/src/templates/getSource.ts @@ -1,3 +1,5 @@ +import { optimizeSvg } from "@/utils/optimizeSvg"; + interface SourceParams { url: string | undefined; } @@ -5,5 +7,6 @@ interface SourceParams { export const getSource = async (params: SourceParams) => { const response = await fetch(params.url || ""); const content = await response.text(); - return content; + const optimizedContent = optimizeSvg({ svgCode: content }); + return optimizedContent; }; diff --git a/src/utils/optimizeSvg.ts b/src/utils/optimizeSvg.ts new file mode 100644 index 0000000..7a3046e --- /dev/null +++ b/src/utils/optimizeSvg.ts @@ -0,0 +1,37 @@ +import { optimize } from "svgo/browser"; + +interface OptimizeSvg { + svgCode: string; +} + +export const optimizeSvg = ({ svgCode }: OptimizeSvg) => { + const svgo = optimize(svgCode, { + multipass: true, + plugins: [ + "removeDimensions", + "removeXMLNS", + "removeDoctype", + "removeComments", + "removeStyleElement", + "cleanupAttrs", + "cleanupEnableBackground", + "cleanupIds", + "minifyStyles", + "removeDoctype", + "removeDesc", + "removeEmptyAttrs", + "removeEmptyText", + "removeHiddenElems", + "removeNonInheritableGroupAttrs", + "removeUnknownsAndDefaults", + "removeUselessDefs", + "removeUselessStrokeAndFill", + "removeXMLProcInst", + { + name: "removeAttrs", + params: { attrs: "(data-name|id|class)" }, + }, + ], + }); + return svgo.data; +}; diff --git a/src/utils/prefixSvgIds.ts b/src/utils/prefixSvgIds.ts index 7693c50..bcdc589 100644 --- a/src/utils/prefixSvgIds.ts +++ b/src/utils/prefixSvgIds.ts @@ -1,4 +1,4 @@ -import { optimize } from "svgo"; +import { optimize } from "svgo/browser"; export const getPrefixFromSvgUrl = (svgUrl: string) => { return svgUrl.split("/").pop()!.replace(".svg", "").split("-").join("_");