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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/compilers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Compiler, CustomCompiler } from './types'
import { Vue2Compiler } from './vue2'
import { Vue3Compiler } from './vue3'
import { WebComponentsCompiler } from './web-components'
import { SvelteRunesCompiler } from './svelte-runes'

export const compilers: Record<Exclude<ResolvedOptions['compiler'], CustomCompiler>, Compiler> = {
'astro': AstroCompiler,
Expand All @@ -20,6 +21,7 @@ export const compilers: Record<Exclude<ResolvedOptions['compiler'], CustomCompil
'raw': RawCompiler,
'solid': SolidCompiler,
'svelte': SvelteCompiler,
'svelte-runes': SvelteRunesCompiler,
'vue2': Vue2Compiler,
'vue3': Vue3Compiler,
'web-components': WebComponentsCompiler,
Expand Down
4 changes: 4 additions & 0 deletions src/core/compilers/svelte-runes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Compiler } from './types'
import { compileSvelte } from './svelte'

export const SvelteRunesCompiler = ((svg: string) => compileSvelte(svg, true)) as Compiler
11 changes: 7 additions & 4 deletions src/core/compilers/svelte.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Compiler } from './types'

export const SvelteCompiler = ((svg: string) => {
export const SvelteCompiler = ((svg: string) => compileSvelte(svg)) as Compiler

export function compileSvelte(svg: string, runes = 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)} {...${runes ? 'p' : '$$props'}}>`
const content = `{@html \`${escapeSvelte(svg.slice(openTagEnd + 1, closeTagStart))}\`}`
const closeTag = svg.slice(closeTagStart)
return `${openTag}${content}${closeTag}`
}) as Compiler
const sfc = `${openTag}${content}${closeTag}`
return runes ? `<script>const{...p}=$props()</script>${sfc}` : sfc
}

// escape curlies, backtick, \t, \r, \n to avoid breaking output of {@html `here`} in .svelte
export function escapeSvelte(str: string): string {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const unplugin = createUnplugin<Options | undefined>((options = {}) => {
case 'marko':
return `${res}.marko`
case 'svelte':
case 'svelte-runes':
return `${res}.svelte`
case 'solid':
return `${res}.tsx`
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface Options {
*
* @default (detect automatically, fallback to 'vue3')
*/
compiler?: 'astro' | 'jsx' | 'marko' | 'none' | 'solid' | 'svelte' | 'raw' | 'vue2' | 'vue3' | 'web-components' | 'qwik' | CustomCompiler
compiler?: 'astro' | 'jsx' | 'marko' | 'none' | 'solid' | 'svelte' | 'svelte-runes' | 'raw' | 'vue2' | 'vue3' | 'web-components' | 'qwik' | CustomCompiler
userquin marked this conversation as resolved.
Show resolved Hide resolved

/**
* JSX style, works only when compiler set to `jsx`
Expand Down