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

Support jsdom v12.x in JSDOMNodeJSEnv #265

Merged
merged 2 commits into from Sep 24, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
106 changes: 75 additions & 31 deletions sbt-scalajs-bundler/src/main/scala/scalajsbundler/JSDOMNodeJSEnv.scala
Expand Up @@ -46,45 +46,89 @@ class JSDOMNodeJSEnv(
protected trait AbstractDOMNodeRunner extends AbstractNodeRunner {

protected def codeWithJSDOMContext(): Seq[VirtualJSFile] = {
val scriptsJSPaths = getLibJSFiles().map {
case file: FileVirtualFile => file.path
case file => libCache.materialize(file).getAbsolutePath
val scriptsFiles = (getLibJSFiles() :+ code).map {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case file: FileVirtualFile => file.file
case file => libCache.materialize(file)
}
val scriptsStringPath = scriptsJSPaths.map('"' + escapeJS(_) + '"')
val scriptsURIsAsJSStrings = scriptsFiles.map { file =>
'"' + escapeJS(file.toURI.toASCIIString) + '"'
}
val scriptsURIsJSArray = scriptsURIsAsJSStrings.mkString("[", ", ", "]")

val jsDOMCode = {
s"""
|(function () {
| var jsdom;
| try {
| jsdom = require("jsdom/lib/old-api.js"); // jsdom >= 10.x
| } catch (e) {
| jsdom = require("jsdom"); // jsdom <= 9.x
| }
| var windowKeys = [];
| var jsdom = require("jsdom");
|
| jsdom.env({
| html: "",
| virtualConsole: jsdom.createVirtualConsole().sendTo(console),
| created: function (error, window) {
| if (error == null) {
| window["__ScalaJSEnv"] = __ScalaJSEnv;
| window["scalajsCom"] = global.scalajsCom;
| windowKeys = Object.keys(window);
| } else {
| console.log(error);
| }
| },
| scripts: [${scriptsStringPath.mkString(", ")}],
| onload: function (window) {
| jsdom.changeURL(window, "http://localhost");
| for (var k in window) {
| if (windowKeys.indexOf(k) == -1)
| global[k] = window[k];
| if (typeof jsdom.JSDOM === "function") {
| // jsdom >= 10.0.0
| var virtualConsole = new jsdom.VirtualConsole()
| .sendTo(console, { omitJSDOMErrors: true });
| virtualConsole.on("jsdomError", function (error) {
| try {
| // Display as much info about the error as possible
| if (error.detail && error.detail.stack) {
| console.error("" + error.detail);
| console.error(error.detail.stack);
| } else {
| console.error(error);
| }
| } finally {
| // Whatever happens, kill the process so that the run fails
| process.exit(1);
| }
| });
|
| var dom = new jsdom.JSDOM("", {
| virtualConsole: virtualConsole,
| url: "http://localhost/",
|
| /* Allow unrestricted <script> tags. This is exactly as
| * "dangerous" as the arbitrary execution of script files we
| * do in the non-jsdom Node.js env.
| */
| resources: "usable",
| runScripts: "dangerously"
| });
|
| ${code.content}
| var window = dom.window;
| window["__ScalaJSEnv"] = __ScalaJSEnv;
| window["scalajsCom"] = global.scalajsCom;
|
| var scriptsSrcs = $scriptsURIsJSArray;
| for (var i = 0; i < scriptsSrcs.length; i++) {
| var script = window.document.createElement("script");
| script.src = scriptsSrcs[i];
| window.document.body.appendChild(script);
| }
| });
| } else {
| // jsdom v9.x
| var windowKeys = [];
|
| jsdom.env({
| html: "",
| virtualConsole: jsdom.createVirtualConsole().sendTo(console),
| created: function (error, window) {
| if (error == null) {
| window["__ScalaJSEnv"] = __ScalaJSEnv;
| window["scalajsCom"] = global.scalajsCom;
| windowKeys = Object.keys(window);
| } else {
| console.log(error);
| }
| },
| scripts: ${scriptsURIsAsJSStrings.init.mkString("[", ", ", "]")},
| onload: function (window) {
| jsdom.changeURL(window, "http://localhost");
| for (var k in window) {
| if (windowKeys.indexOf(k) == -1)
| global[k] = window[k];
| }
|
| ${code.content}
| }
| });
| }
|})();
|""".stripMargin
}
Expand Down
Expand Up @@ -28,6 +28,8 @@ webpackConfigFile in Test := Some(baseDirectory.value / "test.webpack.config.js"
requiresDOM in Test := true
//#relevant-settings

version in installJsdom := "12.0.0"

libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.0" % Test

useYarn := true