Intelligent WebAssembly compilation manager for Go with automatic project detection and a 3-mode compiler system.
- 3-Mode Compiler System:
L(Large): Standard Go compiler for fast development.M(Medium): TinyGo with debug optimizations (-opt=1).S(Small): TinyGo with size optimizations (-opt=z).
- Interactive Integration: Implements DevTUI
FieldHandlerfor real-time mode switching. - Smart Detection: Automatically identifies WASM projects and compiler requirements.
- Developer Experience: Auto-configures VS Code with appropriate
GOOS/GOARCHsettings. - Flexible Output: Support for in-memory serving or physical file output.
client uses a combination of a Config struct for core paths and public setter methods for advanced configuration.
// 1. Create base configuration
cfg := client.NewConfig()
cfg.SourceDir = "web"
cfg.OutputDir = "web/public"
// 2. Initialize client
twc := client.New(cfg)
// 3. Fine-tune configuration via setters
twc.SetAppRootDir("/path/to/project")
twc.SetMainInputFile("app.go") // default: "client.go"
twc.SetOutputName("app") // default: "client"
twc.SetBuildShortcuts("L", "M", "S") // customize mode triggers
// (Optional) Enable physical wasm_exec.js output for file watchers/debug
twc.SetWasmExecJsOutputDir("web/theme/js")Switching modes is asynchronous and reports progress via a channel:
progress := make(chan string)
go func() {
for p := range progress {
fmt.Println("Status:", p)
}
}()
// Switch to Small (Production) mode
twc.Change("S", progress)client handles two types of outputs to optimize the build pipeline:
- WASM Binary (via
OutputDir): The final WebAssembly file loaded by the browser. - JavaScript Runtime: Mode-specific
wasm_exec.js.- Dynamic: Recommended to use
tw.JavascriptForInitializing()to get the content directly for embedding or serving. - Disk-based: Use
tw.SetWasmExecJsOutputDir()if you need a physical file for external bundlers or mode-transparency for other tools.
- Dynamic: Recommended to use
The library automatically selects the best way to compile and serve the WASM binary:
- In-Memory: Compiles directly to a buffer (fastest for development).
- External: Compiles and serves from disk (useful for static integration).
Logic details are available in strategies.go.
On initialization, client can auto-create .vscode/settings.json to ensure gopls correctly recognizes the WASM environment:
{"gopls": {"env": {"GOOS": "js", "GOARCH": "wasm"}}}client follows a hybrid configuration approach:
ConfigStruct: Used for shared dependencies (Store,Logger) and relative directory structures (SourceDir,OutputDir). See config.go.- Public Setters: Used for project-specific overrides (
AppRootDir,MainInputFile,OutputName) and build behavioral changes. These setters are reactive and re-initialize internal state automatically.
- Go 1.21+
- TinyGo (optional, required for Medium/Small modes)