Skip to content

Introduced support of modules #13

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

Merged
merged 1 commit into from
Jun 7, 2021
Merged
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 build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ lazy val `test-project`: Project = project.
enablePlugins(ScalaJSJUnitPlugin).
settings(
scalaJSUseMainModuleInitializer := true,
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)),
jsEnv := new net.exoego.jsenv.jsdomnodejs.JSDOMNodeJSEnv()
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

package net.exoego.jsenv.jsdomnodejs

import com.google.common.io.ByteStreams

import scala.util.control.NonFatal

import java.io._
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, StandardCopyOption}
import java.nio.file.{Files, Path, StandardCopyOption, StandardOpenOption}
import java.net.URI

import com.google.common.jimfs.Jimfs
Expand Down Expand Up @@ -52,6 +54,9 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
case Input.Script(script) =>
script

case Input.CommonJSModule(module) =>
JSDOMNodeJSEnv.wrapAsFunctionCallThis(module)

case _ =>
throw new UnsupportedInputException(input)
}.toList
Expand Down Expand Up @@ -194,6 +199,30 @@ object JSDOMNodeJSEnv {
}
}

/*
* The reason to add this hack is performance degradation.
* Details:
* - https://github.com/scala-js/scala-js/issues/4489
* - https://github.com/jsdom/jsdom/issues/3193
*/
private def wrapAsFunctionCallThis(path: Path): Path = {
val suffix = tmpSuffixRE.findFirstIn(path.getFileName.toString).orNull

val f = File.createTempFile("tmp-", suffix)
f.deleteOnExit()
val w = f.toPath
val out = Files.newOutputStream(w, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)
val writer = new BufferedWriter(new OutputStreamWriter(out))
writer.write("(function(){")
writer.newLine()
writer.flush()
ByteStreams.copy(Files.newInputStream(path), out)
writer.write("}).call(this);")
writer.newLine()
writer.close()
w
}

private def materialize(path: Path): URI = {
try {
path.toFile.toURI
Expand Down
10 changes: 10 additions & 0 deletions test-project/src/main/scala/testproject/OsArch.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package testproject

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

@JSImport("os", "arch")
@js.native
object OsArch extends js.Object {
def apply(): String = js.native
}
4 changes: 4 additions & 0 deletions test-project/src/test/scala/testproject/LibTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ class LibTest {
Lib.appendDocument("foo")
assertEquals(1, count - oldCount)
}

@Test def osArch_should_works(): Unit = {
assertNotNull(OsArch())
}
}