Skip to content

Commit

Permalink
Apply PR review suggestions:
Browse files Browse the repository at this point in the history
- Deprecate `compose.web.targets` in favor of `compose.platformTypes`
- refactor `configureExperimentalWebApplication` to support multiple k/js targets
  • Loading branch information
eymar committed Sep 25, 2023
1 parent aa41589 commit 3a444ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,9 @@ abstract class ComposeExtension @Inject constructor(
* platformTypes.set(platformTypes.get() - KotlinPlatformType.native)
* ```
*/
@Suppress("MemberVisibilityCanBePrivate")
val platformTypes: SetProperty<KotlinPlatformType> = objects.setProperty(KotlinPlatformType::class.java).apply {
set(KotlinPlatformType.values().toMutableSet())
}

val dependencies = ComposePlugin.Dependencies(project)

/**
* @param platformType - the type of platform(s) which should not have the Compose Compiler plugin applied.
* Removes [platformType] from [platformTypes].
*/
fun excludePlatform(vararg platformType: KotlinPlatformType) {
platformTypes.set(platformTypes.map {
it.removeAll(platformType.toSet())
it
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ internal fun Project.configureExperimental(

if (experimentalExt.web._isApplicationInitialized) {
val webExt = composeExt.extensions.getByType(WebExtension::class.java)
for (target in webExt.targetsToConfigure(project)) {
target.configureExperimentalWebApplication(experimentalExt.web.application)
}
webExt.targetsToConfigure(project)
.configureExperimentalWebApplication(project, experimentalExt.web.application)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@ import org.jetbrains.compose.internal.utils.registerTask
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget

internal fun KotlinJsIrTarget.configureExperimentalWebApplication(app: ExperimentalWebApplication) {
val mainCompilation = compilations.getByName("main")
val unpackedRuntimeDir = project.layout.buildDirectory.dir("compose/skiko-wasm/$targetName")
val taskName = "unpackSkikoWasmRuntime${targetName.uppercaseFirstChar()}"
mainCompilation.defaultSourceSet.resources.srcDir(unpackedRuntimeDir)

val skikoJsWasmRuntimeDependency = skikoVersionProvider(project)
.map { skikoVersion ->
project.dependencies.create("org.jetbrains.skiko:skiko-js-wasm-runtime:$skikoVersion")
}
val skikoJsWasmRuntimeConfiguration = project.configurations.create("COMPOSE_SKIKO_JS_WASM_RUNTIME").defaultDependencies {
it.addLater(skikoJsWasmRuntimeDependency)
internal fun Collection<KotlinJsIrTarget>.configureExperimentalWebApplication(
project: Project,
app: ExperimentalWebApplication
) {
val skikoJsWasmRuntimeConfiguration = project.configurations.create("COMPOSE_SKIKO_JS_WASM_RUNTIME")
val skikoJsWasmRuntimeDependency = skikoVersionProvider(project).map { skikoVersion ->
project.dependencies.create("org.jetbrains.skiko:skiko-js-wasm-runtime:$skikoVersion")
}
val unpackRuntime = project.registerTask<ExperimentalUnpackSkikoWasmRuntimeTask>(taskName) {
skikoRuntimeFiles = skikoJsWasmRuntimeConfiguration
outputDir.set(unpackedRuntimeDir)
skikoJsWasmRuntimeConfiguration.defaultDependencies {
it.addLater(skikoJsWasmRuntimeDependency)
}
project.tasks.named(mainCompilation.processResourcesTaskName).configure { processResourcesTask ->
processResourcesTask.dependsOn(unpackRuntime)
forEach {
val mainCompilation = it.compilations.getByName("main")
val unpackedRuntimeDir = project.layout.buildDirectory.dir("compose/skiko-wasm/${it.targetName}")
val taskName = "unpackSkikoWasmRuntime${it.targetName.uppercaseFirstChar()}"
mainCompilation.defaultSourceSet.resources.srcDir(unpackedRuntimeDir)

val unpackRuntime = project.registerTask<ExperimentalUnpackSkikoWasmRuntimeTask>(taskName) {
skikoRuntimeFiles = skikoJsWasmRuntimeConfiguration
outputDir.set(unpackedRuntimeDir)
}
project.tasks.named(mainCompilation.processResourcesTaskName).configure { processResourcesTask ->
processResourcesTask.dependsOn(unpackRuntime)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ abstract class WebExtension : ExtensionAware {

// public api
@Suppress("unused")
@Deprecated(
"""By default, Compose is applied to all declared K/JS-IR targets.
If you need to not apply Compose for K/JS, please exclude `KotlinPlatformType.js` from `compose.platformTypes`"""
)
fun targets(vararg targets: KotlinTarget) {
check(requestedTargets == null) {
"compose.web.targets() was already set!"
Expand Down Expand Up @@ -53,20 +57,12 @@ abstract class WebExtension : ExtensionAware {
if (mppExt != null) {
val mppTargets = mppExt.targets.asMap.values
val jsIRTargets = mppTargets.filterIsInstanceTo(LinkedHashSet<KotlinJsIrTarget>())

return if (jsIRTargets.size > 1) {
project.logger.error(
"w: Default configuration for Compose for Web is disabled: " +
"multiple Kotlin JS IR targets are defined. " +
"Specify Compose for Web Kotlin targets by using `compose.web.targets()`"
)
emptySet()
} else jsIRTargets
return jsIRTargets
}

val jsExt = project.kotlinJsExtOrNull
if (jsExt != null) {
val target = jsExt.target
val target = jsExt.js()
return if (target is KotlinJsIrTarget) {
setOf(target)
} else {
Expand Down

0 comments on commit 3a444ac

Please sign in to comment.