diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt index 71302b5fcf9..f22d2d3353e 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt @@ -16,8 +16,16 @@ package org.jetbrains.kotlin.gradle.tasks +import org.gradle.api.file.FileCollection import org.gradle.api.file.FileTree -import org.gradle.api.tasks.* +import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.TaskAction +import org.gradle.work.ChangeType +import org.gradle.work.Incremental +import org.gradle.work.InputChanges +import org.jetbrains.kotlin.cli.common.arguments.DevModeOverwritingStrategies import org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments import org.jetbrains.kotlin.cli.js.dce.K2JSDce import org.jetbrains.kotlin.compilerRunner.runToolInSeparateProcess @@ -65,21 +73,52 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool(), Kotl @Input var jvmArgs = mutableListOf() + @Incremental + override fun getClasspath(): FileCollection { + return super.getClasspath() + } + private val buildDir by lazy { project.buildDir } + private val isDevMode + get() = dceOptions.devMode || "-dev-mode" in dceOptions.freeCompilerArgs + + private val isExplicitDevModeAllStrategy + get() = strategyAllArg in dceOptions.freeCompilerArgs || + strategyOlderArg !in dceOptions.freeCompilerArgs && + System.getProperty("kotlin.js.dce.devmode.overwriting.strategy") == DevModeOverwritingStrategies.ALL + @TaskAction - fun performDce() { - val inputFiles = (listOf(source) + classpath + fun performDce(inputChanges: InputChanges) { + // in case of explicit `all` strategy do not perform incremental copy + val shouldPerformIncrementalCopy = isDevMode && !isExplicitDevModeAllStrategy + + val classpathFiles = if (shouldPerformIncrementalCopy) { + inputChanges.getFileChanges(classpath) + .filter { it.changeType == ChangeType.MODIFIED || it.changeType == ChangeType.ADDED } + .map { it.file } + } else { + classpath + } + val inputFiles = (listOf(source) + classpathFiles .filter { !kotlinFilesOnly || isDceCandidate(it) } .map { objects.fileCollection().from(it).asFileTree }) .reduce(FileTree::plus) .files.map { it.path } - val outputDirArgs = arrayOf("-output-dir", destinationDir.path) + val outputDirArgs = arrayOf("-output-dir", destinationDirectory.get().asFile.path) - val argsArray = serializedCompilerArguments.toTypedArray() + val processedSerializedArgs = if (shouldPerformIncrementalCopy) { + var shouldAddStrategyAllArgument = true + val processedArgs = serializedCompilerArguments + .map { if (it == strategyOlderArg) strategyAllArg.also { shouldAddStrategyAllArgument = false } else it } + if (shouldAddStrategyAllArgument) processedArgs + strategyAllArg else processedArgs + } else { + serializedCompilerArguments + } + val argsArray = processedSerializedArgs.toTypedArray() val log = GradleKotlinLogger(logger) val allArgs = argsArray + outputDirArgs + inputFiles @@ -93,7 +132,6 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool(), Kotl jvmArgs ) throwGradleExceptionIfError(exitCode) - } private fun isDceCandidate(file: File): Boolean { @@ -107,4 +145,9 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool(), Kotl return File("${file.canonicalPathWithoutExtension()}.meta.js").exists() } + + companion object { + const val strategyAllArg = "-Xdev-mode-overwriting-strategy=${DevModeOverwritingStrategies.ALL}" + const val strategyOlderArg = "-Xdev-mode-overwriting-strategy=${DevModeOverwritingStrategies.OLDER}" + } } \ No newline at end of file