Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to not apply the Compose Compiler plugin #3722

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget

class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
private lateinit var composeCompilerArtifactProvider: ComposeCompilerArtifactProvider
private lateinit var applicableForPlatformTypes: Provider<Set<KotlinPlatformType>>


override fun apply(target: Project) {
super.apply(target)
Expand All @@ -27,6 +29,8 @@ class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion())
}

applicableForPlatformTypes = composeExt.platformTypes

collectUnsupportedCompilerPluginUsages(target)
}
}
Expand Down Expand Up @@ -62,15 +66,14 @@ class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
override fun getPluginArtifactForNative(): SubpluginArtifact =
composeCompilerArtifactProvider.compilerHostedArtifact

override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean =
when (kotlinCompilation.target.platformType) {
KotlinPlatformType.common -> true
KotlinPlatformType.jvm -> true
KotlinPlatformType.js -> isApplicableJsTarget(kotlinCompilation.target)
KotlinPlatformType.androidJvm -> true
KotlinPlatformType.native -> true
KotlinPlatformType.wasm -> false
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean {
val applicableTo = applicableForPlatformTypes.get()

return when (val type = kotlinCompilation.target.platformType) {
KotlinPlatformType.js -> isApplicableJsTarget(kotlinCompilation.target) && applicableTo.contains(type)
igordmn marked this conversation as resolved.
Show resolved Hide resolved
else -> applicableTo.contains(type)
}
}

private fun isApplicableJsTarget(kotlinTarget: KotlinTarget): Boolean {
if (kotlinTarget !is KotlinJsIrTarget) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.jetbrains.compose.internal.utils.nullableProperty
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import javax.inject.Inject

abstract class ComposeExtension @Inject constructor(
Expand Down Expand Up @@ -41,5 +43,17 @@ abstract class ComposeExtension @Inject constructor(
*/
val kotlinCompilerPluginArgs: ListProperty<String> = objects.listProperty(String::class.java)

/**
* A set of kotlin platform types to which the Compose plugin will be applied.
* By default, it contains all KotlinPlatformType values.
* It can be used to disable the Compose plugin for one or more targets:
* ```
* platformTypes.set(platformTypes.get() - KotlinPlatformType.native)
* ```
*/
val platformTypes: SetProperty<KotlinPlatformType> = objects.setProperty(KotlinPlatformType::class.java).apply {
set(KotlinPlatformType.values().toMutableSet())
}

val dependencies = ComposePlugin.Dependencies(project)
}
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