0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/svelte/src/editor.cts

21 lines
625 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, { mode: 'ts' }).code;
tsx = '/// <reference types="svelte2tsx/svelte-shims" />\n' + tsx;
result = tsx.replace(
'export default class extends __sveltets_2_createSvelte2TsxComponent(',
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `
);
} catch (e: any) {
return result;
}
return result;
}