From dc54f914354a304e18426f42589462a9f21207ab Mon Sep 17 00:00:00 2001 From: pheralb Date: Fri, 29 Aug 2025 14:42:12 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Refactor=20parseReactSv?= =?UTF-8?q?gContent=20to=20improve=20SVG=20handling=20and=20formatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/parseReactSvgContent.ts | 35 +++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/utils/parseReactSvgContent.ts b/src/utils/parseReactSvgContent.ts index 0d8c677..ee82789 100644 --- a/src/utils/parseReactSvgContent.ts +++ b/src/utils/parseReactSvgContent.ts @@ -1,33 +1,46 @@ -import { parse, print, type Module } from "@swc/core"; +import prettier from "prettier"; interface ParseReactSvgOptions { componentName: string; svgCode: string; typescript: boolean; - minify?: boolean; } export const parseReactSvgContent = async ({ componentName, svgCode, typescript, - minify = false, }: ParseReactSvgOptions) => { - let structuredCode = ""; + const reactifiedSvg = svgCode + .replace(") => (\n ${svgCode.replace(") => (\n ${reactifiedSvg}\n);\n\n` + `export { ${componentName} };`; } else { - structuredCode = `const ${componentName} = (props) => (\n ${svgCode.replace(" (\n ${reactifiedSvg}\n);\n\nexport { ${componentName} };`; } - const ast: Module = await parse(structuredCode, { - syntax: typescript ? "typescript" : "ecmascript", - ...(typescript ? { tsx: true } : { jsx: true }), + const formatted = await prettier.format(structuredCode, { + parser: typescript ? "typescript" : "babel", + semi: true, + singleQuote: false, + trailingComma: "es5", }); - const { code } = await print(ast, { minify }); - return code; + + return formatted; };