Documentation
¶
Overview ¶
Package sasso is a pure-Go (no cgo) binding to the sasso SCSS-to-CSS compiler.
It embeds the sasso WebAssembly module and runs it with the wazero runtime, so it has no native dependencies, requires no C toolchain, and builds with CGO_ENABLED=0. That makes the resulting binaries fully static and trivial to cross-compile to any target Go and wazero support.
Usage ¶
css, err := sasso.Compile(".a { .b { color: #336699 } }")
if err != nil {
log.Fatal(err)
}
fmt.Print(css)
Choose the output style with WithStyle:
css, _ := sasso.Compile(src, sasso.WithStyle(sasso.Compressed))
A Sass error (bad syntax, undefined variable, ...) is reported as an *Error.
Scope ¶
This binding is self-contained string-in / CSS-out: it compiles a single SCSS source string into CSS. The embedded wasm module is freestanding and has no filesystem access, so @use / @import that load other files and custom importers are NOT supported here. If you need file-based loading or a custom importer, use the sasso C ABI (cgo) or a binding built on it (for example sasso-python / sasso-ruby). For most "compile this stylesheet" use cases the wasm path is all you need.
Concurrency ¶
A single shared wasm instance backs the package and is guarded by a mutex, so Compile is safe to call from multiple goroutines, but calls are serialized.
Versioning ¶
sasso-go has its own version (see Version) that floats independently and pins a specific sasso core (see CompilerVersion), which is the version of the bundled wasm module.
Index ¶
Constants ¶
const CompilerVersion = "0.6.1"
CompilerVersion is the version of the bundled sasso core (the wasm module). The wasm module does not export a version string, so this is tracked here and updated whenever the embedded sasso.wasm is refreshed.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
Compile compiles an SCSS source string and returns the resulting CSS.
On a Sass error the returned error is an *Error carrying the compiler's diagnostic message.
func UseCompilationCache ¶ added in v0.1.1
func UseCompilationCache(dir string)
UseCompilationCache persists wazero's compiled wasm module to an on-disk cache in dir, so the optimizing compiler's one-time module compilation (~1s, see UseInterpreter) is paid only ONCE, ever — across process restarts. The first run with a given dir populates the cache; every subsequent start reads the cached machine code and is ready in roughly 20ms. The cache is keyed by the wazero version and the module's content, so stale entries are ignored automatically and the dir is safe to share across sasso-go versions.
Call it once before the first Compile (the engine is built once); calling it afterward has no effect. An empty dir disables the cache (the default). A dir that can't be created surfaces as an error from the first Compile. A good choice is a subdirectory of os.UserCacheDir.
func UseInterpreter ¶
func UseInterpreter(b bool)
UseInterpreter selects wazero's interpreter engine instead of its default optimizing compiler. Call it once before the first Compile; calling it after the engine is built has no effect.
Trade-off: the optimizing compiler (default) compiles the embedded wasm to machine code, which costs roughly a second of one-time startup but then runs each Compile in tens of microseconds. The interpreter starts about ten times faster but runs each Compile a couple of orders of magnitude slower. Prefer the interpreter for short-lived processes (a CLI compiling a few files); prefer the default for servers or batch jobs that compile many times.
Types ¶
type Error ¶
type Error struct {
Message string
}
Error is returned when the Sass compiler reports an error (syntax error, undefined variable, and so on). Its Message is the compiler's diagnostic.
type Result ¶
Result bundles the CSS and the source map produced by CompileWithSourceMap.
func CompileWithSourceMap ¶
CompileWithSourceMap compiles SCSS and also returns a source map (JSON).
The returned Result.CSS is the CSS and Result.SourceMap is the source-map JSON. The source map includes the original source content. On a Sass error the returned error is an *Error.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
demo
command
Command demo is a tiny consumer of the sasso-go package: it compiles an SCSS snippet, prints the CSS, and (when a dart-sass binary is available) benchmarks in-process compilation against spawning dart-sass once per compile.
|
Command demo is a tiny consumer of the sasso-go package: it compiles an SCSS snippet, prints the CSS, and (when a dart-sass binary is available) benchmarks in-process compilation against spawning dart-sass once per compile. |