wesl

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

wesl-go

Transpile WESL shaders to WGSL (WebGPU Shading Language) from Go. The transpiler runs the official wesl JavaScript implementation in the ekuma JavaScript engine (pure Go), so you do not need Node.js at runtime. When you build for WebAssembly (GOOS=js), the transpiler runs in the browser's JavaScript engine via syscall/js instead of ekuma.

Installation

go get github.com/oliverbestmann/wesl-go

Usage

Create a transpiler with New(), provide your shader files, and call Transpile with the name of the entrypoint file:

package main

import (
	"fmt"

	wesl "github.com/oliverbestmann/wesl-go"
)

func main() {
	tr := wesl.New()

	out, err := tr.Transpile("main.wesl", wesl.Options{
		Files: wesl.Files{
			"main.wesl":    "import package::util::scale;\n// ...",
			"util.wesl":    "fn scale(x: f32) -> f32 { return x * 2.0; }",
		},
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(out) // WGSL output
}
Loading files from an fs.FS

FilesOf walks a filesystem and collects every .wesl and .wgsl file. This works with an embed.FS:

//go:embed shaders
var shaders embed.FS

sub, _ := fs.Sub(shaders, "shaders")

files, err := wesl.FilesOf(sub)
if err != nil {
	panic(err)
}

out, err := tr.Transpile("main.wesl", wesl.Options{Files: files})

Options

Transpile takes the entrypoint file name plus an Options struct:

Field Type Description
Files Files (map[string]string) In-memory file system of shader sources available to the transpiler.
Constants map[string]any Constants injected into the transpiler. Each value must be an int, float32, or string.
Conditions map[string]bool Conditions used for @if and similar conditional compilation.
PackageName string Optional package name for the local sources (in addition to the default package::), enabling imports like import mypkg::foo. Must be a valid WGSL identifier.
Libs []Bundle Library bundles available for the link (see below).

Example with constants and conditions:

out, err := tr.Transpile("main.wesl", wesl.Options{
	Files: files,
	Constants: map[string]any{
		"Foobar": 123,
	},
	Conditions: map[string]bool{
		"MOBILE": true,
	},
})

Library bundles

Libraries are supplied as Bundle values via Options.Libs. A bundle groups a set of modules under a package name and may declare transitive Dependencies:

out, err := tr.Transpile("main.wesl", wesl.Options{
	Files: wesl.Files{
		"main.wesl": "import mylib::double;\n// ...",
	},
	Libs: []wesl.Bundle{
		{
			Name:    "mylib",
			Edition: "2026_pre",
			Modules: map[string]string{
				"./lib.wesl": "fn double(x: f32) -> f32 { return x * 2.0; }",
			},
		},
	},
})

Console output

The embedded transpiler may call console.log, console.info, console.warn, and console.error. By default this output is discarded. Stdout is an atomic.Pointer[io.Writer]; store a writer in it to capture the output:

var w io.Writer = os.Stdout
wesl.Stdout.Store(new(w))

Under GOOS=js the output goes to the browser console and Stdout has no effect.

Updating the embedded transpiler

The bundled wesl JavaScript is embedded from wesl.js. To regenerate it, run:

js/prepare-wesl.sh

This installs the pinned dependencies, bundles js/index.js with esbuild, and copies the result to wesl.js.

Notes

  • New() panics if the embedded JavaScript fails to load. Create the transpiler once and reuse it.
  • The transpiler is not safe for concurrent use; create one per goroutine or guard it with a mutex.
  • Under GOOS=js the transpiler loads its JavaScript with new Function. If your page sets a Content-Security-Policy, it must allow 'unsafe-eval'.
  • The implementation may switch to wesl-rs compiled to WebAssembly in the future.

Documentation

Overview

Package wesl embeds the official WESL JavaScript transpiler and runs it in a JavaScript runtime (ekuma, or the host runtime when built for GOOS=js), letting Go code link WESL/WGSL sources into a single WGSL string without a Node.js dependency.

Index

Constants

This section is empty.

Variables

Stdout, if set, receives console output (console.log/info/warn/error) from the embedded transpiler. When nil (the default) that output is discarded. Under GOOS=js the host console is used and Stdout has no effect.

Functions

This section is empty.

Types

type Bundle added in v1.1.0

type Bundle struct {
	// Name is the npm package name sanitized to be a valid WESL identifier
	// (@ removed, / => __, - => _).
	Name string

	// Edition is the WESL edition of the code, e.g. "2026_pre".
	Edition string

	// Modules maps file paths (relative to the package root, e.g. "./lib.wgsl")
	// to WESL/WGSL code strings.
	Modules map[string]string

	// Dependencies are the packages referenced by this package.
	Dependencies []Bundle
}

Bundle is a bundle of WESL/WGSL modules that can be provided to the transpiler as a library.

type Files

type Files map[string]string

Files describe an in-memory file system that contains all files available to the transpiler.

func FilesOf

func FilesOf(fsys fs.FS) (Files, error)

FilesOf walks fsys and collects every .wesl and .wgsl file into a Files map, keyed by path. Other files are ignored. It is a convenience for building the Options.Files input from an embed.FS or on-disk directory.

type Options

type Options struct {
	// Files to present to the wesl transpiler
	Files Files

	// Constants to inject into the transpiler.
	// Value must be either int, float32 or string
	Constants map[string]any

	// Conditions for @if etc
	Conditions map[string]bool

	// PackageName for the local sources (in addition to default "package::").
	// Enables imports like `import mypkg::foo` alongside `import package::foo`.
	// Should be a valid WGSL identifier (use underscores, not hyphens or slashes).
	PackageName string

	// Libs are library bundles available for the link.
	Libs []Bundle
}

Options configures a single Transpile call.

type Transpiler

type Transpiler struct {
	// contains filtered or unexported fields
}

Transpiler links WESL/WGSL sources into WGSL. A Transpiler owns a JavaScript runtime and is not safe for concurrent use; create one per goroutine or guard it with a mutex. It can be reused across many Transpile calls.

func New

func New() *Transpiler

New creates a Transpiler by spinning up a JavaScript runtime and loading the embedded wesl JavaScript. It panics if the embedded source fails to load; since that only happens on a broken build, reuse a single Transpiler rather than creating one per call.

func (*Transpiler) Transpile

func (t *Transpiler) Transpile(entrypoint string, opts Options) (string, error)

Transpile links the module graph rooted at entrypoint (a file name such as "main.wesl") into a single WGSL string, pulling in referenced local files and library bundles. Dead code outside the root is dropped. It returns an error if parsing or linking fails.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL