Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin JS: Is it a Thing?

Kotlin JS: Is it a Thing?

Alexey Buzdin

March 22, 2017
Tweet

More Decks by Alexey Buzdin

Other Decks in Programming

Transcript

  1. “She turned to the sunlight And shook her yellow head,

    And whispered to her neighbor: "Winter is dead.” - A.A. MILNE, When We Were Very Young https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin
  2. Let’s create HelloKotlin.kt fun main(args: Array<String>) { println("Hello JavaScript!") }

    Let’s compile it to javascript kotlinc-js -output out/sample-library.js -meta-info HelloKotlin.kt
  3. Let’s create HelloKotlin.kt fun main(args: Array<String>) { println("Hello JavaScript!") }

    Let’s compile transpile it to javascript kotlinc-js -output out/sample-library.js -meta-info HelloKotlin.kt
  4. if (typeof kotlin === 'undefined') { throw new Error(“Error loading

    module …”); } this['sample-library'] = function (_, Kotlin) { 'use strict'; var println = Kotlin.kotlin.io.println_s8jyv4$; function main(args) { println('Hello JavaScript!'); } _.main_kand9s$ = main; Kotlin.defineModule('sample-library', _); main([]); return _; }(typeof this['sample-library'] === 'undefined' ? {} : this['sample-library'], kotlin);
  5. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Console Output</title> </head>

    <body> <script type="text/javascript" src="node_modules/kotlin/kotlin.js"></script> <script type="text/javascript" src="out/sample-library.js"></script> </body> </html> THE KOTLIN STANDARD LIBRARY IS ALSO ON NPM!
  6. Running with GRADLE buildscript { ext.kotlin_version = '1.1.1' repositories {

    mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin2js' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version" }
  7. ALL JS LIBRARIES ARE AVAILABLE IN KOTLIN! Use ts2kt to

    convert type definitions http://definitelytyped.org