Browse Source

Add API to not apply the Compose Compiler plugin (#3722)

* Add API to not apply the Compose Compiler plugin

* avoid eager initialization

* Update gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt

Co-authored-by: Igor Demin <igordmn@users.noreply.github.com>

* Update gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt

Co-authored-by: Igor Demin <igordmn@users.noreply.github.com>

* Apply PR review suggestions:
- Deprecate `compose.web.targets` in favor of `compose.platformTypes`
- refactor `configureExperimentalWebApplication` to support multiple k/js targets

---------

Co-authored-by: Igor Demin <igordmn@users.noreply.github.com>
try_three_js
Oleksandr Karpovich 8 months ago committed by GitHub
parent
commit
5ad17128f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt
  2. 14
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt
  3. 5
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/internal/configureExperimental.kt
  4. 39
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/web/internal/configureExperimentalWebApplication.kt
  5. 16
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/web/WebExtension.kt

19
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt

@ -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)
@ -27,6 +29,8 @@ class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion())
}
applicableForPlatformTypes = composeExt.platformTypes
collectUnsupportedCompilerPluginUsages(target)
}
}
@ -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)
else -> applicableTo.contains(type)
}
}
private fun isApplicableJsTarget(kotlinTarget: KotlinTarget): Boolean {
if (kotlinTarget !is KotlinJsIrTarget) return false

14
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt

@ -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(
@ -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)
}

5
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/internal/configureExperimental.kt

@ -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)
}
}

39
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/web/internal/configureExperimentalWebApplication.kt

@ -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)
}
}
}

16
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/web/WebExtension.kt

@ -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!"
@ -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 {

Loading…
Cancel
Save