From 72e8bfc6318c4931847c258d427d8381fe6e1af9 Mon Sep 17 00:00:00 2001 From: Binh Nguyen Date: Thu, 20 Sep 2018 21:52:45 +0700 Subject: [PATCH 1/2] Use the new API of jsdom v10 in JSDOMNodeJSEnv should allow us to ``` version in installJsDom := "12.0.0" ``` --- .../scala/scalajsbundler/JSDOMNodeJSEnv.scala | 106 +++++++++++++----- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/JSDOMNodeJSEnv.scala b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/JSDOMNodeJSEnv.scala index 6eb72831..e529fc79 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/JSDOMNodeJSEnv.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/JSDOMNodeJSEnv.scala @@ -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 { + 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