Thread-safe Go/WASM build handler with sync/async compilation support.
go get github.com/tinywasm/gobuildConfiguration 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() // Synchronousconfig.Callback = func(err error) {
if err != nil {
log.Printf("Failed: %v", err)
} else {
log.Printf("Success!")
}
}
err := compiler.CompileProgram() // Returns immediately// Cancel ongoing compilation
compiler.Cancel()
// Check compilation status
if compiler.IsCompiling() {
fmt.Println("Compilation in progress...")
}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 compilationIsCompiling() bool- Check if compilation is activeMainOutputFileNameWithExtension() string- Get output filename with extension (e.g., "main.wasm")
// 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))- 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