Creating a Kotlin JavaScript library with the Command Line Compiler

Creating a Kotlin JavaScript library with the Command Line Compiler

This tutorial walks us through creating a Kotlin JavaScript library using the command line compiler.

Creating a Kotlin/JavaScript library

We will create a simple Kotlin/JavaScript library.

  1. Using our favorite editor, we create a new file called library.kt:

    package org.sample
       
    fun factorial(n: Int): Long = if (n == 0) 1 else n * factorial(n - 1)
       
    inline fun IntRange.forOdd(f: (Int) -> Unit) {
        this.forEach { if (it % 2 == 1) f(it) }
    }
    
  2. 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