Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSDOMNodeJSEnv.Config.exposeGlobal #19

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}