watr v5.9.0

Compile WebAssembly text to binary in JavaScript

A wat2wasm implementation in plain JavaScript. Built for compilers, build tools, test suites and playgrounds.

$npm i watr playground
input.wat
output.wasm41 bytes

Spec-complete

Full spec and features. 60,380 official testsuite assertions.

Portable

A regular ES module: no .wasm file, no Emscripten. Bundlers, workers and blob URLs load it like any other import.

Lightweight

44 KB minified, 14 KB gzipped, zero dependencies.

01

Performance

A single walk over the parsed tree emits the bytes: no IR, no runtime, nothing to instantiate.

CompilerSizeThroughput
watr44 KB3,123 op/s
spec/wast.js216 KB1,509 op/s
wabt282 KB859 op/s
binaryen1,100 KB473 op/s
wat-compiler152 KB + wabt355 op/s
02

API

The tagged template compiles, instantiates and imports interpolated values in one step. TypeScript types included.

import watr from 'watr'

const { test } = watr`(func (export "test") (call ${console.log} (i32.const 42)))`
test()  // 42

parse

Text to an S-expression tree.

parse('(i32.const 42)')
// ['i32.const', '42']

print

Tree or text, pretty-printed.

print(src)
// indented, one op per line

minify

Same, with whitespace dropped.

print(src, { indent: false, newline: false })
// (module(func(result i32)(i32.const 42)))

polyfill

Newer features lowered to MVP.

import polyfill from 'watr/polyfill'
compile(polyfill(src))

Options, AST format and the rest of the surface: API reference.

03

Optimizer

Folds constants, treeshakes, inlines, coalesces and outlines.

OptimizerTotal sizeBatchFootprint
watr/optimize19,734 B110 ms in-process149 KB
wasm-opt -Oz19,852 B990 ms via CLI1,100 KB
wasm-opt -O322,302 Bnot measured1,100 KB

21-module corpus: smaller than -O3 on every module, ties or beats -Oz on 18 of 20 files.

Passes and options: optimize docs.

04

Conformance

CI runs the WebAssembly spec testsuite, pinned at 2026-08-12. Feature coverage, including stringref, rounding mode control and wide arithmetic, is tracked on webassembly.org. Errors report line and column.

> compile('(func (export "x"')
Error: Unclosed parenthesis at 1:18

watr compiles WebAssembly text only: C and Rust need LLVM or Emscripten, and decoding .wasm back to text is wabt's wasm2wat.

05

CLI

npx watr input.wat              # input.wasm
npx watr input.wat -o out.wasm  # custom output
npx watr input.wat --print      # pretty-print
npx watr input.wat --minify     # minify
npx watr input.wat --optimize   # fold, treeshake, inline, coalesce
npx watr input.wat --polyfill   # newer features to MVP
06

Used by