mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
24 lines
574 B
TypeScript
24 lines
574 B
TypeScript
|
import { svelte2tsx } from 'svelte2tsx';
|
||
|
|
||
|
export function toTSX(code: string, className: string): string {
|
||
|
let result = `
|
||
|
let ${className}__AstroComponent_: Error
|
||
|
export default ${className}__AstroComponent_
|
||
|
`;
|
||
|
|
||
|
try {
|
||
|
let tsx = svelte2tsx(code).code;
|
||
|
tsx = 'let Props = render().props;\n' + tsx;
|
||
|
|
||
|
// Replace Svelte's class export with a function export
|
||
|
result = tsx.replace(
|
||
|
/^export default[\S\s]*/gm,
|
||
|
`export default function ${className}__AstroComponent_(_props: typeof Props): any {}`
|
||
|
);
|
||
|
} catch (e: any) {
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|