mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
✨ Create custom utility to parse SVG code with SWC
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
import { parse, print, type Module } from "@swc/core";
|
||||||
|
|
||||||
|
interface ParseReactSvgOptions {
|
||||||
|
componentName: string;
|
||||||
|
svgCode: string;
|
||||||
|
typescript: boolean;
|
||||||
|
minify?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const parseReactSvgContent = async ({
|
||||||
|
componentName,
|
||||||
|
svgCode,
|
||||||
|
typescript,
|
||||||
|
minify = false,
|
||||||
|
}: ParseReactSvgOptions) => {
|
||||||
|
let structuredCode = "";
|
||||||
|
|
||||||
|
if (typescript) {
|
||||||
|
structuredCode =
|
||||||
|
`import * as React from 'react';\n\n` +
|
||||||
|
`const ${componentName} = (props: React.SVGProps<SVGSVGElement>) => (\n ${svgCode.replace("<svg", "<svg {...props}")}\n);\n\n` +
|
||||||
|
`export { ${componentName} };`;
|
||||||
|
} else {
|
||||||
|
structuredCode = `const ${componentName} = (props) => (\n ${svgCode.replace("<svg", "<svg {...props}")}\n);\n\nexport { ${componentName} };`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ast: Module = await parse(structuredCode, {
|
||||||
|
syntax: typescript ? "typescript" : "ecmascript",
|
||||||
|
...(typescript ? { tsx: true } : { jsx: true }),
|
||||||
|
});
|
||||||
|
const { code } = await print(ast, { minify });
|
||||||
|
return code;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user