Skip to content

Commit

Permalink
Fix scala-js#115: Add ESModule support
Browse files Browse the repository at this point in the history
  • Loading branch information
mushtaq committed Sep 27, 2020
1 parent 4ac4518 commit ddb7615
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ lazy val seleniumJSHttpEnvTest: Project = project.
SeleniumJSEnv.Config()
.withMaterializeInServer("tmp", "http://localhost:8080/tmp/")
)
}
},
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import scala.util.control.NonFatal

import java.util.concurrent.{ConcurrentLinkedQueue, Executors}
import java.util.function.Consumer
import java.nio.file.Path
import java.net.URL

private sealed class SeleniumRun(
driver: WebDriver with JavascriptExecutor,
Expand Down Expand Up @@ -129,20 +131,10 @@ private[selenium] object SeleniumRun {
newRun: Ctor[T], failed: Throwable => T): T = {
validator.validate(runConfig)

val scripts = input.map {
case Input.Script(s) => s
case _ => throw new UnsupportedInputException(input)
}

try {
withCleanup(FileMaterializer(config.materialization))(_.close()) { m =>
val allScriptURLs = (
m.materialize("setup.js", JSSetup.setupCode(enableCom)) +:
scripts.map(m.materialize)
)

val page = m.materialize("scalajsRun.html", htmlPage(allScriptURLs))

val setupJsUrl = m.materialize("setup.js", JSSetup.setupCode(enableCom))
val page = m.materialize("scalajsRun.html", htmlPage(setupJsUrl, input, m))
withCleanup(newDriver())(maybeCleanupDriver(_, config)) { driver =>
driver.navigate().to(page)

Expand Down Expand Up @@ -171,18 +163,31 @@ private[selenium] object SeleniumRun {
private def maybeCleanupDriver(d: WebDriver, config: SeleniumJSEnv.Config) =
if (!config.keepAlive) d.close()

private def htmlPage(scripts: Seq[java.net.URL]): String = {
val scriptTags =
scripts.map(path => s"<script src='${path.toString}'></script>")
private def htmlPage(setupJsUrl: URL, input: Seq[Input], materializer: FileMaterializer): String = {
val setupJs = s"<script src='${setupJsUrl.toString}'></script>"

val tags = input.map {
case Input.Script(path) => makeTag(path, "text/javascript", materializer)
case Input.ESModule(path) => makeTag(path, "module", materializer)
case _ => throw new UnsupportedInputException(input)
}

val allTags = setupJs +: tags

s"""<html>
| <meta charset="UTF-8">
| <body>
| ${scriptTags.mkString("\n ")}
| ${allTags.mkString("\n ")}
| </body>
|</html>
""".stripMargin
}

private def makeTag(path: Path, tpe: String, materializer: FileMaterializer): String = {
val url = materializer.materialize(path)
s"<script defer type='$tpe' src='${url.toString}'></script>"
}

private class WindowOnErrorException(errs: List[String]) extends Exception(s"JS error: $errs")

private def consumer[A](f: A => Unit): Consumer[A] = new Consumer[A] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.scalajs.jsenv.selenium

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

object CamelCase {
def hello(input: String): String = s"Hello ${camelCase(input)}!"

@JSImport("https://cdn.skypack.dev/camelcase@^6.0.0", JSImport.Default)
@js.native
def camelCase(input: String): String = js.native
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.scalajs.jsenv.selenium

import org.junit.Assert._
import org.junit.Test

class ModuleSupportTest {
@Test def testBasicImport(): Unit = {
assertEquals("Hello scalaJsSelenium!", CamelCase.hello("scala js selenium"))
}
}

0 comments on commit ddb7615

Please sign in to comment.