Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add Svelte 5 runes compiler support #347

Merged
merged 9 commits into from
May 1, 2024
19 changes: 16 additions & 3 deletions src/core/compilers/svelte.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import type { Compiler } from './types'

export const SvelteCompiler = ((svg: string) => {
let svelteRunes: boolean | undefined

export const SvelteCompiler = (async (svg: string) => {
if (typeof svelteRunes === 'undefined') {
try {
// @ts-expect-error we don't have svelte as dependency
const { VERSION } = await import('svelte/compiler')
svelteRunes = Number(VERSION.split('.')[0]) >= 5
}
catch {
svelteRunes = false
}
}
const openTagEnd = svg.indexOf('>', svg.indexOf('<svg '))
const closeTagStart = svg.lastIndexOf('</svg')
const openTag = `${svg.slice(0, openTagEnd)} {...$$props}>`
const openTag = `${svg.slice(0, openTagEnd)} {...${svelteRunes ? 'p' : '$$props'}}>`
const content = `{@html \`${escapeSvelte(svg.slice(openTagEnd + 1, closeTagStart))}\`}`
const closeTag = svg.slice(closeTagStart)
return `${openTag}${content}${closeTag}`
const sfc = `${openTag}${content}${closeTag}`
return svelteRunes ? `<script>const{...p}=$props()</script>${sfc}` : sfc
}) as Compiler

// escape curlies, backtick, \t, \r, \n to avoid breaking output of {@html `here`} in .svelte
Expand Down