You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Kotlin多平台JS/JVM项目中配置kotlin-dce-js输出至Jar

Integrating kotlin-dce-js Output into JVM Jar in Kotlin MPP

Let's adjust your Gradle configuration to replace the standard webpack output with the optimized build from runDceKotlinJs. Here's the modified setup:

// First, ensure the DCE plugin is applied (add this to your root build.gradle or JS module's build.gradle)
plugins {
    id 'org.jetbrains.kotlin.js' version 'your-kotlin-version'
    id 'org.jetbrains.kotlin.plugin.dce' version 'your-kotlin-version'
}

// Configure JS module with DCE settings (add this to your JS module's build.gradle if not already present)
kotlin {
    js {
        browser {
            binaries.executable()
        }
        dce {
            // Optional: Specify entry points to keep (critical if your JS code is invoked from JVM)
            keep("sample.SampleJsKt")
            // Optional: Keep source maps for debugging
            sourceMaps = true
        }
    }
}

// Update your JVM jar and run tasks
jvmJar {
    // Depend on DCE task instead of raw webpack (DCE will automatically trigger webpack first)
    dependsOn(runDceKotlinJs)
    // Pull optimized JS files from the DCE output directory
    from(runDceKotlinJs.outputDirectory)
}

task run(type: JavaExec, dependsOn: [jvmJar]) {
    group = "application"
    main = "sample.SampleJvmKt"
    classpath(configurations.jvmRuntimeClasspath, jvmJar)
    args = []
}

Key Changes Explained:

  • Plugin Application: We explicitly include the kotlin-plugin.dce plugin to enable dead code elimination for the JS module. Make sure the plugin version matches your Kotlin version exactly.
  • DCE Configuration: The dce block lets you define critical entry points (like functions your JVM code calls via JS interop) to ensure they aren't stripped during optimization. Adjust the keep value to match your actual JS entry points.
  • Task Dependency Update: jvmJar now depends on runDceKotlinJs instead of jsBrowserWebpack. The DCE task automatically runs webpack first, so you don't need to keep the direct webpack dependency.
  • Output Path Adjustment: Instead of pointing to the raw webpack output, we use runDceKotlinJs.outputDirectory to grab the optimized, dead-code-eliminated JS files. This ensures your JVM jar includes only the JS code your application actually uses.

Verification Tip:

After building, check the contents of your JVM jar to confirm the JS files are smaller than the original webpack output. You can also run the run task as usual—your application should behave identically but with a reduced JS footprint.

内容的提问来源于stack exchange,提问作者Mahdi Nouri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:07:58