vugu / vugu

Vugu: A modern UI library for Go+WebAssembly (experimental)
https://www.vugu.org
MIT License
4.8k stars 175 forks source link

Feedback Request: Do you use the generated `main_wasm.go`? #265

Open owenwaller opened 5 months ago

owenwaller commented 5 months ago

Reason

@bradleypeabody and myself are seeking feedback from the community in relation to the main_wasm.go file. We are considering marking this file as autogenerated in the future and recommending that users do not edit it.

Currently, unless you run vugugen with the -skip-main flag vugugen will generate a file called main_wasm.go that is the main function for your vugu application.

We need to know how the community is using this file (if at all), especially in production environments. So if you are;

Can you please feedback in this issue?

At present we have no idea which category the majority of vugu users fall into so we are trying to determine this as a first step.

A simple "We use the file as is" or something similar is all we need. If you fall into either of the last two options, can you give us an idea why you needed to edit the file or create a new one?

The main_wasm.go

At present if the logic works as follows:

If the `-skip-main` argument is not supplied 
then 
    if the `main_wasm.go` file does not exist
    then
        generate the `main_wasm.go` file
   end if
end if 

The current logic ensures that main_wasm.go is only generated once, so any edits to this file are not lost.

The contents are currently:

//go:build wasm
// +build wasm

package main

import (
    "fmt"

    "flag"

    "github.com/vugu/vugu"
    "github.com/vugu/vugu/domrender"
)

func main() {

    mountPoint := flag.String("mount-point", "#vugu_mount_point", "The query selector for the mount point for the root component, if it is not a full HTML component")
    flag.Parse()

    fmt.Printf("Entering main(), -mount-point=%q\n", *mountPoint)
    defer fmt.Printf("Exiting main()\n")

    renderer, err := domrender.New(*mountPoint)
    if err != nil {
        panic(err)
    }
    defer renderer.Release()

    buildEnv, err := vugu.NewBuildEnv(renderer.EventEnv())
    if err != nil {
        panic(err)
    }

    rootBuilder := &Root{}

    for ok := true; ok; ok = renderer.EventWait() {

        buildResults := buildEnv.RunBuild(rootBuilder)

        err = renderer.Render(buildResults)
        if err != nil {
            panic(err)
        }
    }

}

Possible Options

From the perspective of a developer using vugu to build a web ui the options are:

  1. Do nothing, and preserve the current behaviour
  2. Mark the file as autogenerated and recommend it is regenerated as part of the build process
  3. Add an new init command to generate a version of main_wasm.go for you. This allows us to pull out this functionality from the rest of vugu.
  4. Do not generate the main_wasm.go file. Instead document what you need need to do to create one along with examples.

For the first three options we are also considering exposing the template used to generate the 'main_wasm.go. At present the template is internal to thevuggen` tool. This would allow users to customise the template to suit their needs.

Scapal commented 1 month ago

Autogenerated version is fine for me, I usually only change 1 line in this file: rootBuilder := vuguSetup(buildEnv, renderer.EventEnv())

You could even provide a setup.go skeleton with

func vuguSetup(buildEnv *vugu.BuildEnv, eventEnv vugu.EventEnv) vugu.Builder {
   return &page.Root{}
}
owenwaller commented 1 month ago

@Scapal

Thanks for this, but can you be clear exactly which line you are taking about always having to change with a diff against the generated main_wasm.go?

This line: rootBuilder := &Root{} Is doing more than it appears, specifically the Root is name of the struct that the is generated by vugugen based on the filename of the .vugu file.

This means that the current main_wasm.go has an implicit naming convention in it. This is not obvious if you don't know vugu's internals.

[Aside: I would like to break this convention so that when you run vugugen you can pick a name for the root "object", and change any generated main_wasm.go as required. But that's a not going to happen soon, as we are busy on other areas.]

Thanks

Scapal commented 1 month ago

HI @owenwaller ,

Yes it is exactly that line. I call a vuguSetup() function to setup the routing, the wiring and the authentication. Oh, in fact I don't even change the main_wasm.go, vugugen is detecting that the function exists and adapt the main_wasm.go accordingly:

If your main package includes a function called vuguSetup, then when vugugen creates your main_wasm.go file it will include a call to that function. https://www.vugu.org/doc/wiring