Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasmJs target support. #251

Merged
merged 2 commits into from
Jan 12, 2024
Merged
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
97 changes: 36 additions & 61 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Build_gradle.MavenPomFile
import kotlinx.html.js.packageJson
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

/**
* This build script supports following parameters:
* -PversionTag - works together with "branch-build" profile and overrides "-SNAPSHOT" suffix of the version.
*/
plugins {
kotlin("multiplatform") version "1.9.21"
kotlin("multiplatform") version "1.9.22"
id("maven-publish")
id("signing")
}
Expand Down Expand Up @@ -90,7 +92,7 @@ kotlin {
}
}
}
js(IR) {
js {
moduleName = project.name
browser {
testTask {
Expand All @@ -105,6 +107,16 @@ kotlin {
pom { name by "${project.name}-js" }
}
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = project.name
browser()

mavenPublication {
groupId = group as String
pom { name by "${project.name}-wasm-js" }
}
}

mingwX64()
linuxX64()
Expand All @@ -123,6 +135,17 @@ kotlin {
macosX64()
macosArm64()

@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
common {
group("jsCommon") {
withJs()
// TODO: switch to `withWasmJs()` after upgrade to Kotlin 2.0
withWasm()
}
}
}

metadata {
mavenPublication {
groupId = group as String
Expand All @@ -140,7 +163,7 @@ kotlin {
sourceSets {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
implementation(kotlin("stdlib"))
}
}

Expand All @@ -150,61 +173,6 @@ kotlin {
}
}

val jsMain by getting {
dependencies {
implementation(kotlin("stdlib-js"))
}
}

val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}

val jvmMain by getting {
dependencies {
implementation(kotlin("stdlib"))
}
}

val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit"))
}
}

val nativeMain by creating
val nativeTest by creating

val nativeTargets = listOf(
"mingwX64",
"linuxX64",
"linuxArm64",
"iosX64",
"iosArm64",
"iosArm32",
"iosSimulatorArm64",
"watchosX86",
"watchosX64",
"watchosArm32",
"watchosArm64",
"watchosSimulatorArm64",
"tvosX64",
"tvosArm64",
"tvosSimulatorArm64",
"macosX64",
"macosArm64",
"watchosDeviceArm64",
)

val commonMain by getting
nativeMain.dependsOn(commonMain)

nativeTargets.forEach { target ->
findByName("${target}Main")?.dependsOn(nativeMain)
findByName("${target}Test")?.dependsOn(nativeTest)
}
}
}

Expand All @@ -226,9 +194,14 @@ tasks.register<Task>("generate") {

doLast {
kotlinx.html.generate.generate(
"kotlinx.html",
"src/commonMain/kotlin/generated",
"src/jsMain/kotlin/generated"
pkg = "kotlinx.html",
todir = "src/commonMain/kotlin/generated",
jsdir = "src/jsMain/kotlin/generated",
wasmJsDir = "src/wasmJsMain/kotlin/generated"
)
kotlinx.html.generate.generateJsTagTests(
jsdir = "src/jsTest/kotlin/generated",
wasmJsDir = "src/wasmJsTest/kotlin/generated",
)
}
}
Expand Down Expand Up @@ -342,3 +315,5 @@ rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlu
rootProject.the<org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension>().ignoreScripts = false
}

tasks.getByName("jsBrowserTest").dependsOn("wasmJsTestTestDevelopmentExecutableCompileSync")
tasks.getByName("wasmJsBrowserTest").dependsOn("jsTestTestDevelopmentExecutableCompileSync")
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ repositories {
dependencies {
implementation(kotlin("stdlib"))
implementation("com.sun.xsom:xsom:20140925")
implementation("com.squareup:kotlinpoet:1.15.3")
}
58 changes: 52 additions & 6 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/attributes.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package kotlinx.html.generate

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName

fun String.quote() = "\"$this\""

fun Appendable.attributePseudoDelegate(request: AttributeRequest) {
Expand Down Expand Up @@ -52,13 +59,15 @@ fun Appendable.facade(repository: Repository, facade: AttributeFacade) {
}
}

fun Appendable.eventProperty(parent: String, attribute: AttributeInfo) {
fun Appendable.eventProperty(parent: String, attribute: AttributeInfo, shouldUnsafeCast: Boolean) {
val type = "(org.w3c.dom.events.Event) -> Unit"
variable(receiver = parent, variable = Var(
variable(
receiver = parent, variable = Var(
name = attribute.fieldName + "Function",
type = type,
mutable = true
))
)
)
emptyLine()

getter().defineIs(StringBuilder().apply {
Expand All @@ -67,11 +76,48 @@ fun Appendable.eventProperty(parent: String, attribute: AttributeInfo) {
})
setter {
receiverDot("consumer")
functionCall("onTagEvent", listOf(
val newValue = if (shouldUnsafeCast) {
"newValue.unsafeCast<(Event) -> Unit>()"
} else {
"newValue"
}
functionCall(
"onTagEvent", listOf(
"this",
attribute.name.quote(),
"newValue.unsafeCast<(Event) -> Unit>()"
))
newValue
)
)
}
emptyLine()
}

fun eventProperty(parent: TypeName, attribute: AttributeInfo, shouldUnsafeCast: Boolean): PropertySpec {
val propertyType = LambdaTypeName.get(
returnType = ClassName("kotlin", "Unit"),
parameters = listOf(ParameterSpec.unnamed(ClassName("kotlinx.html.org.w3c.dom.events", "Event"))),
)
return PropertySpec.builder(attribute.fieldName + "Function", propertyType)
.mutable()
.receiver(parent)
.getter(
FunSpec.getterBuilder()
.addStatement("throw UnsupportedOperationException(\"You can't read variable ${attribute.fieldName}\")")
.build()
)
.setter(
FunSpec.setterBuilder()
.addParameter("newValue", propertyType)
.addStatement(
"consumer.onTagEvent(this, %S, %L)",
attribute.name,
if (shouldUnsafeCast) {
"newValue.unsafeCast<(Event) -> Unit>()"
} else {
"newValue"
}
)
.build()
)
.build()
}
Loading