Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ interface KotlinJsSubTargetContainerDsl : KotlinTarget {
*/
val browser: KotlinJsBrowserDsl

/**
* Returns the configuration options for generic execution environments
* used for this [KotlinTarget].
*
* For more information about execution environments, see
* https://kotl.in/kotlin-js-execution-environments
*
* @see org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsGenericDsl
*/
val generic: KotlinJsGenericDsl

/**
* Container for all execution environments enabled for this target.
* Currently, the only supported environments are Node.js and browser.
Expand Down Expand Up @@ -132,6 +143,7 @@ interface KotlinJsSubTargetContainerDsl : KotlinTarget {
interface KotlinJsTargetDsl :
KotlinTarget,
KotlinTargetWithNodeJsDsl,
KotlinTargetWithGenericJsDsl,
HasBinaries<KotlinJsBinaryContainer>,
HasConfigurableKotlinCompilerOptions<KotlinJsCompilerOptions> {

Expand Down Expand Up @@ -322,6 +334,46 @@ interface KotlinTargetWithNodeJsDsl {
}
}

/**
* Base options for configuring generic JS for use in
* Kotlin JS targets.
*
* To learn more see:
* - [Set up a Kotlin/JS project](https://kotl.in/kotlin-js-setup).
*/
interface KotlinTargetWithGenericJsDsl {
/**
* Enables 'generic' as the execution environment for this target,
* so the project can be used running JavaScript code in generic environments.
*
* For more information, see https://kotl.in/kotlin-js-execution-environments
*
* @see KotlinJsGenericDsl
*/
fun generic() = generic { }

/**
* Enables 'generic' as the execution environment for this target,
* so the project can be used running JavaScript code in generic environments.
*
* The target can be configured using [body].
*
* For more information, see https://kotl.in/kotlin-js-execution-environments
*
* @see KotlinJsGenericDsl
*/
fun generic(body: KotlinJsGenericDsl.() -> Unit)

/**
* [Action] based version of [generic] above.
*/
fun generic(fn: Action<KotlinJsGenericDsl>) {
generic {
fn.execute(this)
}
}
}

/**
* Common options for the configuring execution environments for Kotlin JS and Wasm targets.
*
Expand Down Expand Up @@ -473,3 +525,46 @@ interface KotlinJsNodeDsl : KotlinJsSubTargetDsl {
@ExperimentalMainFunctionArgumentsDsl
fun passCliArgumentsToMainFunction()
}

/**
* Generic execution environment options for Kotlin JS targets.
*
* For more information about execution environments, see
* https://kotl.in/kotlin-js-execution-environments
*
* **Note:** This interface is not intended for implementation by build script or plugin authors.
*/
interface KotlinJsGenericDsl : KotlinJsSubTargetDsl {

/**
* _This option is only relevant for JS targets._
* _Do not use in WasmJS targets._
*
* > Note: Passing arguments to the main function is Experimental.
* > It may be dropped or changed at any time.
*
* Enable passing `process.argv` to the main function's `args` parameter.
*
* See https://kotl.in/kotlin-js-pass-arguments-to-main-function
*
* @see KotlinJsTargetDsl.passAsArgumentToMainFunction
*/
@ExperimentalMainFunctionArgumentsDsl
fun passProcessArgvToMainFunction()

/**
* _This option is only relevant for JS targets._
* _Do not use in WasmJS targets._
*
* > Note: Passing arguments to the main function is Experimental.
* > It may be dropped or changed at any time.
*
* Enable passing `process.argv.slice(2)` to the main function's `args` parameter.
*
* See https://kotl.in/kotlin-js-pass-arguments-to-main-function
*
* @see KotlinJsTargetDsl.passAsArgumentToMainFunction
*/
@ExperimentalMainFunctionArgumentsDsl
fun passCliArgumentsToMainFunction()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2026 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.ir

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ProviderFactory
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalMainFunctionArgumentsDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsGenericDsl
import javax.inject.Inject

abstract class KotlinGenericJsIr
@Inject
internal constructor(
target: KotlinJsIrTarget,
private val objects: ObjectFactory,
private val providers: ProviderFactory
) :
KotlinJsIrSubTarget(target, "generic"),
KotlinJsGenericDsl {
@ExperimentalMainFunctionArgumentsDsl
override fun passProcessArgvToMainFunction() {
target.passAsArgumentToMainFunction("process.argv")
}

@ExperimentalMainFunctionArgumentsDsl
override fun passCliArgumentsToMainFunction() {
target.passAsArgumentToMainFunction("process.argv.slice(2)")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ constructor(
body(nodejs)
}

private val genericLazyDelegate = lazy {
addSubTarget(KotlinGenericJsIr::class.java) {
configureSubTarget()
}
}

override val generic: KotlinJsGenericDsl by genericLazyDelegate

override fun generic(body: KotlinJsGenericDsl.() -> Unit) {
body(generic)
}

//d8
@OptIn(ExperimentalWasmDsl::class)
private val d8LazyDelegate = lazy {
Expand Down