Introduction to Kotlin (to) JavaScript

综合技术 2018-05-18 阅读原文


Kotlin is a JVM language that borrows from Scala in an attempt to improve Java. In contrast to Java and Scala (GWT and Scala.js), in which the to-JavaScript compiler is an independent framework, Kotlin provides a to-JavaScript compiler as an integral part of its compiler. Another difference between Kotlin compiler and other frameworks is that Kotlin provides its lib in a separate file (named kotlin.js ), in addition to the compiled source code. This leads to the neccessity to include both files, or find tools to concatenate them. In this post, we will go through a simple example of setting up and running Kotlin to JavaScript.


There are different ways to setup Kotlin to JavaScript based on the IDE or the used build tool (more details can be found in the official documentation ). For this tutorial, we are going to choose the Maven way, as this is the build tool we use often. After spining up a simple maven project through IntelliJ, the following dependencies and plugins need to be added:



the kotlin-maven-plugin compiles kotlin to byte code through the compile goal and compiles the source code to JavaScript through the js goal. The kotlin-stdlib-js is used by the maven-dependency-plugin for including the kotlin.js file which contains the JavaScript version of Kotlin’s runtime and standard library. kotlin.js is static and does not change from an application to another, so it is possible to copy it manually or to import it directly in the HTML page from a link (e.g a CDN). kotlin.js is tied to the used Kotlin version.

Example :

Let’s print a Hello world to the console and examine the generated JavaScript:

fun main(args: Array) {
    println("Hello World!")

Generated kotlinjs-intro.js :

if (typeof kotlin === 'undefined') {
  throw new Error("Error loading module 'kotlinjs-intro'. Its dependency 'kotlin' was not found. 
                   Please, check whether 'kotlin' is loaded prior to 'kotlinjs-intro'.");
this['kotlinjs-intro'] = function (_, Kotlin) {
  'use strict';
  var println =$;
  function main(args) {
    println('Hello World!');
  var package$com = || ( = {});
  var package$gwidgets = package$com.gwidgets || (package$com.gwidgets = {});
  var package$kotlinjs = package$gwidgets.kotlinjs || (package$gwidgets.kotlinjs = {});
  package$kotlinjs.main_kand9s$ = main;
  Kotlin.defineModule('kotlinjs-intro', _);
  return _;
}(typeof this['kotlinjs-intro'] === 'undefined' ? {} : this['kotlinjs-intro'], kotlin);

As mentionned earlier, the generated script looks for defined Kotlin runtime methods and classes and throws an error if kotlin.js is not already loaded. This may lead to issues if scripts are included in the body or have "defer" attributes because the order is important. One possible solution is to use JavaScript build tools such as Grunt or Gulp to concatenate the two scripts.

Another inconvenience is the size of the scripts. Minifying and optimizing the JavaScript code is important for production settings. While GWT and Scala.js provide the user with the option to minify the generated script or run the Closure compiler for even more optimization, Kotlin leaves this task to the developer.

For this simple example, the total size loaded is about 1.51 MB, which is not ideal compared to GWT or Scala.js or even other JavaScript frameworks (check out this comparison between different front end frameworks). After minifying and concatening both scripts, the result is:

which is is better, but still far behind GWT and Scala.js.

Size of .js for printing a Hello World to the console
GWT59.5 KB (.nocache.js plus the loaded module)
Scala.js11.6 KB
Kotlin to JavaScript1.1 MB


Kotlin JavaScript seem to have several bindings for popular JavaScript Librairies ( , , and many others) like react, jquery, and also a dom API, but the ecosystem remains small as Kotlin in itself is a niche and relatively new compared to other JVM languages.


Kotlin to JavaScript is an interesting tool, but it seems like it is still in its infancy and still has a lot to learn. According to Kotlin’s documentation , Kotlin to JavaScript can be used to target both front-end and server side JavaScript. From a front-end perspective, Scala.js and GWT seem like better options. We will explore the generation of server side JavaScript in coming posts.




The MDN Redesign “Behind the Scenes” The MDN redesign will go live after noon Pacific Time today! Kuma , the code that produces the MDN site, is a weird mix of the old and the new.MDN ...
Infinite loop with asynchronous callback pattern i... Suppose I want to send an asynchronous AJAX request to the server, and when it responds send another request and repeat forever: function sendXHR(...
Accurately run a function when the minute changes? How could I accurately run a function when the minute changes? Using a setInterval could work if I trigger it right when the minute changes. But I'm w...
Encoding spaces with JavaScript encodeURIComponent How to encode cookie with javascript / jquery? I am working on an online shop together with my friend. He set a cookie for me wi...
Memoization in JavaScript Exercise: What is memoization? What are its benefits? What is the necessary condition for using memoization? Illustrate the benefits of memoizatio...