Skip to content

Commit

Permalink
CfW: Prevent UnpackSkikoWasmRuntime task execution when it's not need…
Browse files Browse the repository at this point in the history
…ed (#4824)

Fixes: #4823

In #4796 we
intentionally started to configure the web app for all k/js and k/wasm
targets. The configuration involves adding a dependency on skiko-wasm
runtime and unpacking it.
Some projects don't need skiko-wasm-runtime (like those based on
compose.html or just compose.runtime).

**Solution:**
We check if there is a dependency on org.jetbrains.compose.ui libraries
(including transitive dependencies). If we find it, then we enable
skikoUnpack task. Otherwise it's disabled.

## Testing
- Build the gradle plugin locally (with this change)
- Used it in our html landing example:
https://github.com/JetBrains/compose-multiplatform/blob/master/examples/html/landing
- Run `./gradlew jsBrowserDistribution`, check
`.../compose-multiplatform/examples/html/landing/build/dist/js/productionExecutable`
and see NO skiko.* files added there
- Then add `implementation(compose.foundation)` dependency in
build.gradle.jts and run `./gradlew clean jsBrowserDistribution` again -
the build/dist contains skiko.* now

(cherry picked from commit 30164c5)
  • Loading branch information
eymar committed May 17, 2024
1 parent b5daa4d commit ce89665
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,34 @@ internal fun Project.configureWeb(
composeExt: ComposeExtension,
) {
val webExt = composeExt.extensions.getByType(WebExtension::class.java)

// here we check all dependencies (including transitive)
// If there is compose.ui, then skiko is required!
val shouldRunUnpackSkiko = project.provider {
var dependsOnComposeUi = false
project.configurations.matching { configuration ->
val isWasmOrJs = configuration.name.contains("js", true) ||
configuration.name.contains("wasm", true)

configuration.isCanBeResolved && isWasmOrJs
}.all { configuration ->
val match = configuration.incoming.artifacts.resolvedArtifacts.get().any { artifact ->
artifact.id.componentIdentifier.toString().contains("org.jetbrains.compose.ui:ui:")
}

dependsOnComposeUi = dependsOnComposeUi || match
}
dependsOnComposeUi
}

// configure only if there is k/wasm or k/js target:
webExt.targetsToConfigure(project)
.configureWebApplication(project)
.configureWebApplication(project, shouldRunUnpackSkiko)
}

internal fun Collection<KotlinJsIrTarget>.configureWebApplication(
project: Project
project: Project,
shouldRunUnpackSkiko: Provider<Boolean>
) {
val skikoJsWasmRuntimeConfiguration = project.configurations.create("COMPOSE_SKIKO_JS_WASM_RUNTIME")
val skikoJsWasmRuntimeDependency = skikoVersionProvider(project).map { skikoVersion ->
Expand All @@ -47,6 +68,10 @@ internal fun Collection<KotlinJsIrTarget>.configureWebApplication(
testCompilation.defaultSourceSet.resources.srcDir(unpackedRuntimeDir)

val unpackRuntime = project.registerTask<UnpackSkikoWasmRuntimeTask>(taskName) {
onlyIf {
shouldRunUnpackSkiko.get()
}

skikoRuntimeFiles = skikoJsWasmRuntimeConfiguration
outputDir.set(unpackedRuntimeDir)
}
Expand Down

0 comments on commit ce89665

Please sign in to comment.