Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Configuration

scala-wasm adds WIT binding generation and Wasm component linking on top of sbt-scalajs.

project/build.sbt

WasmtimeEnv from wasmtime-env runs linked components with wasmtime when you sbt run or sbt test. Add the following to project/build.sbt:

libraryDependencies +=
  "io.github.scala-wasm" %% "scalajs-env-wasmtime" % "0.1.1"

build.sbt

import org.scalajs.ir.WitScope
import org.scalajs.jsenv.wasmtime.WasmtimeEnv
import org.scalajs.linker.interface.ESVersion
import org.scalajs.linker.interface.WasmComponentModuleInitializerExport
import org.scalajs.linker.interface.WasmComponentModuleInitializerExport._

lazy val root = project
  .in(file("."))
  .enablePlugins(ScalaJSPlugin)
  .settings(
    scalaJSLinkerConfig ~= {
      _.withESFeatures(_.withUseWebAssembly(true).withESVersion(ESVersion.ES2022))
        .withModuleKind(ModuleKind.WasmComponent) // emit Wasm Component
        .withWasmFeatures(_.withModuleInitializerExport(Some(
          // (optional) Export main as wasi:cli/run (WASI entrypoint)
          WasmComponentModuleInitializerExport(
            scope = WitScope.Interface("wasi", "cli", "run", Some("0.2.0")),
            functionName = "run",
            resultType = ResultType.ResultUnitUnit,
          ))))
    },

    // (optional) `wit` directory
    // If `world.wit` exists, scala-wasm automatically generate
    // WIT bindings using `wit-bindgen-scala`.
    scalaJSWitDirectory := baseDirectory.value / "wit",
    scalaJSWitWorld := Some("command"),
    scalaJSWitPackage := Some("example"),

    // (optional) if you wanna export main as endpoint
    scalaJSUseMainModuleInitializer := true,

    // so that we can run/test on wasmtime.
    jsEnv := new WasmtimeEnv(),
  )

WasmtimeEnv needs wasmtime on PATH. Component tests that use the Scala.js test RPC bridge also need wac — see scalajs-env-wasmtime.

Example: Hello world (WASI).