If I have a JavaScript file called myfile.js
as follows:
function myJsFunc() { return "Hello from JavaScript"; }
How can I import this file into a Kotlin/JS project and invoke myJsFunc()
from Kotlin code?
If I have a JavaScript file called myfile.js
as follows:
function myJsFunc() { return "Hello from JavaScript"; }
How can I import this file into a Kotlin/JS project and invoke myJsFunc()
from Kotlin code?
Copied-over from slack discussion
First you need to declare an npm dependency in gradle
kotlin.sourceSets.named("jsMain") {
dependencies {
implementation(npm("package-name", "version"))
}
}
Then you can either use ./gradlew generateExternals
and pray that dukat works or write external declarations yourself.
package-name
and version
appears the same as it would in package.json, so all the protocols like file:xxx
are supported as well.
You can also hack it with local TS sources, however I do not recommend it. If you still must stick with it, create a small npm module somewhere on your repo and use that via file:path/to/that/module
version instead. I recommend generating absolute path to that module in gradle instead of trying to work out relative path from package.json that kotlin plugin generates
kotlin.sourceSets.named("jsMain") {
dependencies {
val pathToLocalNpmModule = rootProject.projectDir.resolve("js/my-module").canonicalPath
implementation(npm("my-module", "file:$pathToLocalNpmModule"))
}
}
Finally, for that particullar function, kotlin external declaration would look like this (assuming that you point your package.json#main
field directly to that js file)
// Explicit export
export function myJsFunc() { return "Hello from JavaScript"; }
// Default export
export default myJsFunc
// Explicit export
@JsModule("my-module")
public external fun myJsFunc(): String
// Default export
@JsModule("my-module")
@JsName("default")
public external fun myJsFunc(): String
Now on the inverse, if you want to integrate your kotlin/js gradle module into local js/ts project, I recommend just using ./gradlew pack
task provided by npm-publish gradle plugin (configures itself, just apply it) and use the bundle via file:xxx
version protocol in your js/ts package.json.
./gradlew generateExternals
doesn't give anything. It would be really nice to be able to generate Kotlin bindings with a gradle task. –
Lapoint You will probably find some interesting information in the doc: Use JavaScript from Kotlin.
Essentially, you'll need to write external
declarations manually to tell Kotlin about the functions and types that should be available in JS.
Making the file itself available depends on your build and how you deploy your code.
external
declarations, it says, tantalisingly, "When the compiler sees such a declaration, it assumes that the implementation for the corresponding class, function or property is provided externally (by the developer or via an npm dependency )" -- without explaining how to provide the implementation. –
Muzz dependencies
block with npm()
helper to get it. –
Latialatices resources
folder, for example? If not, how can I include it? –
Muzz ./gradlew nodeRun
to run my project, and unsurprisingly it can't find my external
function. I guess I need to put the JS file somewhere it can be found (where?) and include @JSModule
or similar annotation to point to the name of the file and the function within the file (what exactly?) –
Muzz © 2022 - 2024 — McMap. All rights reserved.
webpack.config.d
folder as Joffrey pointed out. – Grounds