HI! How to provide an independent TS/JS project's ...
# javascript
м
HI! How to provide an independent TS/JS project's function implementation to Kotlin? I do not understand how to do it with gradle or whatever else. Relatable stack overflow question Situation: I have a Kotlin project and I want to use a TypeScript function from an opensource app, so I copied the source code to my project. The app starts via yarn.
b
First you need to declare an npm dependency in gradle
Copy code
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
Copy code
kotlin.sourceSets.named("jsMain") {
  dependencies {
    val pathToLocalNpmModule = rootProject.projectDir.resolve("js/my-module").canonicalPath
    implementation(npm("my-module", "file:$pathToLocalNpmModule"))
  }
}
Posted all that on SO as well for persistence.
м
@Big Chungus Thank you for the response! if I want to include the whole project, I should use package-name="file:relative_path_to_project"? when i do so, build tells me  Package "file:relative_path_to_project" refers to a non-existing file '"root_project/build/js/project@0.1.0"'. What is wrong?
b
Note my comment about generating absolute path in gradle
Also know that kotlin (nor any other js runtime except deno) cannot work with ts files, those need to be transpiled to js
(drop a +1 on SO if it ends up helping you to solve your issue)
м
@Big Chungus I declared an npm dependency like in your first comment and generated declarations via dukat. But when I call the function, it does not find it at runtime: onStart ERROR my_func is not defined. Do you have any idea what could go wrong?
b
Dukat went wrong. It's still very experimental. I usually just write external declarations of the stuff I need myself. I might use dukat generations as a starting point sometimes.
Plus it seems that dukat is unable to link stuff that have their declarations published separatelly (@types/xxx)
м
Can you please suggest any guide on writing declarations?
b
I've dropped some examples in SO. There's no definitive guide, because JS world is messed up and there are million ways of importing/exporting stuff...
e
Stumbled upon the SO answer two years later. Thanks!
K 1