[Gradle, JS] Move common part of webpack configuration to separate fun
^KT-32273 fixed ^KT-36451 fixed ^KT-37258 fixed
This commit is contained in:
+35
-21
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,9 @@ import org.gradle.api.Task
|
||||
import org.gradle.api.tasks.Copy
|
||||
import org.gradle.language.base.plugins.LifecycleBasePlugin
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJsDce
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.*
|
||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
|
||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
||||
import org.jetbrains.kotlin.gradle.targets.js.subtargets.BrowserDistribution
|
||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
||||
@@ -75,17 +77,14 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
RUN_TASK_NAME
|
||||
)
|
||||
) {
|
||||
it.dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
binary.linkTask,
|
||||
target.project.tasks.getByName(compilation.processResourcesTaskName)
|
||||
it.commonConfigure(
|
||||
compilation = compilation,
|
||||
binary = binary,
|
||||
nodeJs = nodeJs
|
||||
)
|
||||
|
||||
it.configureOptimization(type)
|
||||
|
||||
it.bin = "webpack-dev-server/bin/webpack-dev-server.js"
|
||||
it.compilation = compilation
|
||||
it.entry = binary.linkTask.map { it.outputFile }.get()
|
||||
it.description = "start ${type.name.toLowerCase()} webpack dev server"
|
||||
|
||||
it.devServer = KotlinWebpackConfig.DevServer(
|
||||
@@ -94,10 +93,6 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
)
|
||||
|
||||
it.outputs.upToDateWhen { false }
|
||||
|
||||
commonRunConfigurations.forEach { configure ->
|
||||
it.configure()
|
||||
}
|
||||
}
|
||||
|
||||
if (type == KotlinJsBinaryType.DEVELOPMENT) {
|
||||
@@ -141,22 +136,19 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
WEBPACK_TASK_NAME
|
||||
)
|
||||
) {
|
||||
it.commonConfigure(
|
||||
compilation = compilation,
|
||||
binary = binary,
|
||||
nodeJs = nodeJs
|
||||
)
|
||||
|
||||
it.dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
binary.linkTask,
|
||||
distributeResourcesTask
|
||||
)
|
||||
|
||||
it.configureOptimization(type)
|
||||
|
||||
it.compilation = compilation
|
||||
it.entry = binary.linkTask.map { it.outputFile }.get()
|
||||
it.description = "build webpack ${type.name.toLowerCase()} bundle"
|
||||
it._destinationDirectory = distribution.directory
|
||||
|
||||
commonWebpackConfigurations.forEach { configure ->
|
||||
it.configure()
|
||||
}
|
||||
}
|
||||
|
||||
if (type == KotlinJsBinaryType.PRODUCTION) {
|
||||
@@ -176,6 +168,28 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
}
|
||||
}
|
||||
|
||||
private fun KotlinWebpack.commonConfigure(
|
||||
compilation: KotlinJsCompilation,
|
||||
binary: Executable,
|
||||
nodeJs: NodeJsRootExtension
|
||||
) {
|
||||
val type = binary.type
|
||||
|
||||
dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
binary.linkTask,
|
||||
target.project.tasks.getByName(compilation.processResourcesTaskName)
|
||||
)
|
||||
|
||||
configureOptimization(type)
|
||||
|
||||
entryProperty.set(binary.linkTask.map { it.outputFile })
|
||||
|
||||
commonRunConfigurations.forEach { configure ->
|
||||
configure()
|
||||
}
|
||||
}
|
||||
|
||||
private fun KotlinWebpack.configureOptimization(kind: KotlinJsBinaryType) {
|
||||
mode = getByKind(
|
||||
kind = kind,
|
||||
|
||||
+55
-47
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.*
|
||||
import org.jetbrains.kotlin.gradle.targets.js.ir.executeTaskBaseName
|
||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
|
||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
||||
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
|
||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
||||
@@ -95,8 +96,6 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
val project = compilation.target.project
|
||||
val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
|
||||
|
||||
val compileKotlinTask = compilation.compileKotlinTask
|
||||
|
||||
val commonRunTask = registerSubTargetTask<Task>(disambiguateCamelCased(RUN_TASK_NAME)) {}
|
||||
|
||||
compilation.binaries
|
||||
@@ -109,13 +108,14 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
RUN_TASK_NAME
|
||||
)
|
||||
) {
|
||||
it.dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
target.project.tasks.named(compilation.processResourcesTaskName)
|
||||
it.commonConfigure(
|
||||
compilation = compilation,
|
||||
dceTaskProvider = dceTaskProvider,
|
||||
devDceTaskProvider = devDceTaskProvider,
|
||||
type = type,
|
||||
nodeJs = nodeJs
|
||||
)
|
||||
|
||||
it.configureOptimization(type)
|
||||
|
||||
it.bin = "webpack-dev-server/bin/webpack-dev-server.js"
|
||||
it.compilation = compilation
|
||||
it.description = "start ${type.name.toLowerCase()} webpack dev server"
|
||||
@@ -126,24 +126,6 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
)
|
||||
|
||||
it.outputs.upToDateWhen { false }
|
||||
|
||||
val actualDceTaskProvider = when (type) {
|
||||
KotlinJsBinaryType.PRODUCTION -> dceTaskProvider
|
||||
KotlinJsBinaryType.DEVELOPMENT -> devDceTaskProvider
|
||||
}
|
||||
|
||||
it.entryProperty.set(
|
||||
actualDceTaskProvider.map {
|
||||
it.destinationDir
|
||||
.resolve(compileKotlinTask.outputFile.name)
|
||||
}
|
||||
)
|
||||
|
||||
it.resolveFromModulesFirst = true
|
||||
|
||||
commonRunConfigurations.forEach { configure ->
|
||||
it.configure()
|
||||
}
|
||||
}
|
||||
|
||||
if (type == KotlinJsBinaryType.DEVELOPMENT) {
|
||||
@@ -163,8 +145,6 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
val project = compilation.target.project
|
||||
val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
|
||||
|
||||
val compileKotlinTask = compilation.compileKotlinTask
|
||||
|
||||
val processResourcesTask = target.project.tasks.named(compilation.processResourcesTaskName)
|
||||
|
||||
val distributeResourcesTask = registerSubTargetTask<Copy>(
|
||||
@@ -190,9 +170,15 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
|
||||
)
|
||||
) {
|
||||
it.commonConfigure(
|
||||
compilation = compilation,
|
||||
dceTaskProvider = dceTaskProvider,
|
||||
devDceTaskProvider = devDceTaskProvider,
|
||||
type = type,
|
||||
nodeJs = nodeJs
|
||||
)
|
||||
|
||||
it.dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
processResourcesTask,
|
||||
distributeResourcesTask
|
||||
)
|
||||
|
||||
@@ -201,24 +187,6 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
it.compilation = compilation
|
||||
it.description = "build webpack ${type.name.toLowerCase()} bundle"
|
||||
it._destinationDirectory = distribution.directory
|
||||
|
||||
val actualDceTaskProvider = when (type) {
|
||||
KotlinJsBinaryType.PRODUCTION -> dceTaskProvider
|
||||
KotlinJsBinaryType.DEVELOPMENT -> devDceTaskProvider
|
||||
}
|
||||
|
||||
it.entryProperty.set(
|
||||
actualDceTaskProvider.map {
|
||||
it.destinationDir
|
||||
.resolve(compileKotlinTask.outputFile.name)
|
||||
}
|
||||
)
|
||||
|
||||
it.resolveFromModulesFirst = true
|
||||
|
||||
commonWebpackConfigurations.forEach { configure ->
|
||||
it.configure()
|
||||
}
|
||||
}
|
||||
|
||||
if (type == KotlinJsBinaryType.PRODUCTION) {
|
||||
@@ -238,6 +206,46 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
}
|
||||
}
|
||||
|
||||
private fun KotlinWebpack.commonConfigure(
|
||||
compilation: KotlinJsCompilation,
|
||||
dceTaskProvider: TaskProvider<KotlinJsDceTask>,
|
||||
devDceTaskProvider: TaskProvider<KotlinJsDceTask>,
|
||||
type: KotlinJsBinaryType,
|
||||
nodeJs: NodeJsRootExtension
|
||||
) {
|
||||
dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
target.project.tasks.named(compilation.processResourcesTaskName)
|
||||
)
|
||||
|
||||
configureOptimization(type)
|
||||
|
||||
devServer = KotlinWebpackConfig.DevServer(
|
||||
open = true,
|
||||
contentBase = listOf(compilation.output.resourcesDir.canonicalPath)
|
||||
)
|
||||
|
||||
outputs.upToDateWhen { false }
|
||||
|
||||
val actualDceTaskProvider = when (type) {
|
||||
KotlinJsBinaryType.PRODUCTION -> dceTaskProvider
|
||||
KotlinJsBinaryType.DEVELOPMENT -> devDceTaskProvider
|
||||
}
|
||||
|
||||
entryProperty.set(
|
||||
actualDceTaskProvider.map {
|
||||
it.destinationDir
|
||||
.resolve(compilation.compileKotlinTask.outputFile.name)
|
||||
}
|
||||
)
|
||||
|
||||
resolveFromModulesFirst = true
|
||||
|
||||
commonRunConfigurations.forEach { configure ->
|
||||
configure()
|
||||
}
|
||||
}
|
||||
|
||||
private fun configureDce(
|
||||
compilation: KotlinJsCompilation,
|
||||
dev: Boolean
|
||||
|
||||
Reference in New Issue
Block a user