Skip to content

tinywasm is a lightweight toolbox designed to streamline the compilation of Go packages to WebAssembly (WASM) using the TinyGo compiler.

License

Notifications You must be signed in to change notification settings

tinywasm/client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinywasm Client

Project Badges

Intelligent WebAssembly compilation manager for Go with automatic project detection and a 3-mode compiler system.

🚀 Features

  • 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 FieldHandler for real-time mode switching.
  • Smart Detection: Automatically identifies WASM projects and compiler requirements.
  • Developer Experience: Auto-configures VS Code with appropriate GOOS/GOARCH settings.
  • Flexible Output: Support for in-memory serving or physical file output.

🛠 Usage

Basic Initialization

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")

Mode Switching (DevTUI Integration)

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)

🏗 Core Concepts

Dual Output Architecture

client handles two types of outputs to optimize the build pipeline:

  1. WASM Binary (via OutputDir): The final WebAssembly file loaded by the browser.
  2. 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.

Serving Strategies

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.

VS Code Integration

On initialization, client can auto-create .vscode/settings.json to ensure gopls correctly recognizes the WASM environment:

{"gopls": {"env": {"GOOS": "js", "GOARCH": "wasm"}}}

⚙️ Configuration

client follows a hybrid configuration approach:

  • Config Struct: 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.

📋 Requirements

  • Go 1.21+
  • TinyGo (optional, required for Medium/Small modes)

About

tinywasm is a lightweight toolbox designed to streamline the compilation of Go packages to WebAssembly (WASM) using the TinyGo compiler.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •