astro/packages/integrations/svelte/src/editor.cts
Erika 40a45e3ef6
Properly support and type optional props in Svelte components (#3993)
* Properly support and type optional props in Svelte components

* Change output to support documentating components

* Add changeset
2022-07-20 17:32:46 -04:00

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_1_createSvelte2TsxComponent(',
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `
);
} catch (e: any) {
return result;
}
return result;
}