Kotlin/JS project doesn't build properly for production

Hi,

I’m building a web app with Kotlin/JS and React and everything was working fine while running it locally in development mode, but when I built it in production mode to host it in a server suddenly it didn’t work and I got the following error:

Uncaught TypeError: y is not a function
    at Object.r (example-app.js:720)
    at Object.<anonymous> (example-app.js:720)
    at n (bootstrap:19)
    at Object.<anonymous> (example-app.js:712)
    at n (bootstrap:19)
    at Object.<anonymous> (example-app.js:720)
    at n (bootstrap:19)
    at Object.<anonymous> (example-app.js:712)
    at n (bootstrap:19)
    at Object.<anonymous> (example-app.js:712)

The line that seems to fail in the transpiled JS file is:

y(m.Charsets.UTF_8.newEncoder(),"\r\n",0,"\r\n".length)

Although this seems to be minified so it doesn’t provide much help.

Has anyone come across this issue or knows what can I do to debug it and fix it?

In case it helps, here is my build configuration:

build.gradle.kts

plugins {
    kotlin("js")
    kotlin("plugin.serialization")

    id("com.android.library").apply(false)
}

repositories {
    mavenCentral()
    jcenter()

    maven("https://kotlin.bintray.com/kotlin-js-wrappers/")
}

group = "example.app"
version = "0.1.0"

kotlin.target {
    browser()
    useCommonJs()
}

dependencies {
    implementation(kotlin("stdlib-js"))

    // This is a Kotlin multiplatform project
    implementation(project(":app-core"))

    // React, React DOM + Wrappers
    implementation("org.jetbrains:kotlin-react:16.13.0-pre.94-kotlin-1.3.70")
    implementation("org.jetbrains:kotlin-react-dom:16.13.0-pre.94-kotlin-1.3.70")
    implementation(npm("react", "16.13.0"))
    implementation(npm("react-dom", "16.13.0"))

    // Kotlin Styled
    implementation("org.jetbrains:kotlin-styled:1.0.0-pre.94-kotlin-1.3.70")
    implementation(npm("styled-components", "5.0.1"))
    implementation(npm("inline-style-prefixer", "5.1.2"))

    implementation("org.jetbrains:kotlin-react-router-dom:4.3.1-pre.94-kotlin-1.3.70")
    implementation(npm("react-router-dom", "4.3.1"))

    implementation(npm("react-firebaseui", "4.1.0"))
    implementation(npm("firebase", "7.13.2"))

    val rmwcVersion = "5.7.2"
    implementation(npm("@rmwc/theme", rmwcVersion))
    implementation(npm("@rmwc/button", rmwcVersion))
    implementation(npm("@rmwc/icon-button", rmwcVersion))
    implementation(npm("@rmwc/checkbox", rmwcVersion))
    implementation(npm("@rmwc/linear-progress", rmwcVersion))
    implementation(npm("@rmwc/textfield", rmwcVersion))
    implementation(npm("@rmwc/radio", rmwcVersion))
    implementation(npm("@rmwc/chip", rmwcVersion))
    implementation(npm("@rmwc/select", rmwcVersion))
    implementation(npm("@rmwc/icon", rmwcVersion))

    implementation(npm("react-signature-canvas", "1.0.3"))

    implementation(npm("css-loader", "3.4.2"))
    implementation(npm("style-loader", "1.1.3"))
}

settings.gradle.kts

pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
        jcenter()
        google()

        maven("https://plugins.gradle.org/m2/")
    }

    val kotlinVersion = "1.3.70"

    resolutionStrategy {
        eachPlugin {
            val pluginId = requested.id.id
            when {
                pluginId.startsWith("org.jetbrains.kotlin") -> useVersion(kotlinVersion)
                pluginId.startsWith("kotlinx") -> useModule("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion")
                pluginId.startsWith("com.android") -> useModule("com.android.tools.build:gradle:3.5.3")
            }
        }
    }
}

include(":app-core")

rootProject.name = "example-app"

webpack.config.d/css.js

config.resolve.modules.push("processedResources/Js/main");

config.module.rules.push({
    test: /\.css$/,
    use: ['style-loader', 'css-loader']
});

This is the build.gradle.kts file of the multiplatform app-core project I use:

plugins {
    kotlin("multiplatform")
    // Used to create a pod for the iOS lib
    kotlin("native.cocoapods")
    // Used to create an aar for the Android lib
    id("com.android.library")

    kotlin("plugin.serialization")
}

repositories {
    mavenCentral()
    jcenter()
    google()
}

group = "com.example.core"
version = "1.0.0"

// Multiplatform

kotlin {
    android()
    // This is for iPhone emulator, switch here to iosArm64 (or iosArm32) to build library for iPhone device
    iosX64("ios")
    js { browser { } }

    // See https://github.com/Kotlin/kotlinx.coroutines/blob/master/CHANGES.md
    val coroutinesVersion = "1.3.5"
    // See https://github.com/ktorio/ktor/blob/master/CHANGELOG.md
    val ktorVersion = "1.3.2"
    // See https://github.com/Kotlin/kotlinx.serialization/blob/master/CHANGELOG.md
    val kotlinxSerializationVersion = "0.20.0"

    sourceSets {
        commonMain {
            dependencies {
                implementation(kotlin("stdlib-common"))

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion")

                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization:$ktorVersion")

                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlinxSerializationVersion")
            }
        }

        val androidMain by getting {
            dependencies {
                implementation(kotlin("stdlib"))

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion")

                implementation("io.ktor:ktor-client-android:$ktorVersion")
                implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")

                api("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlinxSerializationVersion")

                // Add AndroidX's ViewModel and LiveData
                implementation("androidx.lifecycle:lifecycle-extensions:2.0.0")
//                kapt("androidx.lifecycle:lifecycle-compiler:$lifecycleVersion")
                implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0")
            }
        }

        val iosMain by getting {
            dependencies {
                implementation(kotlin("stdlib"))

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutinesVersion")

                implementation("io.ktor:ktor-client-ios:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization-native:$ktorVersion")

                api("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$kotlinxSerializationVersion")
            }
        }

        val jsMain by getting {
            dependencies {
                implementation(kotlin("stdlib-js"))

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion")

                implementation("io.ktor:ktor-client-js:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization-js:$ktorVersion")

                api("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$kotlinxSerializationVersion")

                // TODO: Remove these once this issue is fixed: https://github.com/ktorio/ktor/issues/961
                api(npm("text-encoding"))
                api(npm("abort-controller"))
            }
        }
    }
}

configurations.create("compileClasspath")

// Android

android {
    compileSdkVersion(29)
    buildToolsVersion("29.0.2")

    defaultConfig {
        minSdkVersion(19)
        targetSdkVersion(29)

        versionCode = 10000
        versionName = "1.0.0"
    }

    // By default the Android Gradle plugin expects to find the Kotlin source files in the "main" and "test" dirs,
    // so we have to redirect it to "androidMain" and "androidTest" ones
    sourceSets {
        getByName("main") {
            manifest.srcFile("src/androidMain/AndroidManifest.xml")
            java.srcDirs("src/androidMain/kotlin")
            res.srcDirs("src/androidMain/res")
        }
        getByName("test") {
            java.srcDirs("src/androidTest/kotlin")
            res.srcDirs("src/androidTest/res")
        }
    }
}

Hi! It looks like https://youtrack.jetbrains.com/issue/KT-36484.
Try to add in your build.gradle file:

browser {
    dceTask {
        keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
    }
}
2 Likes

That fixed it! Thanks so much @ivan.kubyshkin!! :smiley: