diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt index 996590487c4..72cf5c78b25 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt +++ b/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> + 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 diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt index e16b8e7677c..ee3c0a52cfc 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeExtension.kt +++ b/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 = 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 = objects.setProperty(KotlinPlatformType::class.java).apply { + set(KotlinPlatformType.values().toMutableSet()) + } + val dependencies = ComposePlugin.Dependencies(project) } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/internal/configureExperimental.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/internal/configureExperimental.kt index bb3f480fb96..ae6b6e55677 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/internal/configureExperimental.kt +++ b/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) } } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/web/internal/configureExperimentalWebApplication.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/web/internal/configureExperimentalWebApplication.kt index fd82075cc75..bf262212538 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/experimental/web/internal/configureExperimentalWebApplication.kt +++ b/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.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(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(taskName) { + skikoRuntimeFiles = skikoJsWasmRuntimeConfiguration + outputDir.set(unpackedRuntimeDir) + } + project.tasks.named(mainCompilation.processResourcesTaskName).configure { processResourcesTask -> + processResourcesTask.dependsOn(unpackRuntime) + } } } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/web/WebExtension.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/web/WebExtension.kt index 571a2792ce1..3f3110980ef 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/web/WebExtension.kt +++ b/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()) - - 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 {