Change default output dir in JS DCE Gradle plugin

See KT-21328

Compiler removes all files from its output directory.
Due to this reason (and some other reasons) it't not a good idea
to put DCE output to `classes` dir.
Therefore we changed output dir to `$buildDir/kotlin-js-min`.
Additionally, we now allow to customize output dir.
In particular, this fixes issues with webpack in multi-project build +
continuous Gradle build.
This commit is contained in:
Alexey Andreev
2017-11-28 16:15:32 +03:00
parent 5672e1f56e
commit 1d48442a5d
6 changed files with 49 additions and 6 deletions
@@ -26,6 +26,7 @@ class K2JSDceArguments : CommonToolArguments() {
valueDescription = "<path>",
description = "Output directory"
)
@GradleOption(DefaultValues.StringNullDefault::class)
var outputDirectory: String? by FreezableVar(null)
@Argument(
@@ -221,6 +221,30 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
fun testDce() {
val project = Project("kotlin2JsDceProject", "2.10", minLogLevel = LogLevel.INFO)
project.build("runRhino") {
assertSuccessful()
val pathPrefix = "mainProject/build/kotlin-js-min/main"
assertFileExists("$pathPrefix/exampleapp.js.map")
assertFileExists("$pathPrefix/examplelib.js.map")
assertFileContains("$pathPrefix/exampleapp.js.map", "\"../../../src/main/kotlin/exampleapp/main.kt\"")
assertFileExists("$pathPrefix/kotlin.js")
assertTrue(fileInWorkingDir("$pathPrefix/kotlin.js").length() < 500 * 1000, "Looks like kotlin.js file was not minified by DCE")
}
}
@Test
fun testDceOutputPath() {
val project = Project("kotlin2JsDceProject", "2.10", minLogLevel = LogLevel.INFO)
project.setupWorkingDir()
File(project.projectDir, "mainProject/build.gradle").modify {
it + "\n" +
"runDceKotlinJs.dceOptions.outputDirectory = \"\${buildDir}/min\"\n" +
"runRhino.args = [\"-f\", \"min/kotlin.js\", \"-f\", \"min/examplelib.js\", \"-f\", \"min/exampleapp.js\"," +
"\"-f\", \"../check.js\"]\n"
}
project.build("runRhino") {
assertSuccessful()
val pathPrefix = "mainProject/build/min"
@@ -245,10 +269,10 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
project.build("runRhino") {
assertSuccessful()
val pathPrefix = "mainProject/build/min"
val pathPrefix = "mainProject/build/kotlin-js-min/main"
assertFileExists("$pathPrefix/exampleapp.js.map")
assertFileExists("$pathPrefix/examplelib.js.map")
assertFileContains("$pathPrefix/exampleapp.js.map", "\"../../src/main/kotlin/exampleapp/main.kt\"")
assertFileContains("$pathPrefix/exampleapp.js.map", "\"../../../src/main/kotlin/exampleapp/main.kt\"")
assertFileExists("$pathPrefix/kotlin.js")
assertTrue(fileInWorkingDir("$pathPrefix/kotlin.js").length() > 1000 * 1000, "Looks like kotlin.js file was minified by DCE")
@@ -32,7 +32,11 @@ task runRhino(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
workingDir = "${buildDir}"
main = 'org.mozilla.javascript.tools.shell.Main'
args = ["-opt", "-1", "-f", "min/kotlin.js", "-f", "min/examplelib.js", "-f", "min/exampleapp.js", "-f", "../check.js"]
args = ["-opt", "-1",
"-f", "kotlin-js-min/main/kotlin.js",
"-f", "kotlin-js-min/main/examplelib.js",
"-f", "kotlin-js-min/main/exampleapp.js",
"-f", "../check.js"]
}
runRhino.dependsOn(runDceKotlinJs)
@@ -5,8 +5,14 @@ package org.jetbrains.kotlin.gradle.dsl
interface KotlinJsDceOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonToolOptions {
/**
* Development mode: don't strip out any code, just copy dependencies and remap source maps
* Development mode: don't strip out any code, just copy dependencies
* Default value: false
*/
var devMode: kotlin.Boolean
/**
* Output directory
* Default value: null
*/
var outputDirectory: kotlin.String?
}
@@ -24,11 +24,17 @@ internal abstract class KotlinJsDceOptionsBase : org.jetbrains.kotlin.gradle.dsl
get() = devModeField ?: false
set(value) { devModeField = value }
private var outputDirectoryField: kotlin.String?? = null
override var outputDirectory: kotlin.String?
get() = outputDirectoryField ?: null
set(value) { outputDirectoryField = value }
internal open fun updateArguments(args: org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments) {
allWarningsAsErrorsField?.let { args.allWarningsAsErrors = it }
suppressWarningsField?.let { args.suppressWarnings = it }
verboseField?.let { args.verbose = it }
devModeField?.let { args.devMode = it }
outputDirectoryField?.let { args.outputDirectory = it }
}
}
@@ -37,4 +43,5 @@ internal fun org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments.fillDefa
suppressWarnings = false
verbose = false
devMode = false
outputDirectory = null
}
@@ -43,7 +43,7 @@ class KotlinJsDcePlugin : Plugin<Project> {
}
project.afterEvaluate {
val outputDir = File(kotlinTask.outputFile).parentFile
val outputDir = File(File(project.buildDir, DEFAULT_OUT_DIR), sourceSet.name)
val configuration = project.configurations.findByName(sourceSet.compileConfigurationName)
val dceInputTrees = listOf(project.fileTree(kotlinTask.outputFile)) + configuration.map { project.fileTree(it) }
@@ -51,7 +51,7 @@ class KotlinJsDcePlugin : Plugin<Project> {
with (dceTask) {
classpath = sourceSet.compileClasspath
destinationDir = File(outputDir, "min")
destinationDir = dceTask.dceOptions.outputDirectory?.let { File(it) } ?: outputDir
source(dceInputFiles)
}
}
@@ -60,5 +60,6 @@ class KotlinJsDcePlugin : Plugin<Project> {
companion object {
private const val TASK_SUFFIX = "kotlinJs"
private const val DCE_TASK_PREFIX = "runDce"
private const val DEFAULT_OUT_DIR = "kotlin-js-min"
}
}