Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 60d10b2

Browse files
committedSep 22, 2020
Fix #115: Add ESModule support
1 parent fe57fdb commit 60d10b2

File tree

6 files changed

+83
-22
lines changed

6 files changed

+83
-22
lines changed
 

‎build.sbt

+17-8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ val testSettings: Seq[Setting[_]] = commonSettings ++ Seq(
6464
scalaJSUseMainModuleInitializer := true
6565
)
6666

67+
val httpTestSettings: Seq[Setting[_]] = testSettings ++ Seq(
68+
jsEnv := {
69+
new SeleniumJSEnv(
70+
jsEnvCapabilities.value,
71+
SeleniumJSEnv.Config()
72+
.withMaterializeInServer("tmp", "http://localhost:8080/tmp/")
73+
)
74+
}
75+
)
76+
6777
// We'll need the name scalajs-env-selenium for the `seleniumJSEnv` project
6878
name := "root"
6979

@@ -127,13 +137,12 @@ lazy val seleniumJSEnvTest: Project = project.
127137
lazy val seleniumJSHttpEnvTest: Project = project.
128138
enablePlugins(ScalaJSPlugin).
129139
enablePlugins(ScalaJSJUnitPlugin).
130-
settings(testSettings).
140+
settings(httpTestSettings)
141+
142+
lazy val seleniumJSHttpESModuleEnvTest: Project = project.
143+
enablePlugins(ScalaJSPlugin).
144+
enablePlugins(ScalaJSJUnitPlugin).
145+
settings(httpTestSettings).
131146
settings(
132-
jsEnv := {
133-
new SeleniumJSEnv(
134-
jsEnvCapabilities.value,
135-
SeleniumJSEnv.Config()
136-
.withMaterializeInServer("tmp", "http://localhost:8080/tmp/")
137-
)
138-
}
147+
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }
139148
)

‎seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/SeleniumRun.scala

+27-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import scala.util.control.NonFatal
1010

1111
import java.util.concurrent.{ConcurrentLinkedQueue, Executors}
1212
import java.util.function.Consumer
13+
import java.nio.file.Path
14+
import java.net.URL
1315

1416
private sealed class SeleniumRun(
1517
driver: WebDriver with JavascriptExecutor,
@@ -129,20 +131,19 @@ private[selenium] object SeleniumRun {
129131
newRun: Ctor[T], failed: Throwable => T): T = {
130132
validator.validate(runConfig)
131133

132-
val scripts = input.map {
133-
case Input.Script(s) => s
134-
case _ => throw new UnsupportedInputException(input)
134+
var scripts: Seq[Path] = Seq.empty
135+
var modules: Seq[Path] = Seq.empty
136+
137+
input.foreach {
138+
case Input.Script(s) => scripts :+= s
139+
case Input.ESModule(s) => modules :+= s
140+
case _ => throw new UnsupportedInputException(input)
135141
}
136142

137143
try {
138144
withCleanup(FileMaterializer(config.materialization))(_.close()) { m =>
139-
val allScriptURLs = (
140-
m.materialize("setup.js", JSSetup.setupCode(enableCom)) +:
141-
scripts.map(m.materialize)
142-
)
143-
144-
val page = m.materialize("scalajsRun.html", htmlPage(allScriptURLs))
145-
145+
val setupJsUrl = m.materialize("setup.js", JSSetup.setupCode(enableCom))
146+
val page = m.materialize("scalajsRun.html", htmlPage(setupJsUrl, input, m))
146147
withCleanup(newDriver())(maybeCleanupDriver(_, config)) { driver =>
147148
driver.navigate().to(page)
148149

@@ -171,13 +172,25 @@ private[selenium] object SeleniumRun {
171172
private def maybeCleanupDriver(d: WebDriver, config: SeleniumJSEnv.Config) =
172173
if (!config.keepAlive) d.close()
173174

174-
private def htmlPage(scripts: Seq[java.net.URL]): String = {
175-
val scriptTags =
176-
scripts.map(path => s"<script src='${path.toString}'></script>")
175+
private def htmlPage(setupJsUrl: URL, input: Seq[Input], materializer: FileMaterializer): String = {
176+
val setupJs = s"<script src='${setupJsUrl.toString}'></script>"
177+
178+
val tags = input.map {
179+
case Input.Script(path) =>
180+
val url = materializer.materialize(path)
181+
s"<script src='${url.toString}'></script>"
182+
case Input.ESModule(path) =>
183+
val url = materializer.materialize(path)
184+
s"<script type='module' src='${url.toString}'></script>"
185+
case _ => throw new UnsupportedInputException(input)
186+
}
187+
188+
val allTags = setupJs +: tags
189+
177190
s"""<html>
178191
| <meta charset="UTF-8">
179192
| <body>
180-
| ${scriptTags.mkString("\n ")}
193+
| ${allTags.mkString("\n ")}
181194
| </body>
182195
|</html>
183196
""".stripMargin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.scalajs.jsenv.selenium
2+
3+
object CamelCase {
4+
def hello(input: String): String = s"Hello ${CamelcaseEsModule(input)}!"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.scalajs.jsenv.selenium
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSImport
5+
6+
@JSImport("https://cdn.skypack.dev/camelcase@^6.0.0", JSImport.Default)
7+
@js.native
8+
object CamelcaseEsModule extends js.Object {
9+
def apply(input: String): String = js.native
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.scalajs.jsenv.selenium
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
class CamelCaseTest {
7+
@Test def CamelCaseTest(): Unit = {
8+
assertEquals("Hello scalaJsSelenium!", CamelCase.hello("scala js selenium"))
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.scalajs.jsenv.selenium
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
import scala.scalajs.js.Dynamic.global
7+
8+
class LocationTest {
9+
10+
@Test def LocationTest(): Unit = {
11+
assertEquals("http:", global.window.location.protocol.toString())
12+
assertEquals("localhost:8080", global.window.location.host.toString())
13+
}
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.