livebud / bud

The Full-Stack Web Framework for Go
MIT License
5.57k stars 180 forks source link

Rendering HTML from Svelte SSR #352

Closed jimafisk closed 1 year ago

jimafisk commented 1 year ago

Hi @matthewmueller!

I was exploring how I might leverage your V8 / Svelte compilation in Plenti. Your API is really nicely written :ok_hand:

Simple example compiling Svelte files from a "templates" folder to an "output" folder ```go package main import ( "io/ioutil" "os" "path/filepath" "strings" v8 "github.com/livebud/bud/package/js/v8" "github.com/livebud/bud/package/svelte" ) func main() { vm, _ := v8.Load() svelteCompiler, _ := svelte.Load(vm) filepath.Walk("templates", func(path string, info os.FileInfo, err error) error { if !info.IsDir() && filepath.Ext(path) == ".svelte" { fileContents, _ := os.ReadFile(path) destPath := "output" + strings.TrimPrefix(path, "templates") os.MkdirAll(filepath.Dir(destPath), os.ModePerm) DOM, _ := svelteCompiler.DOM(path, fileContents) ioutil.WriteFile(strings.TrimSuffix(destPath, ".svelte")+"_DOM.js", []byte(DOM.JS), os.ModePerm) SSR, _ := svelteCompiler.SSR(path, fileContents) ioutil.WriteFile(strings.TrimSuffix(destPath, ".svelte")+"_SSR.js", []byte(SSR.JS), os.ModePerm) } return nil }) } ```

My question: Is there a way to run Component.render(props) from your svelte package to generate HTML? Thank you!

Related issues:

matthewmueller commented 1 year ago

Hey @jimafisk 🙂

So those functions are for compiling one Svelte file. I'm guessing in Plenti, you'll want to support imports as well. To do that you'll need to wrap those functions as plugins in ESBuild to get one large blob of bundle JS.

You can find that code here: https://github.com/livebud/bud/blob/2cc8d0a7414d5461d67ffa65ab9e22ce48272e86/framework/view/ssr/ssr.go#L55

(The transformer contains the Svelte compiler)

Once you have that blob of JS, you can call v8.Eval on it:https://github.com/livebud/bud/blob/2cc8d0a7414d5461d67ffa65ab9e22ce48272e86/framework/view/ssr/ssr_test.go#L132

That would call Component.render() and return HTML.