Skip to content

Commit a967867

Browse files
Added JSDOMNodeJSEnv.Config.exposeGlobalVars
1 parent 3682c44 commit a967867

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-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+
.withExposeGlobalVars(Set("global"))
90+
)
8891
)

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
7272
val scriptsURIs = scriptFiles(input).map(JSDOMNodeJSEnv.materialize(_))
7373
val scriptsURIsAsJSStrings =
7474
scriptsURIs.map(uri => '"' + escapeJS(uri.toASCIIString) + '"')
75+
76+
val globalVarsDefs = config.exposeGlobalVars.map { v =>
77+
s"""if ($v) { window["$v"] = $v; }"""
78+
}
79+
7580
val jsDOMCode = {
7681
s"""
7782
|(function () {
@@ -104,6 +109,8 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
104109
| if (error == null) {
105110
| window["__ScalaJSEnv"] = __ScalaJSEnv;
106111
| window["scalajsCom"] = global.scalajsCom;
112+
|
113+
| ${globalVarsDefs.mkString("\n ")}
107114
| } else {
108115
| throw error;
109116
| }
@@ -195,13 +202,15 @@ object JSDOMNodeJSEnv {
195202
final class Config private (
196203
val executable: String,
197204
val args: List[String],
198-
val env: Map[String, String]
205+
val env: Map[String, String],
206+
val exposeGlobalVars: Set[String]
199207
) {
200208
private def this() = {
201209
this(
202210
executable = "node",
203211
args = Nil,
204-
env = Map.empty
212+
env = Map.empty,
213+
exposeGlobalVars = Set.empty[String]
205214
)
206215
}
207216

@@ -214,12 +223,16 @@ object JSDOMNodeJSEnv {
214223
def withEnv(env: Map[String, String]): Config =
215224
copy(env = env)
216225

226+
def withExposeGlobalVars(exposeGlobalVars: Set[String]): Config =
227+
copy(exposeGlobalVars = exposeGlobalVars)
228+
217229
private def copy(
218230
executable: String = executable,
219231
args: List[String] = args,
220-
env: Map[String, String] = env
232+
env: Map[String, String] = env,
233+
exposeGlobalVars: Set[String] = exposeGlobalVars
221234
): Config = {
222-
new Config(executable, args, env)
235+
new Config(executable, args, env, exposeGlobalVars)
223236
}
224237
}
225238

@@ -231,6 +244,7 @@ object JSDOMNodeJSEnv {
231244
* - `executable`: `"node"`
232245
* - `args`: `Nil`
233246
* - `env`: `Map.empty`
247+
* - `exposeGlobalVars`: `Set.empty`
234248
*/
235249
def apply(): Config = new Config()
236250
}

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)