Skip to content

Commit

Permalink
[Wasm] Output module name for web targets with Provider API
Browse files Browse the repository at this point in the history
^KT-71362 fixed


Co-authored-by: Adam Semenenko <[email protected]>
  • Loading branch information
2 people authored and qodana-bot committed Dec 4, 2024
1 parent aba4910 commit 1618143
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1717,4 +1717,27 @@ class Kotlin2JsIrGradlePluginIT : KGPBaseTest() {
}
}
}

@DisplayName("Changed output module name")
@GradleTest
fun testChangedOutputModuleName(gradleVersion: GradleVersion) {
project("kotlin-js-browser-project", gradleVersion) {
val moduleName = "hello"
subProject("app").buildGradleKts.modify {
it.replace(
"target {",
"""
target {
outputModuleName.set("$moduleName")
""".trimIndent()
)
}

build("assemble") {
assertFileExists(
subProject("app").projectPath.resolve("build/compileSync/js/main/productionExecutable/kotlin/$moduleName.mjs")
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,27 @@ class KotlinWasmGradlePluginIT : KGPBaseTest() {
}
}
}

@DisplayName("Changed output module name")
@GradleTest
fun testChangedOutputModuleName(gradleVersion: GradleVersion) {
project("wasm-browser-simple-project", gradleVersion) {
val moduleName = "hello"
buildGradleKts.modify {
it.replace(
"wasmJs {",
"""
wasmJs {
outputModuleName.set("$moduleName")
""".trimIndent()
)
}

build("assemble") {
assertFileExists(
projectPath.resolve("build/compileSync/wasmJs/main/productionExecutable/kotlin/$moduleName.mjs")
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1851,9 +1851,11 @@ public class org/jetbrains/kotlin/gradle/plugin/mpp/KotlinJsCompilation : org/je
public fun getCompileTaskProvider ()Lorg/gradle/api/tasks/TaskProvider;
public final fun getCompilerOptions ()Lorg/jetbrains/kotlin/gradle/plugin/HasCompilerOptions;
public final fun getNpmAggregatedConfigurationName ()Ljava/lang/String;
public final fun getOutputModuleName ()Lorg/gradle/api/provider/Property;
public final fun getOutputModuleName ()Lorg/gradle/api/provider/Provider;
public fun getProcessResourcesTaskName ()Ljava/lang/String;
public final fun getPublicPackageJsonConfigurationName ()Ljava/lang/String;
public synthetic fun getTarget ()Lorg/jetbrains/kotlin/gradle/plugin/KotlinTarget;
public fun getTarget ()Lorg/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTarget;
public final fun packageJson (Lgroovy/lang/Closure;)V
public final fun packageJson (Lorg/gradle/api/Action;)V
}
Expand Down Expand Up @@ -3447,6 +3449,7 @@ public abstract interface class org/jetbrains/kotlin/gradle/targets/js/dsl/Kotli
public abstract fun getBinaries ()Lorg/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsBinaryContainer;
public abstract fun getCompilations ()Lorg/gradle/api/NamedDomainObjectContainer;
public abstract fun getModuleName ()Ljava/lang/String;
public abstract fun getOutputModuleName ()Lorg/gradle/api/provider/Property;
public abstract fun getTestRuns ()Lorg/gradle/api/NamedDomainObjectContainer;
public abstract fun passAsArgumentToMainFunction (Ljava/lang/String;)V
public abstract fun produceExecutable ()V
Expand Down Expand Up @@ -5001,7 +5004,7 @@ public final class org/jetbrains/kotlin/gradle/targets/js/npm/resolver/KotlinCom
public final fun getProjectResolver ()Lorg/jetbrains/kotlin/gradle/targets/js/npm/resolver/KotlinProjectNpmResolver;
public final fun getPublicPackageJsonTaskHolder ()Lorg/gradle/api/tasks/TaskProvider;
public final fun getRootResolver ()Lorg/jetbrains/kotlin/gradle/targets/js/npm/resolver/KotlinRootNpmResolver;
public final fun getTarget ()Lorg/jetbrains/kotlin/gradle/plugin/KotlinTarget;
public final fun getTarget ()Lorg/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTarget;
public final fun setRootResolver (Lorg/jetbrains/kotlin/gradle/targets/js/npm/resolver/KotlinRootNpmResolver;)V
public fun toString ()Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package org.jetbrains.kotlin.gradle.plugin.mpp
import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.attributes.AttributeContainer
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants
Expand All @@ -21,21 +20,23 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJsOptions
import org.jetbrains.kotlin.gradle.plugin.DeprecatedHasCompilerOptions
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.compilationImpl.KotlinCompilationImpl
import org.jetbrains.kotlin.gradle.targets.js.ir.JsBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsBinaryContainer
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.targets.js.npm.PackageJson
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import org.jetbrains.kotlin.gradle.utils.propertyWithConvention
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import javax.inject.Inject

open class KotlinJsCompilation @Inject internal constructor(
compilation: KotlinCompilationImpl,
) : DeprecatedAbstractKotlinCompilationToRunnableFiles<KotlinJsOptions>(compilation),
HasBinaries<KotlinJsBinaryContainer> {

override val target: KotlinJsIrTarget
get() = super.target as KotlinJsIrTarget

@Deprecated(
"To configure compilation compiler options use 'compileTaskProvider':\ncompilation.compileTaskProvider.configure{\n" +
" compilerOptions {}\n}"
Expand All @@ -51,39 +52,10 @@ open class KotlinJsCompilation @Inject internal constructor(
compilation.target.project.objects.domainObjectSet(JsBinary::class.java)
)

val outputModuleName: Property<String> = compilation.project.objects.propertyWithConvention(buildNpmProjectName())

private fun buildNpmProjectName(): String {
val project = target.project

val compilationName = if (compilation.name != KotlinCompilation.MAIN_COMPILATION_NAME) {
compilation.name
} else null

val rootProjectName = project.rootProject.name

val localName = if (project != project.rootProject) {
(rootProjectName + project.path).replace(":", "-")
} else rootProjectName

val targetName = if (target.name.isNotEmpty() && target.name.toLowerCaseAsciiOnly() != "js") {
target.name
.replace(DECAMELIZE_REGEX) {
it.groupValues
.drop(1)
.joinToString(prefix = "-", separator = "-")
}
.toLowerCaseAsciiOnly()
} else null

return sequenceOf(
localName,
targetName,
compilationName
)
.filterNotNull()
.joinToString("-")
}
val outputModuleName: Provider<String> = target.outputModuleName
.map { targetModuleName ->
buildNpmProjectName(targetModuleName, compilationName)
}

@Deprecated("Use compilationName instead", ReplaceWith("compilationName"))
val compilationPurpose: String get() = compilationName
Expand Down Expand Up @@ -128,7 +100,17 @@ open class KotlinJsCompilation @Inject internal constructor(
}

private companion object {
private val DECAMELIZE_REGEX = "([A-Z])".toRegex()
private fun buildNpmProjectName(targetPart: String, compilationName: String): String {
val filteredCompilationName = if (compilationName != KotlinCompilation.MAIN_COMPILATION_NAME) {
compilationName
} else null

return listOfNotNull(
targetPart,
filteredCompilationName
)
.joinToString("-")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.targets.js.dsl
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.provider.Property
import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.HasConfigurableKotlinCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KOTLIN_JS_DCE_TOOL_DEPRECATION_MESSAGE
Expand Down Expand Up @@ -56,8 +57,15 @@ interface KotlinJsTargetDsl :
HasBinaries<KotlinJsBinaryContainer>,
HasConfigurableKotlinCompilerOptions<KotlinJsCompilerOptions> {

@Deprecated("Use outputModuleName with Provider API instead")
var moduleName: String?

/**
* Represents the name of the output module for a Kotlin/JS and Kotlin/Wasm target.
* This property allows customization of output filenames
*/
val outputModuleName: Property<String>

fun browser() = browser { }
fun browser(body: KotlinJsBrowserDsl.() -> Unit)
fun browser(fn: Action<KotlinJsBrowserDsl>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ sealed class JsIrBinary(
)

val target: KotlinJsIrTarget
get() = compilation.target as KotlinJsIrTarget
get() = compilation.target

val project: Project
get() = target.project
Expand All @@ -159,7 +159,7 @@ sealed class JsIrBinary(
it.libraries.from(project.filesProvider { compilation.runtimeDependencyFiles })
}
configAction.configureTask { task ->
val targetCompilerOptions = (compilation.target as KotlinJsIrTarget).compilerOptions
val targetCompilerOptions = compilation.target.compilerOptions
KotlinJsCompilerOptionsHelper.syncOptionsAsConvention(
targetCompilerOptions,
task.compilerOptions
Expand Down Expand Up @@ -211,7 +211,7 @@ internal fun TaskProvider<BinaryenExec>.configureOptimizeTask(binary: WasmBinary
)
}

val target = binary.compilation.target as KotlinJsIrTarget
val target = binary.compilation.target
val compilation = binary.compilation

if (compilation.isMain() && binary.mode == KotlinJsBinaryMode.PRODUCTION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ package org.jetbrains.kotlin.gradle.targets.js.ir

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.MAIN_COMPILATION_NAME
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetWithTests
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.resources.publication.setUpResourcesVariant
import org.jetbrains.kotlin.gradle.targets.js.JsAggregatingExecutionSource
Expand All @@ -29,7 +26,7 @@ import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsPlugin
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmResolverPlugin
import org.jetbrains.kotlin.gradle.targets.js.typescript.TypeScriptValidationTask
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.registerTask
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
Expand Down Expand Up @@ -66,20 +63,11 @@ constructor(
override var wasmTargetType: KotlinWasmTargetType? = null
internal set

override var moduleName: String? = null
@Deprecated("Use outputModuleName with Provider API instead")
override var moduleName: String?
get() = outputModuleName.get()
set(value) {
field = value
compilations.all { compilation ->
val compilationName = if (compilation.name != MAIN_COMPILATION_NAME) {
compilation.name
} else null

val name = sequenceOf(moduleName, compilationName)
.filterNotNull()
.joinToString("-")

compilation.outputModuleName.set(name)
}
outputModuleName.set(value)
}

override val kotlinComponents: Set<KotlinTargetComponent> by lazy {
Expand Down Expand Up @@ -351,6 +339,35 @@ constructor(
.apply {
configureJsDefaultOptions()
}

internal companion object {
private val DECAMELIZE_REGEX = "([A-Z])".toRegex()

internal fun buildNpmProjectName(project: Project, targetName: String): String {
val rootProjectName = project.rootProject.name

val localName = if (project != project.rootProject) {
(rootProjectName + project.path).replace(":", "-")
} else rootProjectName

val targetPartName = if (targetName.isNotEmpty() && targetName.toLowerCaseAsciiOnly() != "js") {
targetName
.replace(DECAMELIZE_REGEX) {
it.groupValues
.drop(1)
.joinToString(prefix = "-", separator = "-")
}
.toLowerCaseAsciiOnly()
} else null

return sequenceOf(
localName,
targetPartName
)
.filterNotNull()
.joinToString("-")
}
}
}

fun KotlinJsIrTarget.wasmDecamelizedDefaultNameOrNull(): String? = if (platformType == KotlinPlatformType.wasm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinCompilationFactory
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTargetPreset
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinJsIrTargetMetrics
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget.Companion.buildNpmProjectName

@DeprecatedTargetPresetApi
open class KotlinJsIrTargetPreset(
Expand All @@ -29,6 +30,7 @@ open class KotlinJsIrTargetPreset(
override fun instantiateTarget(name: String): KotlinJsIrTarget {
return project.objects.newInstance(KotlinJsIrTarget::class.java, project, platformType).apply {
this.isMpp = this@KotlinJsIrTargetPreset.isMpp
this.outputModuleName.convention(buildNpmProjectName(project, name))
KotlinJsIrTargetMetrics.collectMetrics(
isBrowserConfigured = isBrowserConfigured,
isNodejsConfigured = isNodejsConfigured,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.gradle.plugin.diagnostics.reportDiagnosticOncePerBui
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinCompilationFactory
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTargetPreset
import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget.Companion.buildNpmProjectName
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly

Expand All @@ -26,6 +27,7 @@ class KotlinWasmTargetPreset(
override fun instantiateTarget(name: String): KotlinJsIrTarget {
val irTarget = project.objects.newInstance(KotlinJsIrTarget::class.java, project, KotlinPlatformType.wasm)
irTarget.isMpp = true
irTarget.outputModuleName.convention(buildNpmProjectName(project, name))
irTarget.wasmTargetType = targetType

return irTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NodeJsEnvironmentConfigurator(subTarget: KotlinJsIrSubTarget) :
sourceMapStackTraces = false
}

val inputFile = if ((compilation.target as KotlinJsIrTarget).wasmTargetType == KotlinWasmTargetType.WASI) {
val inputFile = if (compilation.target.wasmTargetType == KotlinWasmTargetType.WASI) {
if (binary is ExecutableWasm && binary.mode == KotlinJsBinaryMode.PRODUCTION) {
dependsOn(binary.optimizeTask)
binary.mainOptimizedFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.gradle.api.file.Directory
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.process.ExecSpec
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.disambiguateName
import org.jetbrains.kotlin.gradle.plugin.mpp.fileExtension
Expand All @@ -20,7 +19,6 @@ import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsPlugin.Companion.kotl
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin.Companion.kotlinNodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinPackageJsonTask
import org.jetbrains.kotlin.gradle.utils.getFile
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import java.io.File
import java.io.Serializable

Expand Down Expand Up @@ -59,7 +57,7 @@ open class NpmProject(@Transient val compilation: KotlinJsIrCompilation) : Seria
}

val target: KotlinJsTargetDsl
get() = compilation.target as KotlinJsTargetDsl
get() = compilation.target

val project: Project
get() = target.project
Expand Down
Loading

0 comments on commit 1618143

Please sign in to comment.