svelte(lang='ts'
) template to react component converter (PoC)
$ npm install svelte2tsx-component -D
# default css generator is @emotion/css
$ npm install react react-dom @types/react @types/react-dom @emotion/css -D
script lang="ts"
, leaving TypeScript type information.svelte
transparently as React Componentimport { svelteToTsx } from "svelte2tsx-component";
const code = "<div></div>";
const tsxCode = svelteToTsx(code);
// vite.config.ts
import { defineConfig } from "vite";
import { plugin as svelteToTsx } from "svelte-to-tsx";
import ts from "typescript";
export default defineConfig({
plugins: [svelteToTsx({
extensions: [".svelte"],
tsCompilerOptions: {
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ESNext,
jsx: ts.JsxEmit.ReactJSX,
}
})],
});
svelte template
<script lang="ts">
import { onMount } from "svelte";
export let foo: number;
export let bar: number = 1;
const x: number = 1;
let mut = 2;
onMount(() => {
console.log("mounted");
mut = 4;
});
const onClick = () => {
console.log("clicked");
mut = mut + 1;
}
</script>
<div id="x" class="red">
<h1>Nest</h1>
hello, {x}
</div>
<button on:click={onClick}>click</button>
<style>
.red {
color: red;
}
</style>
to tsx component (react
)
import { useEffect, useState } from "react";
import { css } from "@emotion/css";
export default ({ foo, bar = 1 }: { foo: number; bar?: number }) => {
const x: number = 1;
const [mut, set$mut] = useState(2);
useEffect(() => {
console.log("mounted");
set$mut(4);
}, []);
const onClick = () => {
console.log("clicked");
set$mut(mut + 1);
};
return (
<>
<div id="x" className={selector$red}>
<h1>Nest</h1>
hello, {x}
</div>
<button onClick={onClick}>click</button>
</>
);
};
const selector$red = css`
color: red;
`;
So you can use like this.
import React from "react";
import App from "./App.svelte";
import { createRoot } from "react-dom/client";
const root = document.getElementById("root")!;
createRoot(root).render(<App
name="svelte-app"
onMessage={(data) => {
console.log("message received", data)
}
} />);
(put App.svelte.d.ts
manually yet)
svelte
<script lang="ts">
export let foo: number;
export let bar: number = 1;
</script>
tsx
export default ({ foo, bar = 1 }: { foo: number, bar?: number }) => {
return <></>
}
createEventDispatcher<{}>()
svelte
<script lang="ts">
import {createEventDispatcher} from "svelte";
// Only support ObjectTypeLiteral (TypeReference not supported)
const dispatch = createEventDispatcher<{
message: {
text: string;
};
}>();
const onClick = () => {
dispatch('message', {
text: 'Hello!'
});
}
</script>
<div on:click={onClick}>
hello
</div>
tsx
export default ({
onMessage,
}: {
onMessage?: (data: { text: string }) => void;
}) => {
const onClick = () => {
onMessage?.({
text: "Hello!",
});
};
return (
<>
<div onClick={onClick}>hello</div>
</>
);
};
<div id="myid"></div>
<div id={expr}></div>
<div id="{expr}"></div>
<div id="head{expr}tail"></div>
<div {id}></div>
<div {...params}></div>
svelte
<slot />
tsx
import { type ReactNode } from "react";
export default ({children}, { children: ReactNode }) => {
return <>
{children}
</>;
}
(Named slot not supported)
Convert to react's useEffect
<span class="red">text</span>
<style>
.red: {
color: red;
}
</style>
to
// Auto import with style block
import { css } from "@emotion/css";
// in tsx
<span className={style$red}>text</span>
const selector$red = css`
color: red;
`;
Only support single class selector like .red
.
Not Supported these patterns.
/* selector combination */
.foo .bar {}
.foo > .bar {}
/* element selector */
div {}
/* global selector */
:global(div) {}
style
property with expression<div style="color: {color}"></div>
<div style={obj}></div>
class
property with expression<div class="c1 {v}"></div>
<div class={expr}></div>
<input bind:value />
<svelte:options />
svelte
's setContext
/ getContext
/ tick
/ getAllContexts
svelte/motion
svelte/store
svelte/animation
svelte/transition
svelte/action
<Foo let:prop />
:global()
(Checkboxed item may be supportted latter)
Currently, the scope is not parsed, so unintended variable conflicts may occur.
<script context=module>
export let foo: number
to {foo}: {foo: number}
export let bar: number = 1
to {bar = 1}: {bar?: number}
onMount(() => ...)
=> useEffect(() => ..., [])
onDestroy(() => ...)
=> useEffect(() => { return () => ... }, [])
dispatch('foo', data)
=> onFoo?.(data)
beforeUpdate()
=> useEffect
afterUpdate()
=> useEffect
(omit first change)let x = 1
=> const [x, set$x] = setState(1)
x = 1
=> set$x(1)
;$: added = v + 1;
$: document.title = title
=> useEffect(() => {document.title = title}, [title])
$: { document.title = title }
=> useEffect(() => {document.title = title}, [title])
$: <expr-or-block>
=> useEffect()
<div>1</div>
to <><div>1</div></>
<div id="x"></div>
to <><div id="x"></div></>
<div id={v}></div>
to <><div id={v}></div></>
<div on:click={onClick}></div>
to <div onClick={onClick}></div>
{#if ...}
{:else if ...}
{/else}
{#each items as item}
{#each items as item, idx}
{#key <expr>}
{#each items as item (item.id)}
{id}
{...v}
{@html <expr}
{@debug "message"}
<slot>
<svelte:self>
<svelte:component this={currentSelection.component} foo={bar} />
class
=> className
, on:click
=> onClick
<style>
tag to @emotion/css
import {css} from "..."
importer<div style="...">
to <div style={{}}>
{#await <expr>}
$: ({ name } = person)
<div contenteditable="true" bind:innerHTML={html}>
<img bind:naturalWidth bind:naturalHeight></img>
<div bind:this={element}>
class:name
style:property
use:action
<svelte:window />
<svelte:document />
<svelte:body />
<svelte:element this={expr} />
{@const v = 1}
<div on:click|preventDefault={onClick}></div>
<span bind:prop={}>
<Foo let:xxx>
<Foo on:trigger>
<svelte:fragment>
<slot name="...">
$$slots
.d.ts
(<name>.svelte
with <name>.svelte.d.ts
)Svelte templates are not difficult to edit with only HTML and CSS knowledge, but the modern front-end ecosystem revolves around JSX.
However, the modern front-end ecosystem revolves around JSX, and we think we need a converter that transparently treats Svelte templates as React components. I think so.
(This is my personal opinion).
MIT