Skip to content

Commit

Permalink
Support org.jetbrains.kotlin.js plugin (#1851)
Browse files Browse the repository at this point in the history
Resolves #1821
  • Loading branch information
AlexeyTsvetkov committed Feb 16, 2022
1 parent 1c1f826 commit c1dca65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
Expand Up @@ -137,17 +137,6 @@ class ComposePlugin : Plugin<Project> {
}
}
}

checkAndWarnAboutUsingJsPlugin(project)
}

private fun checkAndWarnAboutUsingJsPlugin(project: Project) {
val msg = "'$COMPOSE_PLUGIN_ID' plugin is not compatible with '$KOTLIN_JS_PLUGIN_ID' plugin. " +
"Use '$KOTLIN_MPP_PLUGIN_ID' instead"

project.plugins.withId(KOTLIN_JS_PLUGIN_ID) {
project.logger.error(msg)
}
}

class RedirectAndroidVariants : ComponentMetadataRule {
Expand Down
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.tasks.SourceSetContainer
import org.gradle.util.GradleVersion
import org.jetbrains.compose.ComposeExtension
import org.jetbrains.compose.web.WebExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension

internal val Project.composeExt: ComposeExtension?
Expand All @@ -26,6 +27,9 @@ internal val Project.mppExt: KotlinMultiplatformExtension
internal val Project.mppExtOrNull: KotlinMultiplatformExtension?
get() = extensions.findByType(KotlinMultiplatformExtension::class.java)

internal val Project.kotlinJsExtOrNull: KotlinJsProjectExtension?
get() = extensions.findByType(KotlinJsProjectExtension::class.java)

internal val Project.javaSourceSets: SourceSetContainer
get() = if (GradleVersion.current() < GradleVersion.version("7.1")) {
convention.getPlugin(JavaPluginConvention::class.java).sourceSets
Expand Down
Expand Up @@ -7,8 +7,10 @@ package org.jetbrains.compose.web

import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.jetbrains.compose.internal.kotlinJsExtOrNull
import org.jetbrains.compose.internal.mppExt
import org.jetbrains.compose.internal.mppExtOrNull
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget

Expand Down Expand Up @@ -46,16 +48,36 @@ abstract class WebExtension : ExtensionAware {
}

private fun defaultJsTargetsToConfigure(project: Project): Set<KotlinJsIrTarget> {
val mppTargets = project.mppExtOrNull?.targets?.asMap?.values ?: emptySet()
val jsIRTargets = mppTargets.filterIsInstanceTo(LinkedHashSet<KotlinJsIrTarget>())

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

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
}

val jsExt = project.kotlinJsExtOrNull
if (jsExt != null) {
val target = jsExt.target
return if (target is KotlinJsIrTarget) {
setOf(target)
} else {
project.logger.error(
"w: Default configuration for Compose for Web is disabled: " +
"Compose for Web does not support legacy (non-IR) JS targets"
)
emptySet()
}
}

return emptySet()
}
}

0 comments on commit c1dca65

Please sign in to comment.