diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/dsl/KotlinJsTargetDsl.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/dsl/KotlinJsTargetDsl.kt index 559485b4079..30448065d7e 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/dsl/KotlinJsTargetDsl.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/dsl/KotlinJsTargetDsl.kt @@ -59,11 +59,6 @@ interface KotlinJsBrowserDsl : KotlinJsSubTargetDsl { ConfigureUtil.configure(fn, this) } } - - val buildVariants: NamedDomainObjectContainer - - fun NamedDomainObjectContainer.release(body: BuildVariant.() -> Unit) - fun NamedDomainObjectContainer.debug(body: BuildVariant.() -> Unit) } interface KotlinJsNodeDsl : KotlinJsSubTargetDsl { diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/subtargets/KotlinBrowserJs.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/subtargets/KotlinBrowserJs.kt index 2bffd694b73..322f610e2d2 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/subtargets/KotlinBrowserJs.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/subtargets/KotlinBrowserJs.kt @@ -26,7 +26,7 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) : KotlinJsSubTarget(target, "browser"), KotlinJsBrowserDsl { - override lateinit var buildVariants: NamedDomainObjectContainer + lateinit var buildVariants: NamedDomainObjectContainer override val testTaskDescription: String get() = "Run all ${target.name} tests inside browser using karma and webpack" @@ -47,40 +47,46 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) : (project.tasks.getByName(webpackTaskName) as KotlinWebpack).body() } - override fun NamedDomainObjectContainer.release(body: BuildVariant.() -> Unit) { - buildVariants.getByName(RELEASE).body() - } - - override fun NamedDomainObjectContainer.debug(body: BuildVariant.() -> Unit) { - buildVariants.getByName(DEBUG).body() - } - override fun configureRun(compilation: KotlinJsCompilation) { val project = compilation.target.project val nodeJs = NodeJsRootPlugin.apply(project.rootProject) - val run = project.registerTask(runTaskName) { - val compileKotlinTask = compilation.compileKotlinTask - it.dependsOn( - nodeJs.npmInstallTask, - compileKotlinTask, - target.project.tasks.getByName(compilation.processResourcesTaskName) - ) + buildVariants.all { buildVariant -> + val kind = buildVariant.kind + val run = project.registerTask( + disambiguateCamelCased( + lowerCamelCaseName( + buildVariant.name, + "run" + ) + ) + ) { + val compileKotlinTask = compilation.compileKotlinTask + it.dependsOn( + nodeJs.npmInstallTask, + compileKotlinTask, + target.project.tasks.getByName(compilation.processResourcesTaskName) + ) - it.bin = "webpack-dev-server/bin/webpack-dev-server.js" - it.compilation = compilation - it.description = "start webpack dev server" + it.configureOptimization(kind) - it.devServer = KotlinWebpackConfig.DevServer( - open = true, - contentBase = listOf(compilation.output.resourcesDir.canonicalPath) - ) + it.bin = "webpack-dev-server/bin/webpack-dev-server.js" + it.compilation = compilation + it.description = "start ${kind.name.toLowerCase()} webpack dev server" - it.outputs.upToDateWhen { false } + it.devServer = KotlinWebpackConfig.DevServer( + open = true, + contentBase = listOf(compilation.output.resourcesDir.canonicalPath) + ) + + it.outputs.upToDateWhen { false } + } + + if (kind == BuildVariantKind.DEBUG) { + target.runTask.dependsOn(run) + } } - - target.runTask.dependsOn(run) } override fun configureBuild(compilation: KotlinJsCompilation) { @@ -88,7 +94,8 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) : val nodeJs = NodeJsRootPlugin.apply(project.rootProject) buildVariants.all { buildVariant -> - project.registerTask( + val kind = buildVariant.kind + val build = project.registerTask( disambiguateCamelCased( lowerCamelCaseName( buildVariant.name, @@ -102,28 +109,34 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) : compileKotlinTask ) - val kind = buildVariant.kind - - it.mode = getByKind( - kind = kind, - releaseValue = Mode.PRODUCTION, - debugValue = Mode.DEVELOPMENT - ) - - it.devtool = getByKind( - kind = kind, - releaseValue = Devtool.SOURCE_MAP, - debugValue = Devtool.EVAL_SOURCE_MAP - ) + it.configureOptimization(kind) it.compilation = compilation - it.description = "build webpack bundle" + it.description = "build webpack ${kind.name.toLowerCase()} bundle" - project.tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).dependsOn(it) + + } + + if (kind == BuildVariantKind.RELEASE) { + project.tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).dependsOn(build) } } } + private fun KotlinWebpack.configureOptimization(kind: BuildVariantKind) { + mode = getByKind( + kind = kind, + releaseValue = Mode.PRODUCTION, + debugValue = Mode.DEVELOPMENT + ) + + devtool = getByKind( + kind = kind, + releaseValue = Devtool.SOURCE_MAP, + debugValue = Devtool.EVAL_SOURCE_MAP + ) + } + private fun getByKind( kind: BuildVariantKind, releaseValue: T,