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("_");