Skip to content

Commit

Permalink
[JS] Fix webpack input files
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgonmic authored and teamcity committed Sep 20, 2024
1 parent d6a78dd commit d34fdd8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
private fun main() {
SomeModule("CUSTOM TEXT")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class SomeModule(val value: String)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -98,18 +94,19 @@ constructor(
@get:NormalizeLineEndings
val inputFiles: FileTree
get() = objects.fileTree()
// in webpack.config.js there is path relative to npmProjectDir (kotlin/<module>.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/<module>.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
Expand Down

0 comments on commit d34fdd8

Please sign in to comment.