Skip to content

Commit

Permalink
fix: unify WASM & JS targets default configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Artyom Shendrik <[email protected]>
  • Loading branch information
amal committed Oct 16, 2023
1 parent 0603498 commit 297c674
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
28 changes: 20 additions & 8 deletions fluxo-kmp-conf/src/main/kotlin/SetupKotlinJs.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import fluxo.conf.dsl.container.KotlinTargetContainer
import org.gradle.api.Action
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsSubTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinTargetWithNodeJsDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinWasmJsTargetDsl


internal val DEFAULT_COMMON_JS_CONFIGURATION: KotlinTargetContainer<KotlinJsTargetDsl>.() -> Unit =
internal val DEFAULT_COMMON_JS_CONFIGURATION: KotlinTargetContainer<KotlinTarget>.() -> Unit =
{
target {
defaults()
if (this is KotlinJsTargetDsl) {
defaults()
} else if (this is KotlinTargetWithNodeJsDsl) {
nodejs {
testTimeout(seconds = TEST_TIMEOUT)
}
}
}
}

public fun KotlinJsTargetDsl.defaults() {
// set up browser & nodejs environment + test timeouts
testTimeout()

// Apply Binaryen optimizer to the WASM target
if (this is KotlinWasmJsTargetDsl) {
applyBinaryen()
}
Expand All @@ -28,6 +37,11 @@ public fun KotlinJsTargetDsl.defaults() {
}
}

try {
useEsModules()
} catch (_: Error) {
}

// Generate TypeScript declaration files
// https://kotlinlang.org/docs/js-ir-compiler.html#preview-generation-of-typescript-declaration-files-d-ts
binaries.executable()
Expand All @@ -50,11 +64,9 @@ public fun KotlinJsTargetDsl.testTimeout(seconds: Int = TEST_TIMEOUT) {

public fun KotlinJsSubTargetDsl.testTimeout(seconds: Int = TEST_TIMEOUT) {
require(seconds > 0) { "Timeout seconds must be greater than 0." }
testTask(
Action {
useMocha { timeout = "${seconds}s" }
},
)
testTask {
useMocha { timeout = "${seconds}s" }
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package fluxo.conf.dsl.container.target

import DEFAULT_COMMON_JS_CONFIGURATION
import DEFAULT_COMMON_JS_CONFIGURATION as DEFAULT_CONF
import fluxo.conf.dsl.container.KotlinTargetContainer
import fluxo.conf.impl.EMPTY_FUN
import org.jetbrains.kotlin.gradle.dsl.KotlinTargetContainerWithWasmPresetFunctions
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinWasmJsTargetDsl
Expand All @@ -23,7 +22,7 @@ public interface WasmTarget<out T : KotlinWasmTargetDsl> : KotlinTargetContainer
@Suppress("NEWER_VERSION_IN_SINCE_KOTLIN")
public fun wasmJs(
targetName: String = "wasmJs",
action: WasmTarget<KotlinWasmJsTargetDsl>.() -> Unit = DEFAULT_COMMON_JS_CONFIGURATION,
action: WasmTarget<KotlinWasmJsTargetDsl>.() -> Unit = DEFAULT_CONF,
)

/**
Expand All @@ -35,7 +34,7 @@ public interface WasmTarget<out T : KotlinWasmTargetDsl> : KotlinTargetContainer
@Suppress("NEWER_VERSION_IN_SINCE_KOTLIN")
public fun wasmWasi(
targetName: String = "wasmWasi",
action: WasmTarget<KotlinWasmWasiTargetDsl>.() -> Unit = EMPTY_FUN,
action: WasmTarget<KotlinWasmWasiTargetDsl>.() -> Unit = DEFAULT_CONF,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,36 @@ internal fun configureKotlinMultiplatform(
}

val project = configuration.project
configuration.context.loadAndApplyPluginIfNotApplied(id = KMP_PLUGIN_ID, project = project)
val context = configuration.context
context.loadAndApplyPluginIfNotApplied(id = KMP_PLUGIN_ID, project = project)

// Add all plugins first, for configuring in next steps.
val pluginManager = project.pluginManager
val containerList = containers.toMutableList()
containerList.iterator().let { iter ->
for (container in iter) {
(container as? ContainerImpl)?.let { c ->
try {
c.applyPluginsWith(pluginManager)
} catch (e: Throwable) {
iter.remove()

var msg = e.toString()
msg = when {
// Special case for Android plugin.
@Suppress("InstanceOfCheckForException")
e is UnknownPluginException && "com.android." in msg ->
ANDROID_PLUGIN_NOT_IN_CLASSPATH_ERROR

else ->
"Couldn't apply ${c.name} container due to: $msg"
val c = container as? ContainerImpl ?: continue
try {
c.applyPluginsWith(pluginManager)
} catch (e: Throwable) {
iter.remove()

var logException = true
var msg = e.toString()
msg = when {
// Special case for Android plugin.
@Suppress("InstanceOfCheckForException")
e is UnknownPluginException && "com.android." in msg -> {
logException = context.isMaxDebug
ANDROID_PLUGIN_NOT_IN_CLASSPATH_ERROR
}
project.logger.e(msg, e)

else ->
"Couldn't apply ${c.name} container due to: $msg"
}

val ex = if (logException) e else null
project.logger.e(msg, ex)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ internal fun KotlinMultiplatformExtension.setupMultiplatformDependencies(
try {
constraints.implementation(it)
} catch (e: Throwable) {
project.logger.w("Failed to add constraint for $it: $e", e)
val ex = if (context.isMaxDebug) e else null
project.logger.w("Failed to add constraint for $it: $e", ex)
}
}

Expand Down

0 comments on commit 297c674

Please sign in to comment.