linkTimeIf
How do you keep a JavaScript implementation and a Wasm component implementation in one artifact?
scala.scalajs.LinkingInfo.linkTimeIf chooses a branch at link time, and the unused branch never be linked.
import scala.scalajs.LinkingInfo.{linkTimeIf, moduleKind, ModuleKind}
linkTimeIf(moduleKind == ModuleKind.WasmComponent) {
wasiPrintln(s)
} {
js.Dynamic.global.console.log(s)
}
linkTimeIf(cond)(thenBranch)(elseBranch) has the same shape as if. cond may only use
see https://www.scala-js.org/news/2025/09/06/announcing-scalajs-1.20.1/ for more details.
For example, from java.lang.Float in scala-wasm. MinimalWasmModule and WasmComponent parse without js.Dynamic.global.parseFloat, and every other module kind uses it.
import scala.scalajs.LinkingInfo.{linkTimeIf, moduleKind}
import scala.scalajs.LinkingInfo.ModuleKind.{MinimalWasmModule, WasmComponent}
linkTimeIf[scala.Float](moduleKind == MinimalWasmModule || moduleKind == WasmComponent) {
parseFloatDecimalWasm(fullNumberStr, integralPartStr, fractionalPartStr, exponentStr)
} {
parseFloatDecimalJS(fullNumberStr, integralPartStr, fractionalPartStr, exponentStr)
}