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
This commit is contained in:
parent
31142411b0
commit
40a45e3ef6
2 changed files with 9 additions and 6 deletions
5
.changeset/lovely-lions-attend.md
Normal file
5
.changeset/lovely-lions-attend.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix optional props not being recognized properly in the editor
|
|
@ -7,13 +7,11 @@ export function toTSX(code: string, className: string): string {
|
||||||
`;
|
`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let tsx = svelte2tsx(code).code;
|
let tsx = svelte2tsx(code, { mode: 'ts' }).code;
|
||||||
tsx = 'let Props = render().props;\n' + tsx;
|
tsx = '/// <reference types="svelte2tsx/svelte-shims" />\n' + tsx;
|
||||||
|
|
||||||
// Replace Svelte's class export with a function export
|
|
||||||
result = tsx.replace(
|
result = tsx.replace(
|
||||||
/^export default[\S\s]*/gm,
|
'export default class extends __sveltets_1_createSvelte2TsxComponent(',
|
||||||
`export default function ${className}__AstroComponent_(_props: typeof Props): any {}`
|
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `
|
||||||
);
|
);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in a new issue