Consume Kotlin Multiplatform Library in vanilla JS web app

I have a Kotlin multiplatform library that targets Kotlin/JVM and Kotlin/JS (Browser) containing some domain classes and business logic that needs to be shared across both frontend and backend. Consuming the multiplatform library as a simple Gradle/Maven dependency in the Java-based backend project was relatively easy and flawless. However, Iā€™d like to write the frontend in plain Javascript or Typescript (not Kotlin) but call methods and classes in the Kotlin multiplatform library (which I have built for JS as well). Is this possible, and how would I do it?

I followed this steps:

  • run jsBrowserDevelopmentWebpack (there is another one for production)
  • copy/paste the .js in your web app
  • add it to an HTML using
  • use classes and functions with their whole package, something like
    ā†’ < module >.< your >.< package >.< class name >
    (if you are working on a multiplatform project maybe you need to add the project name and a ā€˜-ā€™)
    ā†’ < project name >-< module >.< your >.< package >.< class name >

Actually, this recipe is for the legacy compilator. IR compilator has a grade flag called binaries.library() which generates a bundle alongside ts typings, that could be consumed by external webpack. Or you can use GitHub - mpetuska/npm-publish: Gradle plugin for NPM package publishing. Allows for arbitrary publishing as well as seamless integration with Kotlin JS/MPP plugins. to produce an npm library bundle.

1 Like