CLOVIS
08/23/2025, 1:09 PMkotlin {
js {
compilerOptions {
target.set("es2015")
}
}
}
How can I get which target is set from within a Gradle plugin?
The only thing I see is project.kotlinExtension.sourceSets but that doesn't seem to have that option.mbonnin
08/23/2025, 1:14 PMkotlinExtension to a KotlinMultiplatformExtension or something similar, you should get the targetsCLOVIS
08/23/2025, 1:15 PMmbonnin
08/23/2025, 1:16 PMextensions.getByType(KotlinMultiplatformExtension::class.java).targetsmbonnin
08/23/2025, 1:16 PM.configureEach {} to “react” to targets being addedCLOVIS
08/23/2025, 1:20 PMval targets = project.extensions.getByType(KotlinMultiplatformExtension::class.java).targets
targets.named { it == "js" }.configureEach {
this as KotlinJsIrTarget
val target = this.compilerOptions.target
println("Target ${target.get()}")
}mbonnin
08/23/2025, 1:22 PMnamed may fail depending the order of when your plugin gets called (.gradle.kts is imperative)mbonnin
08/23/2025, 1:23 PMtargets.configureEach {
if (name != "js") return@configureEach
this as KotlinJsIrTarget
val target = this.compilerOptions.target
println("Target ${target.get()}")
}CLOVIS
08/23/2025, 1:23 PM.named should be lazy, no? 🤔 It returns a NamedDomainObjectCollectionmbonnin
08/23/2025, 1:23 PMmbonnin
08/23/2025, 1:25 PMmbonnin
08/23/2025, 1:25 PMtasks.named("foo") returns a TaskProvidermbonnin
08/23/2025, 1:25 PMmbonnin
08/23/2025, 1:26 PMhfhbd
08/23/2025, 1:37 PMtargets.withType<KotlinJsIrTarget>().configureEach {?CLOVIS
08/23/2025, 1:38 PMhfhbd
08/23/2025, 1:39 PMconfigureEach should be called twice, it is lazyCLOVIS
08/23/2025, 1:52 PMtarget.set("es2015") , I need to add an additional dependency
• but by the time the Kotlin plugin has initialized itself, it's forbidden to touch dependenciesCLOVIS
08/23/2025, 2:00 PMException in thread "DefaultSharedObjectDecoder reader thread" org.gradle.api.GradleException: Could not load the value of field `collection` of `kotlin.collections.builders.SerializedCollection` bean found in field `_byTask` of `org.jetbrains.kotlin.gradle.targets.js.nodejs.TasksRequirements` bean found in field `tasksRequirements` of `org.jetbrains.kotlin.gradle.targets.js.npm.resolver.KotlinCompilationNpmResolution` bean found in field `byCompilation` of `org.jetbrains.kotlin.gradle.targets.js.npm.resolved.KotlinProjectNpmResolution` bean found in field `v` of `java.util.Collections$SingletonMap` bean found in field `projects` of `org.jetbrains.kotlin.gradle.targets.js.npm.resolved.KotlinRootNpmResolution` bean found in field `__resolution__` of `org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$Parameters_Decorated` bean found in Gradle runtime.ephemient
08/23/2025, 3:00 PMkotlin.targets.withType<KotlinJsIrTarget>().configureEach {
configurations.getByName("${name}Implementation").withDependencies {
if (compilerOptions.target.get() == "es2015") {
add(dependencies.create("group:module:version"))
}
}
}
does that work?CLOVIS
08/23/2025, 3:52 PMKotlinJsIrTarget.configurationsCLOVIS
08/23/2025, 4:30 PMproject.tasks.withType<org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink> {
println(compilerOptions.moduleKind.get())
}hfhbd
08/23/2025, 4:36 PMephemient
08/23/2025, 4:48 PMproject.configurations, etc.ephemient
08/23/2025, 4:52 PMwithDependencies is the last opportunity to add dependencies so if it isn't ready by then, it's hopelessVampire
08/24/2025, 8:21 AMtasks and all others have named("..."), tasks and all others have named { ... }, and it is the same whether it is about tasks or not. named("...") requires the thing to be registered already and failed if it is not there. named { ... } is reactive and will not fail if it is never added.mbonnin
08/24/2025, 10:46 AMVampire
08/24/2025, 12:58 PMnamed { ... } should even behave more different. It effectively is currently just a shorthand for matching { it.name... }, but was intended to be even more configuration avoiding, only realizing the name-matching elements, that just was not implemented correctly.CLOVIS
08/24/2025, 1:02 PMVampire
08/25/2025, 6:45 AMhfhbd
08/25/2025, 9:47 AMVampire
08/25/2025, 10:35 AM