Skip to content

Commit aae6d72

Browse files
committed
Fix #115: Add ESModule support
1 parent fe57fdb commit aae6d72

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

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

0 commit comments

Comments
 (0)