javascript as target in kotlin multiplatform library

Anmol Singh Sahi

I'm building a Kotlin multiplatform library. One of the targets in this project is javascript. In the source set I have added a dependency like this:

 val jsMain by getting {
        dependencies {
            implementation(npm("libphonenumber-js", "1.10.13"))
        }
    }

The gradle sync was successful, now I want to import the files in jsMain directory. How can I achieve this?

shaktiman_droid
  • Add npm dependency with generateExternals as you already did implementation(npm("libphonenumber-js", "1.10.13", generateExternals = true))

  • generateExternals = true triggers a tool called Dukat that generates Kotlin external declarations from the Typescript definition file of that npm module.

  • Once your project syncs, it would have externals folder under your shared module's build folder like shown below,

Screenshot of how external generated Kotlin code folder structure looks like

(Ignore kmp-lib-1621 in above image. That would be your module/library name instead)`

  • Now copy and paste these files in your project (jsMain source set) and remove generateExternal = true from your dependency otherwise it would generate this files everytime. (1) you would lose any manual change (2) if you update the library version then it can potentially break your project

You should be able to call generated external code from Kotlin code, whether you keep it in build folder or you pasted it in your project code.

Important Note: Dukat tool is experiemental and known to create externals that may not work 100% times. So remove all unnecessary code from external generated code so you only end up having few references of classes you want to use from the npm library. Do some trial and error and you would be fine.

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there multiplatform lock in Kotlin?

kotlin-multiplatform : how to use Kotlin Flow in Javascript

Kotlin MultiPlatform - Publish Android Library Failing

Is it possible to create a kotlin multiplatform project referencing the correct target of a common module?

Kotlin multiplatform: kotlinx.coroutines.test library not visible in commonTest module

Multiplatform library with native library

Serve kotlin multiplatform javascript from ktor

Kotlin multiplatform support for Optional

CPointer in Kotlin-Multiplatform

Publish Kotlin MultiPlatform Library Bintray Use Kotlin DSL

kotlinOptions in kotlin multiplatform project

How can I skip all target except Android for Multiplatform Kotlin library publication to maven?

Kotlin multiplatform project

What code coverage tool to use for the javascript code in a Kotlin Multiplatform project?

Kotlin Multiplatform Library: Unable to generate .framework for iOS

Cannot access fixedRateTimer in Kotlin Multiplatform

Hamcrest and Mockk for Kotlin Multiplatform Mobile

Kotlin Multiplatform build failure due to android target

kotlin multiplatform library simple http get request test

Publish kotlin multiplatform library to Maven Central (InvalidMavenPublicationException multiple artifacts with the identical ...)

How to make a multi-module kotlin multiplatform library for iOS?

Copying to clipboard on Kotlin Multiplatform

Can not start new kotlin multiplatform library with dependency on exposed library

Is it possible to use a C++ library from a kotlin multiplatform mobile app?

Kotlin Multiplatform Library using Cocoapods with multiple ios targets

Use JavaFX in Kotlin multiplatform

How To Set Android JVM Target Compability in Kotlin Multiplatform Mobile Project

Is there a way how to add Kotlin Multiplatform target to a published KMP library which does not contain the target artifact?

Kotlin Multiplatform generated code is not aware of target's code