[Gradle, JS] Fix destinations of js and wasm dists

^KT-57629 fixed
This commit is contained in:
Ilya Goncharov
2023-04-18 13:34:00 +00:00
committed by Space Team
parent faf212ca04
commit 418132c240
11 changed files with 52 additions and 40 deletions
@@ -362,7 +362,7 @@ class Kotlin2JsIrGradlePluginIT : AbstractKotlin2JsGradlePluginIT(true) {
fun testKotlinJsPackageModuleName(gradleVersion: GradleVersion) { fun testKotlinJsPackageModuleName(gradleVersion: GradleVersion) {
project("kotlin-js-package-module-name", gradleVersion) { project("kotlin-js-package-module-name", gradleVersion) {
build("assemble") { build("assemble") {
assertFileInProjectExists("build/${Distribution.JS_DIST}/productionExecutable/kotlin-js-package-module-name.js") assertFileInProjectExists("build/${Distribution.DIST}/js/productionExecutable/kotlin-js-package-module-name.js")
assertFileInProjectExists("build/js/packages/@foo/bar/kotlin/@foo/bar.js") assertFileInProjectExists("build/js/packages/@foo/bar/kotlin/@foo/bar.js")
} }
} }
@@ -1149,7 +1149,7 @@ abstract class AbstractKotlin2JsGradlePluginIT(protected val irBackend: Boolean)
assertDirectoryInProjectExists("build/js/packages/kotlin-js-browser-app") assertDirectoryInProjectExists("build/js/packages/kotlin-js-browser-app")
if (irBackend) { if (irBackend) {
assertFileInProjectExists("app/build/${Distribution.JS_DIST}/productionExecutable/app.js") assertFileInProjectExists("app/build/${Distribution.DIST}/js/productionExecutable/app.js")
} else { } else {
assertFileInProjectExists("app/build/distributions/app.js") assertFileInProjectExists("app/build/distributions/app.js")
} }
@@ -1175,7 +1175,7 @@ abstract class AbstractKotlin2JsGradlePluginIT(protected val irBackend: Boolean)
) )
if (irBackend) { if (irBackend) {
assertFileInProjectExists("app/build/${Distribution.JS_DIST}/productionExecutable/index.html") assertFileInProjectExists("app/build/${Distribution.DIST}/js/productionExecutable/index.html")
} else { } else {
assertFileInProjectExists("app/build/distributions/index.html") assertFileInProjectExists("app/build/distributions/index.html")
} }
@@ -9,10 +9,9 @@ import com.google.gson.Gson
import com.google.gson.JsonObject import com.google.gson.JsonObject
import org.gradle.util.GradleVersion import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution.Companion.JS_DIST import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution.Companion.DIST
import org.jetbrains.kotlin.gradle.testbase.* import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.DisplayName
import java.util.zip.ZipFile
import kotlin.io.path.reader import kotlin.io.path.reader
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
@@ -32,10 +31,10 @@ abstract class KotlinJsIrLibraryGradlePluginITBase : KGPBaseTest() {
fun testSimpleJsBinaryLibrary(gradleVersion: GradleVersion) { fun testSimpleJsBinaryLibrary(gradleVersion: GradleVersion) {
project("simple-js-library", gradleVersion) { project("simple-js-library", gradleVersion) {
build("build") { build("build") {
assertFileInProjectExists("build/$JS_DIST/productionLibrary/js-library.js") assertFileInProjectExists("build/$DIST/js/productionLibrary/js-library.js")
assertFileInProjectExists("build/$JS_DIST/productionLibrary/package.json") assertFileInProjectExists("build/$DIST/js/productionLibrary/package.json")
assertFileInProjectExists("build/$JS_DIST/productionLibrary/main.js") assertFileInProjectExists("build/$DIST/js/productionLibrary/main.js")
projectPath.resolve("build/$JS_DIST/productionLibrary/package.json").reader() projectPath.resolve("build/$DIST/js/productionLibrary/package.json").reader()
.use { Gson().fromJson(it, JsonObject::class.java) } .use { Gson().fromJson(it, JsonObject::class.java) }
.getAsJsonObject("dependencies") .getAsJsonObject("dependencies")
?.entrySet()?.associate { (k, v) -> k to v.asString } ?.entrySet()?.associate { (k, v) -> k to v.asString }
@@ -52,9 +51,9 @@ abstract class KotlinJsIrLibraryGradlePluginITBase : KGPBaseTest() {
fun testJsBinaryLibraryAndExecutable(gradleVersion: GradleVersion) { fun testJsBinaryLibraryAndExecutable(gradleVersion: GradleVersion) {
project("js-library-with-executable", gradleVersion) { project("js-library-with-executable", gradleVersion) {
build("build") { build("build") {
assertFileInProjectExists("build/$JS_DIST/productionLibrary/js-library.js") assertFileInProjectExists("build/$DIST/js/productionLibrary/js-library.js")
assertFileInProjectExists("build/$JS_DIST/productionLibrary/package.json") assertFileInProjectExists("build/$DIST/js/productionLibrary/package.json")
assertFileInProjectExists("build/$JS_DIST/productionLibrary/main.js") assertFileInProjectExists("build/$DIST/js/productionLibrary/main.js")
} }
} }
} }
@@ -64,11 +63,11 @@ abstract class KotlinJsIrLibraryGradlePluginITBase : KGPBaseTest() {
fun testJsBinaryLibraryAndExecutableForBrowserAndNodejs(gradleVersion: GradleVersion) { fun testJsBinaryLibraryAndExecutableForBrowserAndNodejs(gradleVersion: GradleVersion) {
project("js-library-with-executable-browser-nodejs", gradleVersion) { project("js-library-with-executable-browser-nodejs", gradleVersion) {
build("build") { build("build") {
assertFileInProjectExists("build/$JS_DIST/productionLibrary/js-library.js") assertFileInProjectExists("build/$DIST/js/productionLibrary/js-library.js")
assertFileInProjectExists("build/$JS_DIST/productionLibrary/package.json") assertFileInProjectExists("build/$DIST/js/productionLibrary/package.json")
assertFileInProjectExists("build/$JS_DIST/productionLibrary/main.js") assertFileInProjectExists("build/$DIST/js/productionLibrary/main.js")
assertFileInProjectExists("build/$JS_DIST/productionExecutable/js-library.js") assertFileInProjectExists("build/$DIST/js/productionExecutable/js-library.js")
} }
} }
} }
@@ -18,7 +18,7 @@ kotlin {
sourceSets { sourceSets {
main { main {
dependencies { dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:latest.release") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.3")
implementation(npm("kotlin", "*")) implementation(npm("kotlin", "*"))
} }
} }
@@ -21,6 +21,6 @@ interface Distribution {
val outputDirectory: DirectoryProperty val outputDirectory: DirectoryProperty
companion object { companion object {
const val JS_DIST = "js-dist" const val DIST = "dist"
} }
} }
@@ -32,7 +32,7 @@ sealed class JsIrBinary(
override val mode: KotlinJsBinaryMode override val mode: KotlinJsBinaryMode
) : JsBinary { ) : JsBinary {
override val distribution: Distribution = override val distribution: Distribution =
createDefaultDistribution(compilation.target.project, name) createDefaultDistribution(compilation.target.project, compilation.target.targetName, name)
val linkTaskName: String = linkTaskName() val linkTaskName: String = linkTaskName()
@@ -95,6 +95,7 @@ class Executable(
override val distribution: Distribution = override val distribution: Distribution =
createDefaultDistribution( createDefaultDistribution(
compilation.target.project, compilation.target.project,
compilation.target.targetName,
super.distribution.distributionName super.distribution.distributionName
) )
@@ -114,7 +114,7 @@ constructor(
override val compilation: KotlinJsCompilation = jsCompilation override val compilation: KotlinJsCompilation = jsCompilation
override val name: String = name override val name: String = name
override val mode: KotlinJsBinaryMode = type override val mode: KotlinJsBinaryMode = type
override val distribution: Distribution = createDefaultDistribution(jsCompilation.target.project) override val distribution: Distribution = createDefaultDistribution(jsCompilation.target.project, jsCompilation.target.targetName)
} }
} }
) )
@@ -78,10 +78,10 @@ abstract class PublicPackageJsonTask :
externalDependencies, externalDependencies,
packageJsonHandlers packageJsonHandlers
).let { packageJson -> ).let { packageJson ->
packageJson.main = "${npmProjectName}.js" packageJson.main = "${npmProjectName.get()}.js"
if (jsIrCompilation.get()) { if (jsIrCompilation.get()) {
packageJson.types = "${npmProjectName}.d.ts" packageJson.types = "${npmProjectName.get()}.d.ts"
} }
packageJson.apply { packageJson.apply {
@@ -9,8 +9,8 @@ import org.gradle.api.logging.Logger
import org.jetbrains.kotlin.gradle.targets.js.nodejs.TasksRequirements import org.jetbrains.kotlin.gradle.targets.js.nodejs.TasksRequirements
import org.jetbrains.kotlin.gradle.targets.js.npm.* import org.jetbrains.kotlin.gradle.targets.js.npm.*
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution
import java.io.Serializable
import java.io.File import java.io.File
import java.io.Serializable
class KotlinCompilationNpmResolution( class KotlinCompilationNpmResolution(
var internalDependencies: Collection<InternalDependency>, var internalDependencies: Collection<InternalDependency>,
@@ -88,13 +88,15 @@ class KotlinCompilationNpmResolution(
): PreparedKotlinCompilationNpmResolution { ): PreparedKotlinCompilationNpmResolution {
val rootResolver = npmResolutionManager.parameters.resolution.get() val rootResolver = npmResolutionManager.parameters.resolution.get()
internalDependencies.map { val internalNpmDependencies = internalDependencies
val compilationNpmResolution: KotlinCompilationNpmResolution = rootResolver[it.projectPath][it.compilationName] .map {
compilationNpmResolution.getResolutionOrPrepare( val compilationNpmResolution: KotlinCompilationNpmResolution = rootResolver[it.projectPath][it.compilationName]
npmResolutionManager, compilationNpmResolution.getResolutionOrPrepare(
logger npmResolutionManager,
) logger
} )
}
.flatMap { it.externalNpmDependencies }
val importedExternalGradleDependencies = externalGradleDependencies.mapNotNull { val importedExternalGradleDependencies = externalGradleDependencies.mapNotNull {
npmResolutionManager.parameters.gradleNodeModulesProvider.get().get(it.dependencyName, it.dependencyVersion, it.file) npmResolutionManager.parameters.gradleNodeModulesProvider.get().get(it.dependencyName, it.dependencyVersion, it.file)
} + fileCollectionDependencies.flatMap { dependency -> } + fileCollectionDependencies.flatMap { dependency ->
@@ -109,9 +111,9 @@ class KotlinCompilationNpmResolution(
) )
} }
}.filterNotNull() }.filterNotNull()
val transitiveNpmDependencies = importedExternalGradleDependencies.flatMap { val transitiveNpmDependencies = (importedExternalGradleDependencies.flatMap {
it.dependencies it.dependencies
}.filter { it.scope != NpmDependency.Scope.DEV } } + internalNpmDependencies).filter { it.scope != NpmDependency.Scope.DEV }
val toolsNpmDependencies = tasksRequirements val toolsNpmDependencies = tasksRequirements
.getCompilationNpmRequirements(projectPath, compilationDisambiguatedName) .getCompilationNpmRequirements(projectPath, compilationDisambiguatedName)
@@ -8,17 +8,26 @@ package org.jetbrains.kotlin.gradle.targets.js.subtargets
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property import org.gradle.api.provider.Property
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.distsDirectory import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.distsDirectory
import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution
import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution.Companion.JS_DIST import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution.Companion.DIST
import org.jetbrains.kotlin.gradle.utils.property import org.jetbrains.kotlin.gradle.utils.property
import java.io.File import java.io.File
internal fun createDefaultDistribution(project: Project, name: String? = null) = internal fun createDefaultDistribution(
DefaultDistribution(project, project.objects.property(name)) project: Project,
targetName: String,
name: String? = null
) =
DefaultDistribution(project, project.objects.property(targetName), project.objects.property(name))
internal fun createDefaultDistribution(project: Project, name: Property<String>?) = internal fun createDefaultDistribution(
DefaultDistribution(project, project.objects.property<String>().apply { project: Project,
targetName: String,
name: Property<String>?
) =
DefaultDistribution(project, project.objects.property(targetName), project.objects.property<String>().apply {
if (name != null) { if (name != null) {
value(name) value(name)
} }
@@ -26,6 +35,7 @@ internal fun createDefaultDistribution(project: Project, name: Property<String>?
class DefaultDistribution( class DefaultDistribution(
private val project: Project, private val project: Project,
private val targetName: Property<String>,
override val distributionName: Property<String>, override val distributionName: Property<String>,
) : Distribution { ) : Distribution {
@Deprecated("Use `distributionName` instead", ReplaceWith("distributionName")) @Deprecated("Use `distributionName` instead", ReplaceWith("distributionName"))
@@ -43,6 +53,6 @@ class DefaultDistribution(
} }
override val outputDirectory: DirectoryProperty = project.objects.directoryProperty().convention( override val outputDirectory: DirectoryProperty = project.objects.directoryProperty().convention(
distributionName.flatMap { project.layout.buildDirectory.dir("$JS_DIST/$it") }.orElse(project.distsDirectory) distributionName.flatMap { project.layout.buildDirectory.dir("$DIST/${targetName.get()}/$it") }.orElse(project.distsDirectory)
) )
} }
@@ -48,7 +48,7 @@ abstract class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
private val webpackTaskConfigurations: MutableList<Action<KotlinWebpack>> = mutableListOf() private val webpackTaskConfigurations: MutableList<Action<KotlinWebpack>> = mutableListOf()
private val runTaskConfigurations: MutableList<Action<KotlinWebpack>> = mutableListOf() private val runTaskConfigurations: MutableList<Action<KotlinWebpack>> = mutableListOf()
private val dceConfigurations: MutableList<Action<KotlinJsDce>> = mutableListOf() private val dceConfigurations: MutableList<Action<KotlinJsDce>> = mutableListOf()
private val distribution: Distribution = createDefaultDistribution(project) private val distribution: Distribution = createDefaultDistribution(project, target.targetName)
override val testTaskDescription: String override val testTaskDescription: String
get() = "Run all ${target.name} tests inside browser using karma and webpack" get() = "Run all ${target.name} tests inside browser using karma and webpack"
@@ -38,7 +38,7 @@ internal open class KotlinJsIrLinkConfig(
task.destinationDirectory.convention( task.destinationDirectory.convention(
project.layout.buildDirectory project.layout.buildDirectory
.dir(COMPILE_SYNC) .dir(COMPILE_SYNC)
.map { it.dir(if (compilation.platformType == KotlinPlatformType.wasm) "wasm" else "js") } .map { it.dir(compilation.target.targetName) }
.map { it.dir(compilation.name) } .map { it.dir(compilation.name) }
.map { it.dir(binary.name) } .map { it.dir(binary.name) }
.map { it.dir(NpmProject.DIST_FOLDER) } .map { it.dir(NpmProject.DIST_FOLDER) }