1

I'm trying to export Kotlin functions to Javascript. The problem is, functions which require arguments are renamed after Kotlin2JS operation, here's an example:

Kotlin source:

fun withParam(args: String) {
    println("JavaScript generated through Kotlin")
}

fun withoutParams() {
    println("Without params")
}

After Kotlin2JS, trying to require in Node REPL:

> const kotlinBundle = require('./build/index.js');
undefined
> kotlinBundle
{ 'withParam_61zpoe$': [Function: withParam],
  withoutParams: [Function: withoutParams] }
>

As you can see, function with argument got exported with _61zpoe$ suffix. Is it possible to get rid of that part?

I'm using kotlin2js plugin and kotlin-stdlib-js:1.1.1 library, my kotlinOptions are:

compileKotlin2Js.kotlinOptions {
  moduleKind = "commonjs"
  outputFile = "build/index.js"
}

Thanks

1 Answer 1

2

You can use @JsName annotation to provide exact name for the function (or other symbol) in compiled javascript. I.e.

@JsName("withParam")    
fun withParam(args: String) {
      println("JavaScript generated through Kotlin")
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.