Skip to content

Commit

Permalink
Add a --js-emit-wasm option and a corresponding using directive (#…
Browse files Browse the repository at this point in the history
…3255)

* emit wasm

* docs

* .

* fix docs

* Test wasm is emitted

* Remove only for CI

* Following feedback

* update ref doc
  • Loading branch information
Quafadas authored Nov 5, 2024
1 parent 3ad7ade commit 72c23a5
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 7 deletions.
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

0 comments on commit 72c23a5

Please sign in to comment.