@@ -10,6 +10,8 @@ import scala.util.control.NonFatal
10
10
11
11
import java .util .concurrent .{ConcurrentLinkedQueue , Executors }
12
12
import java .util .function .Consumer
13
+ import java .nio .file .Path
14
+ import java .net .URL
13
15
14
16
private sealed class SeleniumRun (
15
17
driver : WebDriver with JavascriptExecutor ,
@@ -129,20 +131,10 @@ private[selenium] object SeleniumRun {
129
131
newRun : Ctor [T ], failed : Throwable => T ): T = {
130
132
validator.validate(runConfig)
131
133
132
- val scripts = input.map {
133
- case Input .Script (s) => s
134
- case _ => throw new UnsupportedInputException (input)
135
- }
136
-
137
134
try {
138
135
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 setupJsPath = m.write(" setup.js" , JSSetup .setupCode(enableCom))
137
+ val page = m.materialize(" scalajsRun.html" , htmlPage(setupJsPath, input, m))
146
138
withCleanup(newDriver())(maybeCleanupDriver(_, config)) { driver =>
147
139
driver.navigate().to(page)
148
140
@@ -171,18 +163,31 @@ private[selenium] object SeleniumRun {
171
163
private def maybeCleanupDriver (d : WebDriver , config : SeleniumJSEnv .Config ) =
172
164
if (! config.keepAlive) d.close()
173
165
174
- private def htmlPage (scripts : Seq [java.net.URL ]): String = {
175
- val scriptTags =
176
- scripts.map(path => s " <script src=' ${path.toString}'></script> " )
166
+ private def htmlPage (setupJsPath : Path , input : Seq [Input ], materializer : FileMaterializer ): String = {
167
+ val setupJs = makeTag(setupJsPath, " text/javascript" , materializer)
168
+
169
+ val tags = input.map {
170
+ case Input .Script (path) => makeTag(path, " text/javascript" , materializer)
171
+ case Input .ESModule (path) => makeTag(path, " module" , materializer)
172
+ case _ => throw new UnsupportedInputException (input)
173
+ }
174
+
175
+ val allTags = setupJs +: tags
176
+
177
177
s """ <html>
178
178
| <meta charset="UTF-8">
179
179
| <body>
180
- | ${scriptTags .mkString(" \n " )}
180
+ | ${allTags .mkString(" \n " )}
181
181
| </body>
182
182
|</html>
183
183
""" .stripMargin
184
184
}
185
185
186
+ private def makeTag (path : Path , tpe : String , materializer : FileMaterializer ): String = {
187
+ val url = materializer.materialize(path)
188
+ s " <script defer type=' $tpe' src=' ${url.toString}'></script> "
189
+ }
190
+
186
191
private class WindowOnErrorException (errs : List [String ]) extends Exception (s " JS error: $errs" )
187
192
188
193
private def consumer [A ](f : A => Unit ): Consumer [A ] = new Consumer [A ] {
0 commit comments