Hi everyone! I have a Kotlin Multiplatform library...
# multiplatform
c
Hi everyone! I have a Kotlin Multiplatform library imported to a React Js project and I need to read the private files (in this case local_config.json) of the react project from the Kmp library. Is there any way to do this? I tried to use require() but if I don’t hardcode the path directly as a parameter (require(“../../../../../src/main/resources/local_config.json”)), it doesn’t work. I suspect this is because of how the build tool (Webpack) works on web. Do somebody knows a solution for this?
b
Is the project oss, can you share the repo? If not, can you at least share your kmp project layout and gradle buildfile?
Also what strategy are you using to export kmp project to js?
c
The export strategy is to pulish the project to a private npm registry and install it from there in the react project.
Copy code
plugins {
    kotlin("plugin.serialization") version "1.4.21"
    kotlin("native.cocoapods")
    kotlin("multiplatform")
    id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
    id("com.android.library")
    id("kotlin-parcelize")
    id("maven-publish")
}

group = ""
version = ""

kotlin {
    android()

    iosX64()
    iosArm64()
    iosSimulatorArm64()

    tvos()
    tvosSimulatorArm64()

    js(IR) {
        moduleName = "lib"
        browser {
            webpackTask {
                outputFileName = "output.js"
                output.library = "outputLib"
            }
            testTask {
                useKarma {
                    useChrome()
                }
            }
        }
        generateTypeScriptDefinitions()
        binaries.library()
        binaries.executable()
    }

    cocoapods {
        summary = ""
        homepage = ""
        ios.deploymentTarget = "14.1"
        framework {
            baseName = "ConfigLibrary"
        }
    }

    multiplatformSwiftPackage {
        packageName("KotlinApi")
        swiftToolsVersion("5.3")
        targetPlatforms {
            iOS { v("13") }
            macOS { v("10_15") }
            tvOS { v("13") }
        }
    }

    val publicationsFromMainHost =
        listOf(js()).map { it.name } + "kotlinMultiplatform"
    publishing {
        publications {

            matching { it.name in publicationsFromMainHost }.all {
                val targetPublication = this@all
                tasks.withType<AbstractPublishToMaven>()
                    .matching { it.publication == targetPublication }
                    .configureEach { onlyIf { findProperty("isMainHost") == "true" } }
            }

            repositories {
                maven(url = "RepoUrl") {
                    name = "RepoName"
                    credentials {
                        username = "username"
                        password = "password"
                    }
                }
            }

            register<MavenPublication>("release") {
                groupId = ""
                artifactId = ""
                version = ""

                afterEvaluate {
                    from(components["release"])
                }
            }
        }
    }

    sourceSets {
        val coroutines = "1.6.4"
        val ktor = "2.0.3"
        val okio = "3.2.0"
        val krypto = "2.2.0"

        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")
                implementation("io.ktor:ktor-client-core:$ktor")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.4.1")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor")
                implementation("io.ktor:ktor-client-content-negotiation:$ktor")
                implementation("io.ktor:ktor-client-logging:$ktor")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }

        val androidMain by getting {
            dependencies {
                implementation("com.squareup.okio:okio:$okio")
                implementation("io.ktor:ktor-client-android:$ktor")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
                implementation("androidx.datastore:datastore:1.0.0")
                implementation("androidx.datastore:datastore-preferences:1.0.0")
                implementation("com.soywiz.korlibs.krypto:krypto-android:$krypto")
            }
        }
        val mockkVersion = "1.13.3"
        val androidUnitTest by getting {
            dependencies {
                implementation("io.mockk:mockk:$mockkVersion")
                implementation("io.kotest:kotest-assertions-core:5.5.4")
            }
        }
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting

        val tvosArm64Main by getting
        val tvosX64Main by getting
        val tvosSimulatorArm64Main by getting

        val iosMain by creating {
            dependencies {
                implementation("com.squareup.okio:okio:$okio")
                implementation("io.ktor:ktor-client-ios:$ktor")
            }
            dependsOn(commonMain)

            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)

            tvosArm64Main.dependsOn(this)
            tvosX64Main.dependsOn(this)
            tvosSimulatorArm64Main.dependsOn(this)
        }

        val jsCoroutine = "1.6.4"
        val jsSerialization = "1.4.0"

        val jsMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$jsCoroutine")
                implementation("io.ktor:ktor-client-js:$ktor")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$jsSerialization")
                implementation("com.soywiz.korlibs.krypto:krypto-js:$krypto")
            }
        }

        val commonMobile by creating {
            dependsOn(commonMain)
            androidMain.dependsOn(this)
            iosMain.dependsOn(this)
        }
    }
}

android {
    defaultConfig {
        aarMetadata {
            minCompileSdk = 23
            minSdk = 23
        }
    }
    compileSdk = 33
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    testOptions {
        animationsDisabled = true
        unitTests.apply {
            isReturnDefaultValues = true
        }
    }
}
I think the issue is related to some web build limitations. I was just wondering if there is any other way to read private files from web.