Skip to content

Erikvv/kotlin-js-code-splitting

Repository files navigation

This repository contains a minimal example of how to do code splitting in Kotlin/JS.

To build: ./gradlew build

To run in the Node.js repl:

node

const mainModule = await import(
    "./build/kotlin-webpack/js/productionExecutable/codesplitting.js"
)
mainModule.main()

What is happening?

Main.kt asynchronously imports fun chair from Chair.kt. Code splitting is taking placing: there are two JavaScript files.

Main.kt directly imports fun kofi from Kofi.kt. No code splitting takes place, the code ends up in the same JavaScript file.

Configuration

Essential elements needed to make this work:

// build.gradle.kts
kotlin {
    compilerOptions {
        // Compile each .kt file to a separate .mjs file
        compilerOptions.freeCompilerArgs.add("-Xir-per-file")
    }
}
// Chair.kt
@JsExport
fun chair() {
    /* ... */
}
// Main.kt
importAsync<ChairModule>("./Chair.export.mjs")
    .then { it.chair() }
// webpack.config.d/codesplitting.js
config.entry = {
    main: require('path').resolve(
        __dirname, 
        "kotlin/codesplitting/com/zenmo/Main.export.mjs"
    )
}

About

Minimal example of how to do code splitting in Kotlin/JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published