org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2019 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.
*/
package org.jetbrains.kotlin.gradle.targets.js
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.AbstractKotlinTargetConfigurator.Companion.runTaskNameSuffix
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBrowserDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsNodeDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.subtargets.KotlinBrowserJs
import org.jetbrains.kotlin.gradle.targets.js.subtargets.KotlinNodeJs
import org.jetbrains.kotlin.gradle.tasks.locateTask
import org.jetbrains.kotlin.gradle.testing.internal.KotlinTestReport
import org.jetbrains.kotlin.gradle.testing.testTaskName
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import javax.inject.Inject
open class KotlinJsTarget @Inject constructor(project: Project, platformType: KotlinPlatformType) :
KotlinOnlyTarget(project, platformType),
KotlinTargetWithTests,
KotlinJsTargetDsl
{
override lateinit var testRuns: NamedDomainObjectContainer
internal set
val testTaskName get() = testRuns.getByName(KotlinTargetWithTests.DEFAULT_TEST_RUN_NAME).testTaskName
val testTask: TaskProvider
get() = checkNotNull(project.locateTask(testTaskName))
val runTaskName get() = lowerCamelCaseName(disambiguationClassifier, runTaskNameSuffix)
val runTask
get() = project.tasks.maybeCreate(runTaskName).also {
it.description = "Run js on all configured platforms"
}
private val browserLazyDelegate = lazy {
project.objects.newInstance(KotlinBrowserJs::class.java, this).also {
it.configure()
browserConfiguredHandlers.forEach { handler -> handler(it) }
browserConfiguredHandlers.clear()
}
}
private val browserConfiguredHandlers = mutableListOf Unit>()
val browser by browserLazyDelegate
internal val isBrowserConfigured: Boolean = browserLazyDelegate.isInitialized()
override fun browser(body: KotlinJsBrowserDsl.() -> Unit) {
body(browser)
}
private val nodejsLazyDelegate = lazy {
project.objects.newInstance(KotlinNodeJs::class.java, this).also {
it.configure()
nodejsConfiguredHandlers.forEach { handler -> handler(it) }
nodejsConfiguredHandlers.clear()
}
}
private val nodejsConfiguredHandlers = mutableListOf Unit>()
val nodejs by nodejsLazyDelegate
internal val isNodejsConfigured: Boolean = nodejsLazyDelegate.isInitialized()
override fun nodejs(body: KotlinJsNodeDsl.() -> Unit) {
body(nodejs)
}
fun whenBrowserConfigured(body: KotlinJsBrowserDsl.() -> Unit) {
if (browserLazyDelegate.isInitialized()) {
browser(body)
} else {
browserConfiguredHandlers += body
}
}
fun whenNodejsConfigured(body: KotlinJsNodeDsl.() -> Unit) {
if (nodejsLazyDelegate.isInitialized()) {
nodejs(body)
} else {
nodejsConfiguredHandlers += body
}
}
fun useCommonJs() {
compilations.all {
it.compileKotlinTask.kotlinOptions {
moduleKind = "commonjs"
sourceMap = true
sourceMapEmbedSources = null
}
}
}
}