Commit 2a81c37
committed
Fix scala-js/scala-js#2175: Add support for ECMAScript 2015 modules.
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.1 parent 802518a commit 2a81c37
File tree
2 files changed
+51
-1
lines changed- js-envs/src/main/scala/org/scalajs/jsenv
- nodejs-env/src/main/scala/org/scalajs/jsenv/nodejs
2 files changed
+51
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
32 | 42 | | |
33 | 43 | | |
34 | 44 | | |
| |||
Lines changed: 41 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
164 | 165 | | |
165 | 166 | | |
166 | 167 | | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
167 | 196 | | |
168 | 197 | | |
169 | 198 | | |
| |||
192 | 221 | | |
193 | 222 | | |
194 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
195 | 235 | | |
196 | 236 | | |
197 | 237 | | |
| |||
0 commit comments