Kotlin / kotlinx.html

Kotlin DSL for HTML
Apache License 2.0
1.61k stars 132 forks source link

`HTMLLegendElement is not defined` in nodejs main #159

Closed fwilhe2 closed 2 years ago

fwilhe2 commented 4 years ago

Hi,

I've tried creating some html in a Kotlin Multiplatform project. Jvm and JS (Browser) seems to work well, but I've got some error in nodejs (12) which seems like a bug to me. Kotlin 1.4, Gradle 6.6, org.jetbrains.kotlinx:kotlinx-html-js:0.7.2, project generated via IntelliJ.

src/commonMain/kotlin/demo.kt:

import kotlinx.html.stream.createHTML
import kotlinx.html.table

fun makesomehtml(): String {
    return createHTML().table {  }
}

src/jsMain/kotlin/main.kt:

fun main() {
    print(makesomehtml())
}

The html generator code should work both on jvm and js, thus I've put it into commonMain. In the actual project I've got both dependencies for jvm and js of kotlinx.html.

I've pushed a minimal reproducer here https://github.com/fwilhe2/node-kotlin-html-repro

Run ./gradlew jsNodeRun --info to reproduce (also see the gh actions workflow in the repo)

Error:


/home/runner/work/node-kotlin-html-repro/node-kotlin-html-repro/build/js/node_modules/kotlinx-html-js/kotlinx-html-js.js:11
}(this, function (_, Kotlin) {
 ^
ReferenceError: HTMLLegendElement is not defined
    at /home/runner/work/node-kotlin-html-repro/node-kotlin-html-repro/build/js/node_modules/kotlinx-html-js/kotlinx-html-js.js:65:29
    at /home/runner/work/node-kotlin-html-repro/node-kotlin-html-repro/build/js/node_modules/kotlinx-html-js/kotlinx-html-js.js:5:5
    at Object.<anonymous> (/home/runner/work/node-kotlin-html-repro/node-kotlin-html-repro/build/js/node_modules/kotlinx-html-js/kotlinx-html-js.js:11:2)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at /home/runner/work/node-kotlin-html-repro/node-kotlin-html-repro/build/js/packages/node-kotlin-html-repro/kotlin/node-kotlin-html-repro.js:5:48

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':jsNodeRun'.
> Process 'command '/home/runner/.gradle/nodejs/node-v12.16.1-linux-x64/bin/node'' finished with non-zero exit value 1

Did I mess up the config, or is this a bug?

SalomonBrys commented 3 years ago

While waiting for 0.7.3 to be published, here's a (horrible) hack in Gradle that "fixes" the issue:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile> {
    doLast {
        val content = outputFile.readText()
        outputFile.writer().buffered().use {
            it.write("if (typeof(HTMLLegendElement) == 'undefined') HTMLLegendElement = {};\n")
            it.write(content)
        }
    }
}
fwilhe2 commented 3 years ago

Any update on this?

Tapchicoma commented 2 years ago

I don't see this issue anymore after updating 0.7.2 -> 0.7.5 release

fwilhe2 commented 2 years ago

I don't see this issue anymore after updating 0.7.2 -> 0.7.5 release

Thanks for the hint, I could verify this in my reproducer