2 years ago
#67697
applecommander
How to export a Kotlin Multi-Platform project to a js project?
I'm trying to create a kotlin multi-platform project which will be used be independent android, ios, and javascript projects. The independent projects are meant to call into this library. I've been able to export the android and ios versions but I can't figure out how to export the javascript version.
My build file looks like this:
plugins {
kotlin("multiplatform") version "1.6.0"
id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
// id("net.akehurst.kotlin.kt2ts") version("1.6.0")
}
group = "me.mikschn"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin.targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java) {
binaries.all {
binaryOptions["memoryModel"] = "experimental"
}
}
kotlin {
jvm {
withJava()
}
js(LEGACY) {
browser {
webpackTask {
output.libraryTarget = "commonjs2"
}
commonWebpackConfig {
cssSupport.enabled = true
}
}
binaries.executable()
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
iosArm32("nativeArm32") {
binaries {
framework {
baseName = "KMPExample"
}
}
}
iosArm64("nativeArm64") {
binaries {
framework {
baseName = "KMPExample"
}
}
}
iosX64("nativeX64") {
binaries {
framework {
baseName = "KMPExample"
}
}
}
multiplatformSwiftPackage {
packageName("KMPExample")
swiftToolsVersion("5.3")
targetPlatforms {
iOS { v("13") }
}
outputDirectory(File(rootDir, "/"))
}
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt")
implementation("com.benasher44:uuid:0.3.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")
}
}
val jvmTest by getting {
dependencies {
implementation("junit:junit:4.13.2")
implementation("org.jetbrains.kotlin:kotlin-test:1.6.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0-native-mt")
implementation("org.mockito:mockito-core:4.1.0")
implementation ("io.kotlintest:kotlintest-runner-junit5:3.3.2")
implementation ("org.jetbrains.kotlin:kotlin-reflect:1.6.0")
runtimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.5.2")
implementation ("org.junit.jupiter:junit-jupiter-api:5.5.2")
implementation ("org.junit.jupiter:junit-jupiter-params:5.5.2")
}
}
val jsMain by getting
}
}
I exported the javascript build using Verdaccio. From the folder kmpexample/build/js/packages/KMPExample
I ran npm publish --registry http://localhost:4873
.
The generated package.json in that folder looks like this:
{
"name": "KMPExample",
"version": "1.0.0-SNAPSHOT",
"main": "kotlin/KMPExample.js",
"devDependencies": {
"webpack": "5.57.1",
"webpack-cli": "4.9.0",
"format-util": "1.0.5",
"source-map-loader": "3.0.0",
"css-loader": "6.3.0",
"style-loader": "3.3.0"
},
"dependencies": {},
"peerDependencies": {},
"optionalDependencies": {},
"bundledDependencies": []
}
(Note there are no references to kotlin
or uuid-js
which are dependencies of my kotlin project).
Then in the folder kmpexample/jsTestApp/ts-react
(javascript app) I modified package.json
to include "KMPExample": "1.0.0-SNAPSHOT"
. Then I ran yarn start
to build and run the js project.
I get these compilation errors:
Compiled with problems:X
ERROR in ./node_modules/KMPExample/kotlin/KMPExample.js
Module not found: Error: Can't resolve 'kotlin' in '/Users/mikschn/workspace/kmpexample/jsTestApp/ts-react/node_modules/KMPExample/kotlin'
ERROR in ./node_modules/KMPExample/kotlin/KMPExample.js
Module not found: Error: Can't resolve 'uuid-js-legacy' in '/Users/mikschn/workspace/kmpexample/jsTestApp/ts-react/node_modules/KMPExample/kotlin'
ERROR in src/App.tsx:4:22
TS7016: Could not find a declaration file for module 'KMPExample'. '/Users/mikschn/workspace/example/jsTestApp/ts-react/node_modules/KMPExample/kotlin/KMPExample.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/KMPExample` if it exists or add a new declaration (.d.ts) file containing `declare module 'KMPExample';`
2 | import logo from "./logo.svg";
3 | import "./App.css";
> 4 | import * as kmp from "KMPExample";
| ^^^^^^^^^^^^
5 |
6 | function App() {
7 | kmp.main()
It seems that the system cannot find the dependencies of my project, which are kotlin
and uuid-js-legacy
.
What is the right way to export my KMP project to a javascript project, so it can be called by that js project?
javascript
kotlin
kotlin-multiplatform
0 Answers
Your Answer