Creating Kotlin/JavaScript library with IntelliJ IDEA
Setting up the environment
The tutorial works with IntelliJ IDEA Community Edition as well as IntelliJ IDEA Ultimate. For best experience developing Kotlin/JavaScript applications, we recommend using IntelliJ IDEA Ultimate, which includes the JavaScript debugger and other JavaScript support features.
-
Create a New Kotlin/JavaScript Project.
-
Specify the project name (e.g. sample-library), and click the Create for Kotlin JS library.
-
Select “Use library from plugin”. Of cource, you can choose copy library to some directory in your project, if you prefer.
-
Click the Finish.
-
We should now have the new project created and the following folder structure
-
Let’s create a new Kotlin file under the source folder. It can be named anything. In our case we will call it library:
package org.sample fun factorial(n: Int): Long = if (n == 0) 1 else n * factorial(n - 1)
-
Using File > Settings, check compiler settings for Kotlin compiler. Disable “Copy library runtime files”.
Click the Build > Make Project, and check that out/production directory contains two new files.
-
Open Project Structure Dialog (File > Project Structure), select Artifacts and add new empty artifact.
Choose name for artifact and add module compilation output to the content of newly created artifact.
-
Click the Build > Build Artifacts, and the resulting artifact is our library, which can be distributed.