[Gradle, JS] Browser with binaries
This commit is contained in:
+30
-49
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.gradle.targets.js.ir
|
||||
import org.gradle.api.NamedDomainObjectContainer
|
||||
import org.gradle.api.Task
|
||||
import org.gradle.api.plugins.BasePluginConvention
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.tasks.Copy
|
||||
import org.gradle.language.base.plugins.LifecycleBasePlugin
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJsDce
|
||||
@@ -67,34 +66,29 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
val project = compilation.target.project
|
||||
val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
|
||||
|
||||
buildVariants.all { buildVariant ->
|
||||
val kind = buildVariant.kind
|
||||
val commonRunTask = project.registerTask<Task>(disambiguateCamelCased(RUN_TASK_NAME)) {}
|
||||
|
||||
binaries.getBinaries(compilation).all { binary ->
|
||||
val type = binary.type
|
||||
|
||||
val runTask = project.registerTask<KotlinWebpack>(
|
||||
disambiguateCamelCased(
|
||||
buildVariant.name,
|
||||
binary.name,
|
||||
RUN_TASK_NAME
|
||||
)
|
||||
) {
|
||||
it.dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
getByKind(
|
||||
kind,
|
||||
compilation.productionLinkTask,
|
||||
compilation.developmentLinkTask
|
||||
),
|
||||
binary.linkTask,
|
||||
target.project.tasks.getByName(compilation.processResourcesTaskName)
|
||||
)
|
||||
|
||||
it.configureOptimization(kind)
|
||||
it.configureOptimization(type)
|
||||
|
||||
it.bin = "webpack-dev-server/bin/webpack-dev-server.js"
|
||||
it.compilation = compilation
|
||||
it.entry = getByKind(
|
||||
kind,
|
||||
compilation.productionLinkTask.map { it.outputFile },
|
||||
compilation.developmentLinkTask.map { it.outputFile }
|
||||
).get()
|
||||
it.description = "start ${kind.name.toLowerCase()} webpack dev server"
|
||||
it.entry = binary.linkTask.map { it.outputFile }.get()
|
||||
it.description = "start ${type.name.toLowerCase()} webpack dev server"
|
||||
|
||||
it.devServer = KotlinWebpackConfig.DevServer(
|
||||
open = true,
|
||||
@@ -108,9 +102,9 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
}
|
||||
}
|
||||
|
||||
if (kind == BuildVariantKind.DEVELOPMENT) {
|
||||
if (type == BuildVariantKind.DEVELOPMENT) {
|
||||
target.runTask.dependsOn(runTask)
|
||||
project.registerTask<Task>(disambiguateCamelCased(RUN_TASK_NAME)) {
|
||||
commonRunTask.configure {
|
||||
it.dependsOn(runTask)
|
||||
}
|
||||
}
|
||||
@@ -142,49 +136,45 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
val assembleTask = project.tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME)
|
||||
assembleTask.dependsOn(distributeResourcesTask)
|
||||
|
||||
buildVariants.all { buildVariant ->
|
||||
val kind = buildVariant.kind
|
||||
val webpackCommonTask = project.registerTask<Task>(disambiguateCamelCased(WEBPACK_TASK_NAME)) {
|
||||
}
|
||||
|
||||
project.registerTask<Task>(disambiguateCamelCased(DISTRIBUTION_TASK_NAME)) {
|
||||
it.dependsOn(webpackCommonTask)
|
||||
it.dependsOn(distributeResourcesTask)
|
||||
}
|
||||
|
||||
binaries.getBinaries(compilation).all { binary ->
|
||||
val type = binary.type
|
||||
val webpackTask = project.registerTask<KotlinWebpack>(
|
||||
disambiguateCamelCased(
|
||||
buildVariant.name,
|
||||
binary.name,
|
||||
WEBPACK_TASK_NAME
|
||||
)
|
||||
) {
|
||||
it.dependsOn(
|
||||
nodeJs.npmInstallTask,
|
||||
getByKind(
|
||||
kind,
|
||||
compilation.productionLinkTask,
|
||||
compilation.developmentLinkTask
|
||||
),
|
||||
binary.linkTask,
|
||||
distributeResourcesTask
|
||||
)
|
||||
|
||||
it.configureOptimization(kind)
|
||||
it.configureOptimization(type)
|
||||
|
||||
it.compilation = compilation
|
||||
it.entry = getByKind(
|
||||
kind,
|
||||
compilation.productionLinkTask.map { it.outputFile },
|
||||
compilation.developmentLinkTask.map { it.outputFile }
|
||||
).get()
|
||||
it.description = "build webpack ${kind.name.toLowerCase()} bundle"
|
||||
it.destinationDirectory = distribution.directory!!
|
||||
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 (kind == BuildVariantKind.PRODUCTION) {
|
||||
if (type == BuildVariantKind.PRODUCTION) {
|
||||
assembleTask.dependsOn(webpackTask)
|
||||
val webpackCommonTask = project.registerTask<Task>(disambiguateCamelCased(WEBPACK_TASK_NAME)) {
|
||||
webpackCommonTask.configure {
|
||||
it.dependsOn(webpackTask)
|
||||
}
|
||||
project.registerTask<Task>(disambiguateCamelCased(DISTRIBUTION_TASK_NAME)) {
|
||||
it.dependsOn(webpackCommonTask)
|
||||
it.dependsOn(distributeResourcesTask)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -212,15 +202,6 @@ open class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) :
|
||||
BuildVariantKind.DEVELOPMENT -> debugValue
|
||||
}
|
||||
|
||||
private fun <T> getByKind(
|
||||
kind: BuildVariantKind,
|
||||
releaseValue: Provider<T>,
|
||||
debugValue: Provider<T>
|
||||
): Provider<T> = when (kind) {
|
||||
BuildVariantKind.PRODUCTION -> releaseValue
|
||||
BuildVariantKind.DEVELOPMENT -> debugValue
|
||||
}
|
||||
|
||||
override fun configureBuildVariants() {
|
||||
buildVariants = project.container(BuildVariant::class.java)
|
||||
buildVariants.create(PRODUCTION) {
|
||||
|
||||
+8
-10
@@ -28,7 +28,6 @@ constructor(
|
||||
get() = target.project
|
||||
|
||||
private val binaryNames = mutableSetOf<String>()
|
||||
private val compilationToBinaries = mutableMapOf<KotlinJsCompilation, MutableSet<JsBinary>>()
|
||||
|
||||
private val defaultCompilation: KotlinJsCompilation
|
||||
get() = target.compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
|
||||
@@ -50,13 +49,19 @@ constructor(
|
||||
create = ::TestExecutable
|
||||
)
|
||||
|
||||
internal fun getBinaries(
|
||||
compilation: KotlinJsCompilation
|
||||
): DomainObjectSet<JsBinary> =
|
||||
matching { it.compilation == compilation }
|
||||
|
||||
internal fun getBinary(
|
||||
compilation: KotlinJsCompilation,
|
||||
buildVariantKind: BuildVariantKind
|
||||
): JsBinary =
|
||||
compilationToBinaries.getValue(
|
||||
getBinaries(
|
||||
compilation
|
||||
).single { it.type == buildVariantKind }
|
||||
).matching { it.type == buildVariantKind }
|
||||
.single()
|
||||
|
||||
|
||||
private fun <T : JsBinary> createBinaries(
|
||||
@@ -77,13 +82,6 @@ constructor(
|
||||
|
||||
val binary = create(name, buildVariantKind, compilation)
|
||||
add(binary)
|
||||
with(compilationToBinaries[compilation]) {
|
||||
if (this != null) {
|
||||
add(binary)
|
||||
} else {
|
||||
compilationToBinaries[compilation] = mutableSetOf<JsBinary>(binary)
|
||||
}
|
||||
}
|
||||
// Allow accessing binaries as properties of the container in Groovy DSL.
|
||||
if (this is ExtensionAware) {
|
||||
extensions.add(binary.name, binary)
|
||||
|
||||
-23
@@ -6,38 +6,15 @@
|
||||
package org.jetbrains.kotlin.gradle.targets.js.ir
|
||||
|
||||
import org.gradle.api.file.SourceDirectorySet
|
||||
import org.gradle.api.tasks.TaskProvider
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.BuildVariantKind
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.BuildVariantKind.DEVELOPMENT
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.BuildVariantKind.PRODUCTION
|
||||
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
|
||||
|
||||
class KotlinJsIrCompilation(
|
||||
target: KotlinTarget,
|
||||
name: String
|
||||
) : KotlinJsCompilation(target, name) {
|
||||
val productionLinkTaskName: String = linkTaskName(PRODUCTION)
|
||||
|
||||
val productionLinkTask: TaskProvider<KotlinJsIrLink>
|
||||
get() = target.project.tasks.named(productionLinkTaskName) as TaskProvider<KotlinJsIrLink>
|
||||
|
||||
val developmentLinkTaskName: String = linkTaskName(DEVELOPMENT)
|
||||
|
||||
val developmentLinkTask: TaskProvider<KotlinJsIrLink>
|
||||
get() = target.project.tasks.named(developmentLinkTaskName) as TaskProvider<KotlinJsIrLink>
|
||||
|
||||
private fun linkTaskName(type: BuildVariantKind): String =
|
||||
lowerCamelCaseName(
|
||||
"compile",
|
||||
type.name.toLowerCase(),
|
||||
compilationName.takeIf { it != KotlinCompilation.MAIN_COMPILATION_NAME },
|
||||
"Kotlin",
|
||||
target.targetName
|
||||
)
|
||||
|
||||
internal val allSources: MutableSet<SourceDirectorySet> = mutableSetOf()
|
||||
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ constructor(
|
||||
KotlinJsBinaryContainer::class.java,
|
||||
this,
|
||||
WrapUtil.toDomainObjectSet(JsBinary::class.java).apply {
|
||||
all {
|
||||
matching { it is Executable }.all {
|
||||
whenBrowserConfigured {
|
||||
(this as KotlinJsIrSubTarget).produceExecutable()
|
||||
}
|
||||
|
||||
-9
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||
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.KotlinBrowserJsIr
|
||||
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
|
||||
@@ -44,14 +43,6 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
||||
override val testTaskDescription: String
|
||||
get() = "Run all ${target.name} tests inside browser using karma and webpack"
|
||||
|
||||
private val irBrowser: KotlinBrowserJsIr?
|
||||
get() = target.irTarget?.browser
|
||||
|
||||
override fun produceExecutable() {
|
||||
super.produceExecutable()
|
||||
irBrowser?.produceExecutable()
|
||||
}
|
||||
|
||||
override fun configureDefaultTestFramework(testTask: KotlinJsTest) {
|
||||
testTask.useKarma {
|
||||
useChromeHeadless()
|
||||
|
||||
-9
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.gradle.targets.js.subtargets
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsNodeDsl
|
||||
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinNodeJsIr
|
||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
|
||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
||||
import javax.inject.Inject
|
||||
@@ -21,14 +20,6 @@ open class KotlinNodeJs @Inject constructor(target: KotlinJsTarget) :
|
||||
|
||||
private val runTaskName = disambiguateCamelCased("run")
|
||||
|
||||
private val irNodejs: KotlinNodeJsIr?
|
||||
get() = target.irTarget?.nodejs
|
||||
|
||||
override fun produceExecutable() {
|
||||
super.produceExecutable()
|
||||
irNodejs?.produceExecutable()
|
||||
}
|
||||
|
||||
override fun runTask(body: NodeJsExec.() -> Unit) {
|
||||
(project.tasks.getByName(runTaskName) as NodeJsExec).body()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user