Skip to content

Commit c6f07fd

Browse files
authored
Fix skiko.js unpacking for k/js target (#5105)
K/JS and K/Wasm have differences in the packaging logic, and therefore we need to account for it when unpacking Skiko files. Fixes [CMP-5649](https://youtrack.jetbrains.com/issue/CMP-5649) ## Testing - Added a test, which checks the state of k/js distribution This should be tested by QA
1 parent 3c348bc commit c6f07fd

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/web/internal/configureWebApplication.kt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ import org.gradle.api.artifacts.Configuration
1010
import org.gradle.api.artifacts.ResolvedDependency
1111
import org.gradle.api.artifacts.UnresolvedDependency
1212
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
13+
import org.gradle.api.file.DuplicatesStrategy
1314
import org.gradle.api.provider.Provider
15+
import org.gradle.language.jvm.tasks.ProcessResources
1416
import org.jetbrains.compose.ComposeBuildConfig
1517
import org.jetbrains.compose.ComposeExtension
1618
import org.jetbrains.compose.internal.utils.detachedComposeDependency
1719
import org.jetbrains.compose.internal.utils.registerTask
1820
import org.jetbrains.compose.web.WebExtension
1921
import org.jetbrains.compose.web.tasks.UnpackSkikoWasmRuntimeTask
22+
import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
23+
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
2024
import org.jetbrains.kotlin.gradle.tasks.IncrementalSyncTask
2125

2226
internal fun Project.configureWeb(
@@ -46,13 +50,16 @@ internal fun Project.configureWeb(
4650
}
4751
}
4852

53+
val targets = webExt.targetsToConfigure(project)
54+
4955
// configure only if there is k/wasm or k/js target:
50-
if (webExt.targetsToConfigure(project).isNotEmpty()) {
51-
configureWebApplication(project, shouldRunUnpackSkiko)
56+
if (targets.isNotEmpty()) {
57+
configureWebApplication(targets, project, shouldRunUnpackSkiko)
5258
}
5359
}
5460

5561
internal fun configureWebApplication(
62+
targets: Collection<KotlinJsIrTarget>,
5663
project: Project,
5764
shouldRunUnpackSkiko: Provider<Boolean>
5865
) {
@@ -76,9 +83,29 @@ internal fun configureWebApplication(
7683
outputDir.set(unpackedRuntimeDir)
7784
}
7885

79-
project.tasks.withType(IncrementalSyncTask::class.java) {
80-
it.dependsOn(unpackRuntime)
81-
it.from.from(unpackedRuntimeDir)
86+
targets.forEach { target ->
87+
target.compilations.all { compilation ->
88+
// `wasmTargetType` is available starting with kotlin 1.9.2x
89+
if (target.wasmTargetType != null) {
90+
// Kotlin/Wasm uses ES module system to depend on skiko through skiko.mjs.
91+
// Further bundler could process all files by its own (both skiko.mjs and skiko.wasm) and then emits its own version.
92+
// So that’s why we need to provide skiko.mjs and skiko.wasm only for webpack, but not in the final dist.
93+
compilation.binaries.all {
94+
it.linkSyncTask.configure {
95+
it.dependsOn(unpackRuntime)
96+
it.from.from(unpackedRuntimeDir)
97+
}
98+
}
99+
} else {
100+
// Kotlin/JS depends on Skiko through global space.
101+
// Bundler cannot know anything about global externals, so that’s why we need to copy it to final dist
102+
project.tasks.named(compilation.processResourcesTaskName, ProcessResources::class.java) {
103+
it.from(unpackedRuntimeDir)
104+
it.dependsOn(unpackRuntime)
105+
it.exclude("META-INF")
106+
}
107+
}
108+
}
82109
}
83110
}
84111

gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/GradlePluginTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class GradlePluginTest : GradlePluginTestBase() {
4949
check.taskSuccessful(":compileKotlinJs")
5050
check.taskSuccessful(":compileKotlinWasmJs")
5151
check.taskSuccessful(":wasmJsBrowserDistribution")
52+
check.taskSuccessful(":jsBrowserDistribution")
5253

5354
file("./build/dist/wasmJs/productionExecutable").apply {
5455
checkExists()
@@ -61,6 +62,14 @@ class GradlePluginTest : GradlePluginTestBase() {
6162
// one file is the app wasm file and another one is skiko wasm file with a mangled name
6263
assertEquals(2, distributionFiles.filter { it.endsWith(".wasm") }.size)
6364
}
65+
66+
file("./build/dist/js/productionExecutable").apply {
67+
checkExists()
68+
assertTrue(isDirectory)
69+
val distributionFiles = listFiles()!!.map { it.name }.toList()
70+
assertTrue(distributionFiles.contains("skiko.wasm"))
71+
assertTrue(distributionFiles.contains("skiko.js"))
72+
}
6473
}
6574
}
6675

gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/KotlinCompatibilityTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class KotlinCompatibilityTest : GradlePluginTestBase() {
1616
fun testKotlinMpp_1_9_10() = testMpp("1.9.10")
1717

1818
@Test
19-
fun testKotlinJsMpp_1_9_10() = testJsMpp("1.9.10")
19+
fun testKotlinJsMpp_1_9_24() = testJsMpp("1.9.24")
2020

2121
@Test
2222
fun testKotlinMpp_1_9_20() = testMpp("1.9.20")

0 commit comments

Comments
 (0)