Skip to content

Commit fa086a3

Browse files
Added JSDOMNodeJSEnv.Config.exposeGlobal
1 parent 3682c44 commit fa086a3

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target/
2+
node_modules/

build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@ lazy val `test-project`: Project = project.
8484
enablePlugins(ScalaJSJUnitPlugin).
8585
settings(
8686
scalaJSUseMainModuleInitializer := true,
87-
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()
87+
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv(
88+
org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv.Config()
89+
.withExposeGlobal(true)
90+
)
8891
)

jsdom-nodejs-env/src/main/scala/org/scalajs/jsenv/jsdomnodejs/JSDOMNodeJSEnv.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
104104
| if (error == null) {
105105
| window["__ScalaJSEnv"] = __ScalaJSEnv;
106106
| window["scalajsCom"] = global.scalajsCom;
107+
|
108+
| if (${config.exposeGlobal}) {
109+
| window["global"] = global;
110+
| }
107111
| } else {
108112
| throw error;
109113
| }
@@ -195,13 +199,15 @@ object JSDOMNodeJSEnv {
195199
final class Config private (
196200
val executable: String,
197201
val args: List[String],
198-
val env: Map[String, String]
202+
val env: Map[String, String],
203+
val exposeGlobal: Boolean
199204
) {
200205
private def this() = {
201206
this(
202207
executable = "node",
203208
args = Nil,
204-
env = Map.empty
209+
env = Map.empty,
210+
exposeGlobal = false
205211
)
206212
}
207213

@@ -214,12 +220,16 @@ object JSDOMNodeJSEnv {
214220
def withEnv(env: Map[String, String]): Config =
215221
copy(env = env)
216222

223+
def withExposeGlobal(exposeGlobal: Boolean): Config =
224+
copy(exposeGlobal = exposeGlobal)
225+
217226
private def copy(
218227
executable: String = executable,
219228
args: List[String] = args,
220-
env: Map[String, String] = env
229+
env: Map[String, String] = env,
230+
exposeGlobal: Boolean = exposeGlobal
221231
): Config = {
222-
new Config(executable, args, env)
232+
new Config(executable, args, env, exposeGlobal)
223233
}
224234
}
225235

@@ -231,6 +241,7 @@ object JSDOMNodeJSEnv {
231241
* - `executable`: `"node"`
232242
* - `args`: `Nil`
233243
* - `env`: `Map.empty`
244+
* - `exposeGlobal`: `false`
234245
*/
235246
def apply(): Config = new Config()
236247
}

test-project/src/test/scala/testproject/LibTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testproject
22

33
import scala.scalajs.js
4+
import scala.scalajs.js.Dynamic.{global => g}
45

56
import org.junit.Test
67
import org.junit.Assert._
@@ -13,4 +14,11 @@ class LibTest {
1314
Lib.appendDocument("foo")
1415
assertEquals(1, count - oldCount)
1516
}
17+
18+
@Test def expose_nodejs_global(): Unit = {
19+
assertTrue(js.typeOf(g.global) == "object")
20+
assertTrue(js.typeOf(g.global.require) == "function")
21+
22+
assertTrue(g.global.require("fs") != null)
23+
}
1624
}

0 commit comments

Comments
 (0)