waterstopper
waterstopper

Reputation: 588

Specify module kind for kotlin/js compiler in multiplatform project

As stated in here, it is possible to compile Kotlin/JS to different modules. However, proposed solution doesn't work. When trying to add this block of code:

tasks.named<KotlinJsCompile>("compileKotlinJs").configure {
    compilerOptions.moduleKind.set(org.jetbrains.kotlin.gradle.dsl.JsModuleKind.MODULE_COMMONJS)
}

Gradle says Unresolved reference: compilerOptions.

I also found another documentation in which a different approach is used for compiler options configuration:

tasks.named("compileKotlin", org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask::class.java) {
    compilerOptions {
        freeCompilerArgs.add("-Xexport-kdoc")
    }
    // here compilerOptinons.moduleKind throws unresolved reference
}

In this block of code I do manage to get compilerOptions, but it doesn't contain moduleKind, even if I set task name to compileKotlinJs.

How can I set JS moduleKind in kotlin multiplatform?

My complete build.gradle.kts:

import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask

plugins {
    kotlin("multiplatform") version "1.8.10"
}

group = "llesha.project"
version = "0.5"

repositories {
    mavenLocal()
    mavenCentral()
}

kotlin {
    jvm {
        jvmToolchain(11)
        withJava()
    }
    js(IR) {
        browser {
            commonWebpackConfig {
                cssSupport {
                    enabled.set(false)
                }
            }
            webpackTask {
                output.libraryTarget = "commonjs2"
            }
        }
        binaries.executable()
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val jvmMain by getting
        val jvmTest by getting
        val jsMain by getting
        val jsTest by getting
    }
}

Upvotes: 3

Views: 263

Answers (0)

Related Questions