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 22, 2020
1 parent fe57fdb commit 0c455f6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 24 deletions.
25 changes: 17 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ val testSettings: Seq[Setting[_]] = commonSettings ++ Seq(
scalaJSUseMainModuleInitializer := true
)

val httpTestSettings: Seq[Setting[_]] = testSettings ++ Seq(
jsEnv := {
new SeleniumJSEnv(
jsEnvCapabilities.value,
SeleniumJSEnv.Config()
.withMaterializeInServer("tmp", "http://localhost:8080/tmp/")
)
}
)

// We'll need the name scalajs-env-selenium for the `seleniumJSEnv` project
name := "root"

Expand Down Expand Up @@ -127,13 +137,12 @@ lazy val seleniumJSEnvTest: Project = project.
lazy val seleniumJSHttpEnvTest: Project = project.
enablePlugins(ScalaJSPlugin).
enablePlugins(ScalaJSJUnitPlugin).
settings(testSettings).
settings(httpTestSettings)

lazy val seleniumJSHttpESModuleEnvTest: Project = project.
enablePlugins(ScalaJSPlugin).
enablePlugins(ScalaJSJUnitPlugin).
settings(httpTestSettings).
settings(
jsEnv := {
new SeleniumJSEnv(
jsEnvCapabilities.value,
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,13 +163,25 @@ 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) =>
val url = materializer.materialize(path)
s"<script src='${url.toString}'></script>"
case Input.ESModule(path) =>
val url = materializer.materialize(path)
s"<script type='module' src='${url.toString}'></script>"
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.scalajs.jsenv.selenium

object CamelCase {
def hello(input: String): String = s"Hello ${CamelcaseEsModule(input)}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.scalajs.jsenv.selenium

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

@JSImport("https://cdn.skypack.dev/camelcase@^6.0.0", JSImport.Default)
@js.native
object CamelcaseEsModule extends js.Object {
def apply(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 CamelCaseTest {
@Test def CamelCaseTest(): Unit = {
assertEquals("Hello scalaJsSelenium!", CamelCase.hello("scala js selenium"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.scalajs.jsenv.selenium

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

import scala.scalajs.js.Dynamic.global

class LocationTest {

@Test def LocationTest(): Unit = {
assertEquals("http:", global.window.location.protocol.toString())
assertEquals("localhost:8080", global.window.location.host.toString())
}
}

0 comments on commit 0c455f6

Please sign in to comment.