Skip to content

Commit

Permalink
Fix scala-js#3295: Do not set __ScalaJSEnv.javaSystemProperties in th…
Browse files Browse the repository at this point in the history
…e sbt plugin.

Instead, we set it up in our own build.

That feature was initially included in the sbt plugin because our
build needed it, but it has always been a bit hacky. In particular,
it requires the sbt plugin to know too much about how JS files are
executed, notably whether they are scripts or modules. Moreover,
that feature was the only reason left that the sbt plugin needed
to know about `__ScalaJSEnv` at all.
  • Loading branch information
sjrd committed Mar 16, 2018
1 parent 80a631f commit f583da1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
23 changes: 23 additions & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,29 @@ object Build {
Tests.Argument("-t" + arg)

testOptionTags.value.map(testArgument)
},

// Add a JS file defining Java system properties
jsExecutionFiles in Test := {
val prev = (jsExecutionFiles in Test).value

val javaSysPropsPattern = "-D([^=]*)=(.*)".r
val javaSystemProperties = (javaOptions in Test).value.collect {
case javaSysPropsPattern(propName, propValue) => (propName, propValue)
}.toMap

val formattedProps = javaSystemProperties.map {
case (propName, propValue) =>
"\"" + escapeJS(propName) + "\": \"" + escapeJS(propValue) + "\""
}
val code = {
"var __ScalaJSEnv = (typeof __ScalaJSEnv === \"object\" && __ScalaJSEnv) ? __ScalaJSEnv : {};\n" +
"__ScalaJSEnv.javaSystemProperties = {" + formattedProps.mkString(", ") + "};\n"
}
val javaSysPropsFile =
new MemVirtualJSFile("setJavaSystemProperties.js").withContent(code)

javaSysPropsFile +: prev
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,7 @@ private[sbtplugin] object ScalaJSPluginInternal {
*/
jsExecutionFiles := (jsExecutionFiles in (This, Zero, This)).value,

// Optionally add a JS file defining Java system properties
jsExecutionFiles ++= {
val javaSysPropsPattern = "-D([^=]*)=(.*)".r
val javaSystemProperties = javaOptions.value.collect {
case javaSysPropsPattern(propName, propValue) => (propName, propValue)
}.toMap

if (javaSystemProperties.isEmpty) {
Nil
} else {
val formattedProps = javaSystemProperties.map {
case (propName, propValue) =>
"\"" + escapeJS(propName) + "\": \"" + escapeJS(propValue) + "\""
}
val code = {
"var __ScalaJSEnv = (typeof __ScalaJSEnv === \"object\" && __ScalaJSEnv) ? __ScalaJSEnv : {};\n" +
"__ScalaJSEnv.javaSystemProperties = {" + formattedProps.mkString(", ") + "};\n"
}
Seq(new MemVirtualJSFile("setJavaSystemProperties.js").withContent(code))
}
},

// Crucially, add the Scala.js linked file to the JS files
// Add the Scala.js linked file to the JS files (by default, the only one)
jsExecutionFiles +=
new FileVirtualJSFile(scalaJSLinkedFile.value.data),

Expand Down

0 comments on commit f583da1

Please sign in to comment.