Skip to content

Commit

Permalink
Added JSDOMNodeJSEnv.Config.exposeGlobalVars
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Apr 1, 2019
1 parent 3682c44 commit a967867
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
node_modules/
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,8 @@ lazy val `test-project`: Project = project.
enablePlugins(ScalaJSJUnitPlugin).
settings(
scalaJSUseMainModuleInitializer := true,
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv(
org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv.Config()
.withExposeGlobalVars(Set("global"))
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
val scriptsURIs = scriptFiles(input).map(JSDOMNodeJSEnv.materialize(_))
val scriptsURIsAsJSStrings =
scriptsURIs.map(uri => '"' + escapeJS(uri.toASCIIString) + '"')

val globalVarsDefs = config.exposeGlobalVars.map { v =>
s"""if ($v) { window["$v"] = $v; }"""
}

val jsDOMCode = {
s"""
|(function () {
Expand Down Expand Up @@ -104,6 +109,8 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
| if (error == null) {
| window["__ScalaJSEnv"] = __ScalaJSEnv;
| window["scalajsCom"] = global.scalajsCom;
|
| ${globalVarsDefs.mkString("\n ")}
| } else {
| throw error;
| }
Expand Down Expand Up @@ -195,13 +202,15 @@ object JSDOMNodeJSEnv {
final class Config private (
val executable: String,
val args: List[String],
val env: Map[String, String]
val env: Map[String, String],
val exposeGlobalVars: Set[String]
) {
private def this() = {
this(
executable = "node",
args = Nil,
env = Map.empty
env = Map.empty,
exposeGlobalVars = Set.empty[String]
)
}

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

def withExposeGlobalVars(exposeGlobalVars: Set[String]): Config =
copy(exposeGlobalVars = exposeGlobalVars)

private def copy(
executable: String = executable,
args: List[String] = args,
env: Map[String, String] = env
env: Map[String, String] = env,
exposeGlobalVars: Set[String] = exposeGlobalVars
): Config = {
new Config(executable, args, env)
new Config(executable, args, env, exposeGlobalVars)
}
}

Expand All @@ -231,6 +244,7 @@ object JSDOMNodeJSEnv {
* - `executable`: `"node"`
* - `args`: `Nil`
* - `env`: `Map.empty`
* - `exposeGlobalVars`: `Set.empty`
*/
def apply(): Config = new Config()
}
Expand Down
8 changes: 8 additions & 0 deletions test-project/src/test/scala/testproject/LibTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testproject

import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}

import org.junit.Test
import org.junit.Assert._
Expand All @@ -13,4 +14,11 @@ class LibTest {
Lib.appendDocument("foo")
assertEquals(1, count - oldCount)
}

@Test def expose_nodejs_global(): Unit = {
assertTrue(js.typeOf(g.global) == "object")
assertTrue(js.typeOf(g.global.require) == "function")

assertTrue(g.global.require("fs") != null)
}
}

0 comments on commit a967867

Please sign in to comment.