Kotlin/JS in 1.4 and beyond Sebastian Aigner October 14, 2020 - - PowerPoint PPT Presentation

kotlin js in 1 4 and beyond sebastian aigner
SMART_READER_LITE
LIVE PREVIEW

Kotlin/JS in 1.4 and beyond Sebastian Aigner October 14, 2020 - - PowerPoint PPT Presentation

Kotlin 1.4 Online Event Kotlin/JS in 1.4 and beyond Sebastian Aigner October 14, 2020 @sebi_io An overview of Kotlin/JS in 1.4 Kotlin/JS in 1.4 makes the development experience more unified and cohesive, and adds more control for


slide-1
SLIDE 1

Kotlin 1.4 Online Event October 14, 2020 @sebi_io

Kotlin/JS in 1.4 and beyond Sebastian Aigner

slide-2
SLIDE 2

An overview

  • f Kotlin/JS in 1.4
slide-3
SLIDE 3

Kotlin/JS in 1.4 makes the development experience more unified and cohesive, and adds more control for configurations and integrations directly from Gradle.

slide-4
SLIDE 4

kotlin2js and kotlin-dce-js are deprecated with Kotlin 1.4.0.

A clear start

plugins { kotlin("js") version ”1.4.0" } plugins { kotlin("multiplatform") version ”1.4.0" }

slide-5
SLIDE 5

Aligned naming conventions between Gradle plugins

Compatibility between JS and Multiplatform plugins

kotlin { js { browser() binaries.executable() } }

slide-6
SLIDE 6

Style support out of the box

source: webpack.js.org

.sass .jpg .css .jpg .png Modules with dependencies Static assets .sass .sass .png .cjs .js .hbs .js .js

slide-7
SLIDE 7

Style support out of the box

CSS & style-loader support through Gradle with fine grained configuration.

webpackTask { cssSupport.enabled = true } runTask { cssSupport.enabled = true } testTask { useKarma { // . . . webpackConfig.cssSupport.enabled = true } }

slide-8
SLIDE 8

Managing npm dependencies devNpm

  • ptionalNpm

peerNpm

{ "main": "kotlin/myProject.js", "devDependencies": { . . . }, "dependencies": { "camelcase": "6.0.0", "kotlin": ”...", "kotlin-test": ”..." }, "peerDependencies": { . . . }, "optionalDependencies": { . . . }, . . . "name": ”myProject", "version": "1.0.0-SNAPSHOT" }

implementation(npm("camelcase", "6.0.0"))

slide-9
SLIDE 9

Generating external declarations

1) Auto-generate at build time

implementation(npm( "decamelize", "4.0.0", generateExternals = true ))

Set default behavior for all npm dependencies (overwritten by individual settings) Enable external declaration for individual npm dependendency Dukat (experimental) auto-generates Kotlin external declarations, reduces work for using JS dependencies from Kotlin with type-safety. kotlin.js.generate.externals=true

slide-10
SLIDE 10

Dukat (experimental) auto-generates Kotlin external declarations, reduces work for using JS dependencies from Kotlin with type-safety.

Generating external declarations

2) Generate & adjust declarations manually

generateExternals Gradle task

Creates external declarations (in ./externals/)

slide-11
SLIDE 11

Decoupling the browser APIs

kotlin.browser kotlin.dom kotlinx.browser kotlinx.dom

Gradual shift for browser and DOM related packages towards separate artifacts.

slide-12
SLIDE 12

Using the Node.js API

fun main() { dns.lookup("example.org") { err, address, family -> console.log("address: $address, family IPv$family") } }

kotlinx-nodejs (experimental) provides type-safe access to Node.js APIs from Kotlin/JS code

slide-13
SLIDE 13

New Alpha IR Compiler for Kotlin/JS

The next evolutionary step, available to try right now.

slide-14
SLIDE 14

The Alpha IR Compiler is the main focus

  • f innovation for Kotlin/JS. Its goals

are to improve speed, bundle size, and interoperability with the JavaScript and TypeScript ecosystems.

slide-15
SLIDE 15

Addressing bundle size

  • Strengthening Dead Code Elimination
  • Per-Gradle module code splitting

(configurable from the DSL, coming soon™)

After compilation After JS DCE After bundle Default backend

3.9 MiB 1.1 MiB 713 KiB 430 KiB 329 KiB 184 KiB 74 KiB 40 KiB

IR backend After ZIP

slide-16
SLIDE 16

Planned support for ES6 (modules & co.)

  • Export your Kotlin code as ES6 modules
  • More optimizations regarding bundle

size unlocked (webpack tree shaking)

  • Gradual addition of more ES6 features

(e.g. classes) to the compilation output

slide-17
SLIDE 17

Planned feature parity with source maps

slide-18
SLIDE 18

Strong interop

  • Ability to use Kotlin/JS alongside
  • ther web technologies
  • No need to “Rewrite It In Kotlin”
  • Less constraints for choice
  • f framework, tooling
slide-19
SLIDE 19

Introducing @JsExport

@JsExport class KotlinGreeter(val greeting: String) { fun greet(name: String): String { return "$greeting, $name!" } } @JsExport fun wave(person: String): String { return "$person waves" }

slide-20
SLIDE 20

Preview: TypeScript definitions

type Nullable<T> = T | null | undefined export class KotlinGreeter { constructor(greeting: string); readonly greeting: string; greet(name: string): string; } export function wave(person: string): string; export as namespace library;

slide-21
SLIDE 21

Using Kotlin code from TypeScript

slide-22
SLIDE 22

Smooth transition to the new compiler

kotlin { js(IR) { browser { } binaries.executable() } } kotlin.js.compiler=ir

build.gradle(.kts) gradle.properties Backported @JsExport annotation and BOTH mode to ease transition

slide-23
SLIDE 23

Smooth transition to the new compiler

Backported @JsExport annotation and BOTH mode to ease transition

kotlin { js(IR /* or LEGACY, BOTH */) { browser { } binaries.executable() } } kotlin.js.compiler=ir /* legacy, both */

build.gradle(.kts) gradle.properties

slide-24
SLIDE 24

Both mode and Gradle metadata

BOTH Mode Legacy Compiler Legacy Artifact (JAR) IR Compiler IR Artifact (KLIB) same Maven coordinate

slide-25
SLIDE 25

Learn more & try the new Kotlin/JS compiler

Kotlin/JS IR compiler documentation available on kotlinlang.org

slide-26
SLIDE 26

Kotlin and WebAssembly

A brief update on another Kotlin target

slide-27
SLIDE 27

WebAssembly: Making progress!

  • Representation in the WebAssembly

community working group

  • Close interaction with the WebAssembly

VM teams

  • Work on a first prototype to play

around with

  • We have high hopes!
slide-28
SLIDE 28

Wrapping up

What we’ve seen, and where to go to learn more.

slide-29
SLIDE 29

Kotlin/JS in 1.4:

  • More unified Gradle DSL
  • More control and integrations
  • Browser & DOM API evolutions
  • Alpha IR Compiler with fancy new features
slide-30
SLIDE 30

More about Kotlin/JS

  • Check the ”What’s new” section on kotlinlang.org
  • Read the release blog post on blog.jetbrains.com/kotlin
  • Join the dialogue on kotlinlang.slack.com #javascript
slide-31
SLIDE 31

Thanks! Have a nice Kotlin

@sebi_io