Skip to content

Commit 95b925e

Browse files
Fixed #183: JSDOMNodeJSEnv is handled incorrectly (Scalajs support issue)
1 parent 182d26a commit 95b925e

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

build.sbt

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ lazy val runtime = CrossProject("scalac-scoverage-runtime", file("scalac-scovera
7575
libraryDependencies += "org.scalatest" %%% "scalatest" % ScalatestVersion % "test",
7676
scalaJSStage := FastOptStage,
7777
inConfig(Test)(jsEnv := RhinoJSEnv().value)
78+
//
79+
// to test using Node.js env
80+
//
81+
//inConfig(Test)(jsEnv := new org.scalajs.jsenv.nodejs.NodeJSEnv()),
82+
//requiresDOM in Test := true
7883
)
7984

8085
lazy val `scalac-scoverage-runtimeJVM` = runtime.jvm

scalac-scoverage-runtime/js/src/main/scala/scalajssupport/File.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ class File(path: String) {
5757
}
5858

5959
object File {
60-
val jsFile: JsFileObject = if (js.Dynamic.global.hasOwnProperty("Packages").asInstanceOf[Boolean])
60+
61+
private val jsFile: JsFileObject = if (g.hasOwnProperty("Packages").asInstanceOf[Boolean])
6162
RhinoFile
62-
else if (!js.Dynamic.global.hasOwnProperty("window").asInstanceOf[Boolean])
63+
else if (js.typeOf(g.global) == "object" && js.typeOf(g.global.require) == "function")
6364
NodeFile
6465
else
6566
PhantomFile

scalac-scoverage-runtime/js/src/main/scala/scalajssupport/NodeFile.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scalajssupport
22

33
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSGlobal
45

56
class NodeFile(path: String) extends JsFile {
67
def this(path: String, child: String) = {
@@ -85,9 +86,17 @@ trait NodePath extends js.Object {
8586
def join(paths: String*): String = js.native
8687
}
8788

89+
@js.native
90+
@JSGlobal("global")
91+
object NodeJsGlobal extends js.Object {
92+
def require(module: String): js.Any = js.native
93+
}
94+
8895
private[scalajssupport] object NodeFile extends JsFileObject {
89-
val fs: FS = js.Dynamic.global.require("fs").asInstanceOf[FS]
90-
val nodePath: NodePath = js.Dynamic.global.require("path").asInstanceOf[NodePath]
96+
97+
private val fs: FS = NodeJsGlobal.require("fs").asInstanceOf[FS]
98+
private val nodePath: NodePath = NodeJsGlobal.require("path").asInstanceOf[NodePath]
99+
91100
def write(path: String, data: String, mode: String = "a") = {
92101
fs.writeFileSync(path, data, js.Dynamic.literal(flag = mode))
93102
}

0 commit comments

Comments
 (0)