Git Product home page Git Product logo

kt-multiplatform-js's Introduction

Proof of concept - Working with Kotlin and Javascript mulitplatform

Goals

The benefit of using Kotlin to generate JS binaries is that I can have a single language for all my source code, without having to master many different languages.

If the following goals can be achieved by this project, then I can use Kotlin Javascript multiplatform to build CLI (Node.js) apps and Browser based apps (that run in Chrome or electron):

  • Use KT to write web apps that run in Node.js (CLI), Browser (Vanilla JS), and Browser (React).
  • Be able to import JS deps (that have TS bindings) and use them (eg: lodash, Ink3, etc).
  • Be able to debug KT code, after it has compiled to JS and is running Browser or Node.js.

How it all works

Kotlin MP uses a gradle as its build system, and JB has created a gradle plugin "Kotlin/JS plugin" - org.jetbrains.kotlin.js (very similar to what they did for IDEA plugins) that actually orchestrates a few things:

  • Kotlin/JS IR compiler that compiles KT code to IR code and then to JS
    • It is possible to use the LEGACY compiler, but that isn't recommended because Kotlin 1.4.x is all about the new IR compiler backend. Since I don't have any old code, I don't have to worry about the lack of binary compatibility (of generated JS) between the LEGACY and IR compilers.
  • Yarn to handle npm deps
    • Dukat to handle TS type defs for npm deps
  • Webpack to build the artifacts
  • Allows for selecting browser or Node.js execution environments
    • Configure Node.js package.json settings
    • Configure browser settings
  • Allows for run configurations to be declared
  • Allows for test configurations to be declared
  • And more.

Node.js - Configuring the execution environment and deps

Here are the official docs on configuring the kotlin { js(IR) { nodejs { ... } } } portion of the build.gradle.kts file.

The things in ... are:

  • binaries.executable() - This causes the compiler to emit executable .js files.
  • dependencies { ... } - The things that go in here are:
    • The Kotlin/JS standard library is always imported, so there's nothing to add here for it.
    • You have to import the kotlin.test library if you have tests.
    • You can also import the kotlinx-nodejs experimental library if you want direct access to Node.js APIs.
    • npm deps can be added here with this syntax.
      • Eg of npm dep: implementation(npm("react", "> 14.0.0 <=16.9.0"))
      • Eg of npm devDep: implementation(devNpm(...))

Debugging support

TODO Browser - Configuring the execution environment and deps

TODO Important Gradle tasks that are provided

kt-multiplatform-js's People

Contributors

nazmulidris avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.