Gradle, JS: nodejs target

This commit is contained in:
Sergey Rostov
2019-04-23 14:44:05 +03:00
parent a609476245
commit 066efc3732
5 changed files with 102 additions and 4 deletions
@@ -74,7 +74,7 @@ class KotlinJsSingleTargetConfigurator(kotlinPluginVersion: String) :
override fun newTestsConfigurator(compilation: KotlinJsCompilation) =
object : KotlinJsCompilationTestsConfigurator(compilation) {
override fun configureDefaultTestFramework(it: KotlinJsTest) {
it.useMocha { }
it.useKarma { }
}
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. 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.nodejs
import org.gradle.api.tasks.AbstractExecTask
import org.gradle.api.tasks.Internal
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import java.io.File
class NodeJsExec : AbstractExecTask<NodeJsExec>(NodeJsExec::class.java) {
private val npmProject by lazy { project.npmProject }
init {
val nodeJs = NodeJsPlugin.apply(project).root
dependsOn(nodeJs.nodeJsSetupTask)
executable = nodeJs.environment.nodeExecutable
workingDir = project.npmProject.nodeWorkDir
}
@Internal
var script: String? = null
fun require(moduleName: String) {
args("--require", moduleName)
}
fun requireSourceMapSupport() {
require("kotlin-test-nodejs-runner/kotlin-nodejs-source-map-support.js")
}
fun script(file: File) {
check(script == null) {
"Script was already set to $script"
}
script = file.canonicalFile.relativeToOrSelf(npmProject.nodeWorkDir).path
}
fun compileOutput(compilationTask: Kotlin2JsCompile) =
script(npmProject.compileOutput(compilationTask))
}
@@ -46,7 +46,7 @@ open class NpmProject(
if (hoistGradleNodeModules) project.rootProject.npmProject.nodeModulesDir
else nodeModulesDir
open fun moduleOutput(compilationTask: Kotlin2JsCompile): File {
open fun compileOutput(compilationTask: Kotlin2JsCompile): File {
return compileOutputCopyDest?.resolve(compilationTask.outputFile.name) ?: compilationTask.outputFile
}
@@ -132,7 +132,7 @@ open class KotlinWebpack : DefaultTask() {
it.dependsOn(compileKotlinTask)
it.entry = npmProject.moduleOutput(compileKotlinTask)
it.entry = npmProject.compileOutput(compileKotlinTask)
it.outputs.upToDateWhen { false }
}
@@ -148,7 +148,7 @@ open class KotlinWebpack : DefaultTask() {
it.dependsOn(compileKotlinTask)
it.bin = "webpack-dev-server"
it.entry = npmProject.moduleOutput(compileKotlinTask)
it.entry = npmProject.compileOutput(compileKotlinTask)
val projectDir = compilation.target.project.projectDir.canonicalPath
it.devServer = KotlinWebpackConfig.DevServer(
@@ -0,0 +1,53 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. 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.nodejs
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsCompilationTestsConfigurator
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTargetConfigurator
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.tasks.createOrRegisterTask
class KotlinNodeJsSingleTargetConfigurator(kotlinPluginVersion: String) :
KotlinJsTargetConfigurator(kotlinPluginVersion) {
override fun configureTarget(target: KotlinOnlyTarget<KotlinJsCompilation>) {
super.configureTarget(target)
configureApplication(target)
}
private fun configureApplication(target: KotlinOnlyTarget<KotlinJsCompilation>) {
target.compilations.all { compilation ->
if (compilation.name == KotlinCompilation.MAIN_COMPILATION_NAME) {
// source maps support
compilation.dependencies {
runtimeOnly(kotlin("test-nodejs-runner"))
}
val project = target.project
project.createOrRegisterTask<NodeJsExec>("run") {
it.args(
project.npmProject.compileOutput(compilation.compileKotlinTask),
"--require", "kotlin-test-nodejs-runner/kotlin-nodejs-source-map-support.js"
)
}
}
}
}
override fun newTestsConfigurator(compilation: KotlinJsCompilation) =
object : KotlinJsCompilationTestsConfigurator(compilation) {
override fun configureDefaultTestFramework(it: KotlinJsTest) {
it.useNodeJs { }
}
}
}