You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit adds a third `ModuleKind` for ES modules, namely
`ModuleKind.ESModule`. When emitting an ES module, `@JSImport`s
and `@JSExportTopLevel`s straightforwardly map to ES `import` and
`export` clauses, respectively.
At the moment, imports are always implemented using a namespace
import, then selecting fields inside the namespace. This is
suboptimal because it can prevent advanced DCE across ES modules.
Improving on this is left for future work.
The Node.js-based environment is adapted to interpret files whose
name ends with `.mjs` as ES modules rather than scripts. This
aligns with how Node.js itself identifies ES modules as of version
10.x, although it is still experimental, so that could change in
the future.
For the 0.6.x branch, the `TestAdapter` assumes that it can force
the `JSEnv` to interpret its launcher as an ES module by giving it
the `.mjs` extension as well. This is wrong in general, but there
does not seem to be a good way to deal with this issue. In 1.x,
this will be a non-issue since the `TestAdapter` does not require
any launcher.
Although setting `scalaJSLinkerConfig.moduleKind` to
`ModuleKind.ESModule` is enough for the Scala.js linker to emit a
valid ES module, two additional settings are required to *run* or
*test* using Node.js:
artifactPath in (proj, Compile, fastOptJS) :=
(crossTarget in (proj, Compile)).value / "somename.mjs"
jsEnv := {
new org.scalajs.jsenv.NodeJSEnv(
org.scalajs.jsenv.NODEJSEnv.Config()
.withArguments(List("--experimental-modules"))
)
}
The first setting is necessary to give the `.mjs` extension to the
file produced by Scala.js, which in turn is necessary for Node.js
to accept it as an ES module.
The second setting will be necessary until Node.js declares its
support for ES module as non-experimental.
ES modules are incompatible with a few features, which are all gone
in Scala.js 1.x:
* `EnvironmentInfo.exportsNamespace`: an ES module cannot read its
own exports namespace, short of importing itself (which requires
it to know its file name). The value of `exportsNamespace` is
`undefined` in an ES module.
* Generation of a `main` launcher script by the sbt plugin.
Attempting to use one will throw an exception in the build.
Moreover, the version of the Closure Compiler that we use does not
support ES modules yet, so we deactivate GCC when emitting an ES
module.
At this point, the emission of ES modules can be considered stable,
but the support in `NodeJSEnv` is experimental (since the support
of ES modules in Node.js is itself experimental).
Running the full test suite with ES modules requires Node.js 10.2.0
or later. It has been tested with v10.12.0.
0 commit comments