Skip to content

Add a --js-emit-wasm option and a corresponding using directive #3255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ final case class ScalaJsOptions(
@HelpMessage("Enable jsdom")
jsDom: Option[Boolean] = None,

@Group(HelpGroup.ScalaJs.toString)
@Tag(tags.experimental)
@HelpMessage("Emit WASM")
jsEmitWasm: Option[Boolean] = None,

@Group(HelpGroup.ScalaJs.toString)
@Tag(tags.should)
@HelpMessage("A header that will be added at the top of generated .js files")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ final case class SharedOptions(
smallModuleForPackage = jsSmallModuleForPackage,
esVersionStr = jsEsVersion,
noOpt = jsNoOpt,
remapEsModuleImportMap = jsEsModuleImportMap.filter(_.trim.nonEmpty).map(os.Path(_, Os.pwd))
remapEsModuleImportMap = jsEsModuleImportMap.filter(_.trim.nonEmpty).map(os.Path(_, Os.pwd)),
jsEmitWasm = jsEmitWasm.getOrElse(false)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import scala.util.Try
|`//> using jsModuleSplitStyleStr` _value_
|
|`//> using jsEsVersionStr` _value_
|
|`//> using jsEmitWasm` _true|false_
|
|`//> using jsEsModuleImportMap` _value_
|""".stripMargin
Expand All @@ -65,7 +67,8 @@ final case class ScalaJs(
jsAvoidClasses: Option[Boolean] = None,
jsAvoidLetsAndConsts: Option[Boolean] = None,
jsModuleSplitStyleStr: Option[String] = None,
jsEsVersionStr: Option[String] = None
jsEsVersionStr: Option[String] = None,
jsEmitWasm: Option[Boolean] = None
) extends HasBuildOptions {
// format: on
def buildOptions: Either[BuildException, BuildOptions] =
Expand All @@ -83,7 +86,8 @@ final case class ScalaJs(
avoidLetsAndConsts = jsAvoidLetsAndConsts,
moduleSplitStyleStr = jsModuleSplitStyleStr,
esVersionStr = jsEsVersionStr,
noOpt = jsNoOpt
noOpt = jsNoOpt,
jsEmitWasm = jsEmitWasm.getOrElse(false)
)

def absFilePath(pathStr: String): Either[ImportMapNotFound, Path] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,43 @@ trait RunScalaJsTestDefinitions { _: RunTestDefinitions =>
}
}

test("Emit Wasm") {
val outDir = "out"

val inputs = TestInputs(
os.rel / "run.scala" ->
s"""//> using jsEmitWasm true
|//> using jsModuleKind es
|//> using jsModuleSplitStyleStr fewestmodules
|
|object Foo {
| def main(args: Array[String]): Unit = {
| println("Hello")
| }
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val absOutDir = root / outDir

os.proc(
TestUtil.cli,
"--power",
"package",
"run.scala",
"--js",
"-o",
absOutDir.toString(),
"-f",
extraOptions
)
.call(cwd = root).out.trim()
expect(os.exists(absOutDir / "main.wasm"))

// TODO : Run WASM using node. Requires node 22.
}
}

test("remap imports directive") {
val importmapFile = "importmap.json"
val outDir = "out"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ final case class ScalaJsLinkerConfig(
jsHeader: Option[String] = None,
prettyPrint: Boolean = false,
relativizeSourceMapBase: Option[String] = None,
remapEsModuleImportMap: Option[os.Path] = None
remapEsModuleImportMap: Option[os.Path] = None,
emitWasm: Boolean = false
) {
def linkerCliArgs: Seq[String] = {
val moduleKindArgs = Seq("--moduleKind", moduleKind)
Expand All @@ -34,6 +35,7 @@ final case class ScalaJsLinkerConfig(
val jsEsModuleImportMap = if (remapEsModuleImportMap.nonEmpty)
Seq("--importmap", remapEsModuleImportMap.getOrElse(os.pwd / "importmap.json").toString)
else Nil
val jsEmitWasm = if (emitWasm) Seq("--emitWasm") else Nil

val configArgs = Seq[os.Shellable](
moduleKindArgs,
Expand All @@ -45,7 +47,8 @@ final case class ScalaJsLinkerConfig(
relativizeSourceMapBaseArgs,
jsHeaderArg,
prettyPrintArgs,
jsEsModuleImportMap
jsEsModuleImportMap,
jsEmitWasm
)

configArgs.flatMap(_.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ final case class ScalaJsOptions(
moduleSplitStyleStr: Option[String] = None,
smallModuleForPackage: List[String] = Nil,
esVersionStr: Option[String] = None,
noOpt: Option[Boolean] = None
noOpt: Option[Boolean] = None,
jsEmitWasm: Boolean = false
) {
def fullOpt: Either[UnrecognizedJsOptModeError, Boolean] =
if (mode.isValid)
Expand Down Expand Up @@ -150,7 +151,8 @@ final case class ScalaJsOptions(
smallModuleForPackage = smallModuleForPackage,
esFeatures = esFeatures,
jsHeader = header,
remapEsModuleImportMap = remapEsModuleImportMap
remapEsModuleImportMap = remapEsModuleImportMap,
emitWasm = jsEmitWasm
)
}
}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,10 @@ A file relative to the root directory containing import maps for ES module impor

Enable jsdom

### `--js-emit-wasm`

Emit WASM

### `--js-header`

A header that will be added at the top of generated .js files
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ Add Scala.js options
`//> using jsModuleSplitStyleStr` _value_

`//> using jsEsVersionStr` _value_

`//> using jsEmitWasm` _true|false_

`//> using jsEsModuleImportMap` _value_

Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/scala-command/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ Add Scala.js options
`//> using jsModuleSplitStyleStr` _value_

`//> using jsEsVersionStr` _value_

`//> using jsEmitWasm` _true|false_

`//> using jsEsModuleImportMap` _value_

Expand Down
Loading