coletz
10/09/2018, 3:11 PMconst jsdom = require("jsdom"); const { JSDOM } = jsdom;. For what I've understood, this is equivalent to const JSDOM = require("jsdom").JSDOM. Anyway when trying to use it with kotlin I'm having some issue since JSDOM is a constructor, but in kotlin is called as a function (and thus it fails with Class constructor JSDOM cannot be invoked without 'new'. Is it possible to call JSDOM as a constructor in any way? I've tried so many stuff, even using js(require(jsdom).JSDOM).window as Window but it fails with ReferenceError: Window is not definedSvyatoslav Kuzmich [JB]
10/09/2018, 3:32 PMval dom = js("new JSDOM(html)")gbaldeck
10/09/2018, 4:05 PMcoletz
10/09/2018, 4:24 PMjs("new JSDOM(..)") leads to JSDOM is not definedcoletz
10/09/2018, 4:26 PMcoletz
10/09/2018, 4:44 PMSvyatoslav Kuzmich [JB]
10/09/2018, 4:47 PMexternal class? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-js-module/index.htmlgbaldeck
10/09/2018, 4:48 PMgbaldeck
10/09/2018, 4:52 PMgbaldeck
10/09/2018, 4:52 PMcoletz
10/09/2018, 4:53 PMcoletz
10/09/2018, 5:01 PMexternal fun require(module: String): dynamic
// with js one does: const jsdom = require('jsdom');
val jsdom = require("jsdom");
// with js one does: const { JSDOM } = jsdom
val JSDOM = jsdom.JSDOM // totally not sure about that
// then for calling with js: const dom = new JSDOM(html)
val dom = JSDOM(html) // not working
Sorry if I'm not understanding correctly :/gbaldeck
10/09/2018, 5:06 PMconsole.log("jsdom: ", jsdom) in order to see what the object contains. It probably contains a default property or some other property that actually contains the full module. Then you can reference that by doing val jsdom = require("jsdom").default and your above code should workgbaldeck
10/09/2018, 5:13 PM@JsModule as wellrequire statements hould be fine and you do not need @JsModulegildor
10/09/2018, 5:19 PM@JsModule looks more idiomatic for me than requirecoletz
10/09/2018, 5:20 PMcoletz
10/09/2018, 5:29 PMgildor
10/09/2018, 5:30 PMgbaldeck
10/09/2018, 5:45 PMjs("new ImportedConstructor"), and I honestly can't think of a way to do it besides something like this fun JSDOM(html: dynamic): dynamic = js("new jsdom.JSDOM(html)")coletz
10/09/2018, 6:20 PM@JsModule("jsdom")
abstract external class JsDom {
class JSDOM(url: String){
val window: Window
}
}coletz
10/09/2018, 6:21 PMgbaldeck
10/09/2018, 6:54 PMJsDom.JSDOM() ?coletz
10/09/2018, 6:55 PMgbaldeck
10/09/2018, 7:00 PMJsModule at the file level as well, which would allow you you get rid of the abstract external class JsDom and just use external class JSDOM https://kotlinlang.org/docs/reference/js-modules.html#applying-jsmodule-to-packagescoletz
10/09/2018, 7:52 PM@file:JsModule("jsdom")
package mozilla
import org.w3c.dom.Window
external class JSDOM(html: String){
val window: Window
}
now it's instantiated simply with JSDOM(html), without jsdom. prefixgildor
10/10/2018, 12:51 AMrequire