Skip to content

Commit 939cd1b

Browse files
authored
chore: update adders to svelte@5 syntax (#69)
1 parent e53e346 commit 939cd1b

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

packages/adders/common.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type AstKinds,
88
type AstTypes
99
} from '@svelte-cli/core/js';
10+
import * as html from '@svelte-cli/core/html';
1011
import { Walker, type Question, type FileEditor } from '@svelte-cli/core';
1112
import { parseScript } from '@svelte-cli/core/parsers';
1213

@@ -370,3 +371,21 @@ function isFunctionDeclaration(
370371
): node is AstTypes.FunctionDeclaration {
371372
return node.type === 'FunctionDeclaration' && node.id?.name === funcName;
372373
}
374+
375+
export function addSlot(
376+
jsAst: AstTypes.Program,
377+
htmlAst: html.HtmlDocument,
378+
svelteVersion: string
379+
) {
380+
const slotSyntax =
381+
svelteVersion && (svelteVersion.startsWith('4') || svelteVersion.startsWith('3'));
382+
383+
if (slotSyntax) {
384+
const slot = html.element('slot');
385+
html.appendElement(htmlAst.childNodes, slot);
386+
return;
387+
}
388+
389+
common.addFromString(jsAst, 'let { children } = $props();');
390+
html.addFromRawHtml(htmlAst.childNodes, '{@render children()}');
391+
}

packages/adders/tailwindcss/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineAdder, defineAdderOptions } from '@svelte-cli/core';
22
import { addImports } from '@svelte-cli/core/css';
33
import { array, common, exports, functions, imports, object } from '@svelte-cli/core/js';
44
import { parseCss, parseScript, parseJson, parseSvelte } from '@svelte-cli/core/parsers';
5+
import { addSlot } from '../common.ts';
56

67
export const options = defineAdderOptions({
78
plugins: {
@@ -121,12 +122,19 @@ export default defineAdder({
121122
},
122123
{
123124
name: ({ kit }) => `${kit?.routesDirectory}/+layout.svelte`,
124-
content: ({ content, typescript }) => {
125-
const { script, generateCode } = parseSvelte(content, { typescript });
125+
content: ({ content, typescript, dependencyVersion }) => {
126+
const { script, template, generateCode } = parseSvelte(content, { typescript });
126127
imports.addEmpty(script.ast, '../app.css');
128+
129+
if (content.length === 0) {
130+
const svelteVersion = dependencyVersion('svelte');
131+
if (!svelteVersion) throw new Error('Failed to determine svelte version');
132+
addSlot(script.ast, template.ast, svelteVersion);
133+
}
134+
127135
return generateCode({
128136
script: script.generateCode(),
129-
template: content.length === 0 ? '<slot />' : undefined
137+
template: content.length === 0 ? template.generateCode() : undefined
130138
});
131139
},
132140
condition: ({ kit }) => Boolean(kit)

packages/core/tooling/html/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {
22
type HtmlChildNode,
3+
type HtmlDocument,
34
HtmlElement,
45
HtmlElementType,
56
parseHtml
67
} from '@svelte-cli/ast-tooling';
78

9+
export type { HtmlDocument };
10+
811
export function div(attributes: Record<string, string> = {}): HtmlElement {
912
return element('div', attributes);
1013
}

0 commit comments

Comments
 (0)