An open API service indexing awesome lists of open source software.

https://github.com/linux-china/kotlin-deno-example

🦕Deno app development with Kotlin
https://github.com/linux-china/kotlin-deno-example

deno kotlin

Last synced: 4 months ago
JSON representation

🦕Deno app development with Kotlin

Awesome Lists containing this project

README

          

Kotlin Deno example
===================
🦕Deno development with Kotlin.

# Features

* Deno completion: convert lib.deno.d.ts to Kotlin declarations

# How it work?

* Compile kotlin into Javascript with nodejs target
* Append stub for Deno for compiled JS code like following.

```javascript
// ========stub for Deno=======
import Kotlin from "https://jspm.dev/kotlin@1.4.0"

const module = {
exports: {}
};

function require(moduleName) {
if (moduleName === 'kotlin') {
return Kotlin
} else {
return {};
}
}

// ====== compiled JS code from Kotlin======

(function (_, Kotlin) {
// ...
}(module.exports, require('kotlin')));

export default module.exports;
```

* Build the project

```
$ ./gradlew deno
```

* Run the code with Deno. Please add --no-check for Deno run to disable kotlin module check.

```
$ deno run --no-check build/deno/kotlin-deno-example.js
```

# How to invoke Deno stdlib?

* Declare "external fun" for Kotlin in src/main/kotlin/deno/lib.std.kt
* Add imports in "buildSrc/src/scripts/deps.js"

# Import other npm packages or Deno modules

* Use dukat to generate Kotlin declarations from xxx.d.ts or maintain lib.xxx.kt manually
* import Deno modules in deps.js
* import npm packages in deps.js by https://jspm.dev/xxx@version

# Use Kotlinx Coroutines library

* add dependency in build.gradle.kts

```
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.3.9")
```

* Add kotlinx-coroutines-core import in templates.js

```javascript
import Kotlin from "https://jspm.dev/kotlin@1.4.0"
import KotlinxCoroutinesCore from "https://jspm.dev/kotlinx-coroutines-core@1.3.9"

//......

function require(moduleName) {
if (moduleName === 'kotlin') {
return Kotlin
} else if (moduleName === 'kotlinx-coroutines-core') {
return KotlinxCoroutinesCore;
} else {
return {};
}
}
```

# Kotlin/JS IR compiler
As of Kotlin 1.4.0, the Kotlin/JS IR compiler has Alpha stability level.
With Kotlin/JS IR compiler support, and and compiled JS code is more clean.

# Use cases

* Kotlin Deno App
* Kotlin as library: TypeScript to invoke JavaScript code compiled by Kotlin

# Dev Tips

* Enables continuous build: re-execute tasks when task file inputs change

```
$ ./gradlew -t deno
```

* deno run --watch flags to watch file changes. Deno 1.4.0+ required

```
$ deno run --unstable --watch --allow-read --no-check build/deno/kotlin-deno-example.js
```

# References

* Deno: https://deno.land/
* Kotlin: https://kotlinlang.org/
* lib.deno.d.ts: https://github.com/denoland/deno/releases/latest/download/lib.deno.d.ts
* dukat: Converter of TypeScript definition files to Kotlin declarations https://github.com/Kotlin/dukat
* jspm: provides a module CDN allowing any package from npm to be directly loaded in the browser and other JS environments https://jspm.org/