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 is a port of b744d12e0c6d8af74960e0ce5071c8e0011249a5.
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.
A new `Input.ESModulesToLoad` instructs a JSEnv to load files as
ES modules.
The Node.js-based environment, however, will only *actually*
interpret the files as ES modules if their name ends with `.mjs`.
This happens because of how Node.js itself identifies ES modules
as of version 10.x, although it is still experimental, so that
could change in the future. `.js` files will be loaded as
CommonJS modules instead.
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.
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