I’ve been using Kotlin to develop a small 2D simulation software, and I’m using Kotlin multiplatform projects to make it run on both the JVM and in a browser. So far it works well.
However, I have a problem when I want to call functions defined in Kotlin/JS from regular Javascript.
To make my application work in the browser, I’m including the big JS file which is under the “build/distributions” folder after running the “build” Gradle task. When my Kotlin/JS application contains a main() function, this one is automatically being called when the HTML pag referencing the JS file is opened, and it works well.
But if I remove the main function and instead I create a start() function which is supposed to be called manually (after a click on a button, for instance), it does not work: it says the function start() is not defined even though it is declared in the Kotlin code.
After opening the generated JS file it seems that indeed there is no start() function. It looks like all the name of the functions have been minified.
I tried adding @JsName, but it didn’t change anything.
So I guess I’m doing something wrong, but I really don’t know what and how to make it work.
Note: I'm using Kotlin 1.3.70
EDIT: Here is the core of my build.gradle.kts:
plugins {
kotlin("js") version "1.3.70-eap-184"
}
repositories {
mavenLocal()
mavenCentral()
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
}
dependencies {
}
kotlin {
target {
nodejs {
}
browser {
webpackTask {
mode = org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig.Mode.PRODUCTION
bin = "$projectDir/node_modules/$bin"
}
}
}
}