From d34fdd82ee4a973028cffbaa0fef60d8d253ca59 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Thu, 19 Sep 2024 12:00:51 +0200 Subject: [PATCH] [JS] Fix webpack input files --- .../kotlin/gradle/Kotlin2JsGradlePluginIT.kt | 18 ++++++++++++ .../testProject/js-per-file/build.gradle.kts | 25 ++++++++++++++++ .../js-per-file/src/jsMain/kotlin/Main.kt | 3 ++ .../src/jsMain/kotlin/SomeModule.kt | 1 + .../targets/js/webpack/KotlinWebpack.kt | 29 +++++++++---------- 5 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/build.gradle.kts create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/Main.kt create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/SomeModule.kt diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt index 70d2afde2b290..2fad49e25afc3 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.PackageJson import org.jetbrains.kotlin.gradle.targets.js.npm.fromSrcPackageJson import org.jetbrains.kotlin.gradle.testbase.* import org.jetbrains.kotlin.gradle.util.replaceText +import org.jetbrains.kotlin.test.TestMetadata import org.junit.jupiter.api.DisplayName import java.util.zip.ZipFile import kotlin.io.path.* @@ -1699,4 +1700,21 @@ class Kotlin2JsIrGradlePluginIT : KGPBaseTest() { } } } + + @DisplayName("Check webpack update with per-file") + @GradleTest + @TestMetadata("js-per-file") + fun testWebpackUpdatePerFile(gradleVersion: GradleVersion) { + project("js-per-file", gradleVersion) { + build("assemble") { + assertTasksExecuted(":jsBrowserProductionWebpack") + } + + projectPath.resolve("src/jsMain/kotlin/Main.kt").replaceText("CUSTOM TEXT", "CUSTOM TEXT 2") + + build("assemble") { + assertTasksExecuted(":jsBrowserProductionWebpack") + } + } + } } diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/build.gradle.kts b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/build.gradle.kts new file mode 100644 index 0000000000000..c53a5eb5765b7 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/build.gradle.kts @@ -0,0 +1,25 @@ +import org.jetbrains.kotlin.gradle.dsl.JsModuleKind +import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink + +plugins { + kotlin("multiplatform") +} + +repositories { + mavenCentral() + mavenLocal() +} + +kotlin { + js { + binaries.executable() + browser { + + } + } +} + +tasks.withType(KotlinJsIrLink::class.java) { + this.compilerOptions.freeCompilerArgs.add("-Xir-per-file") + this.compilerOptions.moduleKind.set(JsModuleKind.MODULE_ES) +} diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/Main.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/Main.kt new file mode 100644 index 0000000000000..94da97339d170 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/Main.kt @@ -0,0 +1,3 @@ +private fun main() { + SomeModule("CUSTOM TEXT") +} diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/SomeModule.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/SomeModule.kt new file mode 100644 index 0000000000000..78d9d4af52533 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/js-per-file/src/jsMain/kotlin/SomeModule.kt @@ -0,0 +1 @@ +class SomeModule(val value: String) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpack.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpack.kt index bc837a206fed4..6ce9c9b1ea35e 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpack.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpack.kt @@ -38,10 +38,6 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.RequiresNpmDependencies import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig.Mode import org.jetbrains.kotlin.gradle.utils.* -import org.jetbrains.kotlin.gradle.utils.archivesName -import org.jetbrains.kotlin.gradle.utils.injected -import org.jetbrains.kotlin.gradle.utils.property -import org.jetbrains.kotlin.gradle.utils.providerWithLazyConvention import java.io.File import javax.inject.Inject @@ -98,18 +94,19 @@ constructor( @get:NormalizeLineEndings val inputFiles: FileTree get() = objects.fileTree() - // in webpack.config.js there is path relative to npmProjectDir (kotlin/.js). - // And we need have relative path in build cache - // That's why we use npmProjectDir with filter instead of just inputFilesDirectory, - // if we would use inputFilesDirectory, we will get in cache just file names, - // and if directory is changed to kotlin2, webpack config will be invalid. - .from(npmProjectDir) - .matching { - it.include { element: FileTreeElement -> - val inputFilesDirectory = inputFilesDirectory.get().asFile - element.file == inputFilesDirectory || - element.file.parentFile == inputFilesDirectory - } + .let { fileTree -> + val inputFilesDirectory = inputFilesDirectory.get().asFile + // in webpack.config.js there is path relative to npmProjectDir (kotlin/.js). + // And we need have relative path in build cache + // That's why we use npmProjectDir with filter instead of just inputFilesDirectory, + // if we would use inputFilesDirectory, we will get in cache just file names, + // and if directory is changed to kotlin2, webpack config will be invalid. + fileTree.from(npmProjectDir) + .matching { + it.include { element: FileTreeElement -> + inputFilesDirectory.isParentOf(element.file) + } + } } @get:Input