Skip to content

Commit bb39519

Browse files
authoredSep 27, 2020
Merge pull request #116 from mushtaq/esmodules
Fix #115: Add ESModule support
2 parents fe57fdb + f964525 commit bb39519

File tree

6 files changed

+59
-21
lines changed

6 files changed

+59
-21
lines changed
 

‎build.sbt

+5-3
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ lazy val seleniumJSEnv: Project = project.
7878
* guava stuff which in turn makes selenium fail.
7979
*/
8080
"org.seleniumhq.selenium" % "selenium-server" % "3.141.59",
81-
"org.scala-js" %% "scalajs-js-envs" % scalaJSVersion,
82-
"org.scala-js" %% "scalajs-js-envs-test-kit" % scalaJSVersion % "test",
81+
"org.scala-js" %% "scalajs-js-envs" % "1.1.1",
82+
"com.google.jimfs" % "jimfs" % "1.1",
83+
"org.scala-js" %% "scalajs-js-envs-test-kit" % "1.1.1" % "test",
8384
"com.novocode" % "junit-interface" % "0.11" % "test"
8485
),
8586

@@ -135,5 +136,6 @@ lazy val seleniumJSHttpEnvTest: Project = project.
135136
SeleniumJSEnv.Config()
136137
.withMaterializeInServer("tmp", "http://localhost:8080/tmp/")
137138
)
138-
}
139+
},
140+
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }
139141
)

‎project/build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0")
1+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.2.0")
22

33
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.18")
44

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package org.scalajs.jsenv.selenium
22

3+
import java.nio.charset.StandardCharsets
4+
import java.nio.file.{Files, Path}
5+
6+
import com.google.common.jimfs.Jimfs
7+
38
private[selenium] object JSSetup {
4-
def setupCode(enableCom: Boolean): String = {
9+
def setupFile(enableCom: Boolean): Path = {
10+
val path = Jimfs.newFileSystem().getPath("setup.js")
11+
val contents = setupCode(enableCom).getBytes(StandardCharsets.UTF_8)
12+
Files.write(path, contents)
13+
}
14+
15+
private def setupCode(enableCom: Boolean): String = {
516
s"""
617
|(function() {
718
| // Buffers for console.log / console.error
@@ -77,4 +88,5 @@ private[selenium] object JSSetup {
7788
|}).call(this)
7889
""".stripMargin
7990
}
91+
8092
}

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

+18-16
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,11 @@ 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)
135-
}
136-
137134
try {
138135
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-
136+
val setupJsScript = Input.Script(JSSetup.setupFile(enableCom))
137+
val fullInput = setupJsScript +: input
138+
val page = m.materialize("scalajsRun.html", htmlPage(fullInput, m))
146139
withCleanup(newDriver())(maybeCleanupDriver(_, config)) { driver =>
147140
driver.navigate().to(page)
148141

@@ -171,18 +164,27 @@ private[selenium] object SeleniumRun {
171164
private def maybeCleanupDriver(d: WebDriver, config: SeleniumJSEnv.Config) =
172165
if (!config.keepAlive) d.close()
173166

174-
private def htmlPage(scripts: Seq[java.net.URL]): String = {
175-
val scriptTags =
176-
scripts.map(path => s"<script src='${path.toString}'></script>")
167+
private def htmlPage(fullInput: Seq[Input], materializer: FileMaterializer): String = {
168+
val tags = fullInput.map {
169+
case Input.Script(path) => makeTag(path, "text/javascript", materializer)
170+
case Input.ESModule(path) => makeTag(path, "module", materializer)
171+
case _ => throw new UnsupportedInputException(fullInput)
172+
}
173+
177174
s"""<html>
178175
| <meta charset="UTF-8">
179176
| <body>
180-
| ${scriptTags.mkString("\n ")}
177+
| ${tags.mkString("\n ")}
181178
| </body>
182179
|</html>
183180
""".stripMargin
184181
}
185182

183+
private def makeTag(path: Path, tpe: String, materializer: FileMaterializer): String = {
184+
val url = materializer.materialize(path)
185+
s"<script defer type='$tpe' src='$url'></script>"
186+
}
187+
186188
private class WindowOnErrorException(errs: List[String]) extends Exception(s"JS error: $errs")
187189

188190
private def consumer[A](f: A => Unit): Consumer[A] = new Consumer[A] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.scalajs.jsenv.selenium
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSImport
5+
6+
object CamelCase {
7+
def hello(input: String): String = s"Hello ${camelCase(input)}!"
8+
9+
@JSImport("https://cdn.skypack.dev/camelcase@^6.0.0", JSImport.Default)
10+
@js.native
11+
def camelCase(input: String): String = js.native
12+
}
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 ModuleSupportTest {
7+
@Test def testBasicImport(): Unit = {
8+
assertEquals("Hello scalaJsSelenium!", CamelCase.hello("scala js selenium"))
9+
}
10+
}

0 commit comments

Comments
 (0)