Skip to content

Commit

Permalink
Merge pull request #116 from mushtaq/esmodules
Browse files Browse the repository at this point in the history
Fix #115: Add ESModule support
  • Loading branch information
gzm0 authored Sep 27, 2020
2 parents fe57fdb + f964525 commit bb39519
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 21 deletions.
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ lazy val seleniumJSEnv: Project = project.
* guava stuff which in turn makes selenium fail.
*/
"org.seleniumhq.selenium" % "selenium-server" % "3.141.59",
"org.scala-js" %% "scalajs-js-envs" % scalaJSVersion,
"org.scala-js" %% "scalajs-js-envs-test-kit" % scalaJSVersion % "test",
"org.scala-js" %% "scalajs-js-envs" % "1.1.1",
"com.google.jimfs" % "jimfs" % "1.1",
"org.scala-js" %% "scalajs-js-envs-test-kit" % "1.1.1" % "test",
"com.novocode" % "junit-interface" % "0.11" % "test"
),

Expand Down Expand Up @@ -135,5 +136,6 @@ lazy val seleniumJSHttpEnvTest: Project = project.
SeleniumJSEnv.Config()
.withMaterializeInServer("tmp", "http://localhost:8080/tmp/")
)
}
},
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }
)
2 changes: 1 addition & 1 deletion project/build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.2.0")

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package org.scalajs.jsenv.selenium

import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path}

import com.google.common.jimfs.Jimfs

private[selenium] object JSSetup {
def setupCode(enableCom: Boolean): String = {
def setupFile(enableCom: Boolean): Path = {
val path = Jimfs.newFileSystem().getPath("setup.js")
val contents = setupCode(enableCom).getBytes(StandardCharsets.UTF_8)
Files.write(path, contents)
}

private def setupCode(enableCom: Boolean): String = {
s"""
|(function() {
| // Buffers for console.log / console.error
Expand Down Expand Up @@ -77,4 +88,5 @@ private[selenium] object JSSetup {
|}).call(this)
""".stripMargin
}

}
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,11 @@ 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 setupJsScript = Input.Script(JSSetup.setupFile(enableCom))
val fullInput = setupJsScript +: input
val page = m.materialize("scalajsRun.html", htmlPage(fullInput, m))
withCleanup(newDriver())(maybeCleanupDriver(_, config)) { driver =>
driver.navigate().to(page)

Expand Down Expand Up @@ -171,18 +164,27 @@ 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(fullInput: Seq[Input], materializer: FileMaterializer): String = {
val tags = fullInput.map {
case Input.Script(path) => makeTag(path, "text/javascript", materializer)
case Input.ESModule(path) => makeTag(path, "module", materializer)
case _ => throw new UnsupportedInputException(fullInput)
}

s"""<html>
| <meta charset="UTF-8">
| <body>
| ${scriptTags.mkString("\n ")}
| ${tags.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'></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 bb39519

Please sign in to comment.