Skip to content

minimal build handler for compiling Go or WebAssembly targets via CLI. Easy to integrate into other toolkits or workflows.

License

Notifications You must be signed in to change notification settings

tinywasm/gobuild

Repository files navigation

gobuild

Project Badges

Thread-safe Go/WASM build handler with sync/async compilation support.

Installation

go get github.com/tinywasm/gobuild

Quick Start

Configuration is documented in config.go.

// See config.go for full configuration options
config := &Config{
    Command:                    "go",
    MainInputFileRelativePath:  "server/main.go",
    OutName:                    "app",
    Extension:                  ".exe",
    OutFolderRelativePath:      "dist",
    Logger:                     func(msg ...any) { fmt.Println(msg...) },
    Timeout:                    5 * time.Second,
    Env:                        []string{"GOOS=js", "GOARCH=wasm"}, // For WASM compilation
}

compiler := gobuild.New(config)
err := compiler.CompileProgram() // Synchronous

Async Compilation

config.Callback = func(err error) {
    if err != nil {
        log.Printf("Failed: %v", err)
    } else {
        log.Printf("Success!")
    }
}
err := compiler.CompileProgram() // Returns immediately

Thread-Safe Control

// Cancel ongoing compilation
compiler.Cancel()

// Check compilation status
if compiler.IsCompiling() {
    fmt.Println("Compilation in progress...")
}

Methods

  • CompileProgram() error - Compile to disk (sync/async based on callback)
  • CompileToMemory() ([]byte, error) - Compile to memory (returns byte slice, sync only)
  • Cancel() error - Cancel current compilation
  • IsCompiling() bool - Check if compilation is active
  • MainOutputFileNameWithExtension() string - Get output filename with extension (e.g., "main.wasm")

In-Memory Compilation

// Compile directly to memory without writing to disk
binary, err := compiler.CompileToMemory()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Compiled binary size: %d bytes\n", len(binary))

Features

  • Thread-safe: Automatic cancellation of previous compilations
  • Unique temp files: Prevents conflicts during concurrent builds
  • Context-aware: Proper cancellation and timeout handling
  • In-memory: Compile directly to memory slice without disk I/O

About

minimal build handler for compiling Go or WebAssembly targets via CLI. Easy to integrate into other toolkits or workflows.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages