We will create a simple Kotlin/JavaScript library.
Using our favorite editor, we create a new file called library.kt:
Compile the library using the JS compiler
$ kotlinc-js -output sample-library.js -meta-info library.kt
The -meta-info
option indicates that an additional JS file with binary meta-information about compiled kotlin code will be created.
If you want to see all available options run
$ kotlinc-js -help
After compilation we have two new files:
sample-library.js sample-library.meta.js
You can simply distribute two JS files, sample-library.js
and sample-library.meta.js
. The former file contains translated JavaScript code, the latter file contains some meta-information about Kotlin code, which is needed by compiler.
Alternatively, you can append the content of sample-library.meta.js
to the end of sample-library.js
and distribute only the resulting file.
Also you can create an archive, which can be distributed as a library:
Create binom.kt:
import org.sample.factorial import org.sample.forOdd fun binom(m: Int, n: Int): Long = if (m < n) factorial(n) / factorial(m) / factorial(n-m) else 1 fun oddFactorial(n: Int): Long { var result: Long = 1L (1..n).forOdd { result = result * it } return result }
Compile with library:
Both files sample-library.js
and sample-library.meta.js
should be present in the latter case, because translated JavaScript file contains meta-information about inlining, which is needed by compiler.
If you have an archive sample-library.jar
, which contains sample-library.js
and sample-library.meta.js
, you can use the following command
© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/tutorials/javascript/getting-started-command-line/command-line-library-js.html