Compile and render string as svelte server-side component #12043
-
Is there a way to compile string to svelte Component class? Imagine we have component code in database as string, network fetch or read local file as string:
and we need to parse it as AST and render like this comment: sveltejs/svelte#9377 (comment) with Is there a solution how to create SSR component from string? React have similar parsers to JSX from string: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Something like this? import { compile } from 'svelte/compiler';
const result = compile('<div class="container">Hello world</div><style></style>', { generate: 'ssr' }).render(); |
Beta Was this translation helpful? Give feedback.
Dug into it a bit more: compiling the JS code for the Svelte component and writing it to disk works. Then, we dynamically import the module on disk, call
render
, and get the html and css. Hope this helps!