Cats Effect working with Wasm+JS
by Manav
1 min read
cats-effect implementation, "Hello world" application in Cats Effect.
we have already implemented Hello World Application on wasm for scala.js here. Now before trying to build up to running the Cats Effect test suite on Wasm, let's write cats-effect in Wasm backend.
Whats Next
For a simple IO based hello world application as mentioned on cat-effect github, taking Compile / mainClass:
import cats.effect._
object Main extends IOApp.Simple {
val run = IO.println("Hello, World!")
}
Also introducing:
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % "3.5.7"
),
inside the previously created build.sbt file will make sure fastLinkJS to generate Main.wasm file.
Whats Important
Make sure to use the Wasm backend and set scalaJSLinkerConfig.value .withExperimentalUseWebAssembly(true)
Example:
// Emit ES modules with the Wasm backend
scalaJSLinkerConfig := {
scalaJSLinkerConfig.value
.withExperimentalUseWebAssembly(true) // use the Wasm backend
.withModuleKind(ModuleKind.ESModule) // required by the Wasm backend
},
// Configure Node.js with the required WebAssembly flags
jsEnv := {
import org.scalajs.jsenv.nodejs.NodeJSEnv
val config = NodeJSEnv.Config()
.withArgs(List(
"--experimental-wasm-exnref",
"--experimental-wasm-imported-strings",
"--turboshaft-wasm"
))
new NodeJSEnv(config)
}
take boiler plate from docs.
Output
generate output using the same sbt clean fastLinkJS command and
for output run that wasm file inside your browser, take ref.