Skip to content

Commit 5fd2aed

Browse files
authored
Merge pull request #617 from CanizaresSoftwares/add_kotlin_js_support
feat: add JavaScript target support for compose-multiplatform
2 parents 618d3ee + bc3b746 commit 5fd2aed

File tree

18 files changed

+312
-470
lines changed

18 files changed

+312
-470
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The library provides the following artifacts:
5555

5656
`com.kizitonwose.calendar:compose`: The compose artifact for Android projects. This uses the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) APIs.
5757

58-
`com.kizitonwose.calendar:compose-multiplatform`: The compose artifact for Compose Multiplatform projects. This uses the [kotlinx-datetime](https://github.com/Kotlin/kotlinx-datetime) library and supports Android, iOS, WasmJs and Desktop platforms.
58+
`com.kizitonwose.calendar:compose-multiplatform`: The compose artifact for Compose Multiplatform projects. This uses the [kotlinx-datetime](https://github.com/Kotlin/kotlinx-datetime) library and supports Android, iOS, js, WasmJs and Desktop platforms.
5959

6060
`com.kizitonwose.calendar:view`: The view artifact for Android projects. This uses the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) APIs and can exist alongside the Android compose artifact in an Android project if needed.
6161

@@ -116,7 +116,7 @@ Add the multiplatform calendar library to your project's `build.gradle.kts`:
116116
```kotlin
117117
commonMain.dependencies {
118118
// The calendar library for compose multiplatform projects
119-
// Supports Android, iOS, WasmJs and Desktop platforms
119+
// Supports Android, iOS, js, WasmJs and Desktop platforms
120120
implementation("com.kizitonwose.calendar:compose-multiplatform:<latest-version>")
121121
}
122122
```

compose-multiplatform/library/api/library.klib.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Klib ABI Dump
2-
// Targets: [iosArm64, iosSimulatorArm64, iosX64, wasmJs]
2+
// Targets: [iosArm64, iosSimulatorArm64, iosX64, js, wasmJs]
33
// Rendering settings:
44
// - Signature version: 2
55
// - Show manifest properties: true

compose-multiplatform/library/build.gradle.kts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ kotlin {
2222
binaries.library()
2323
}
2424

25+
js(IR) {
26+
browser()
27+
}
28+
2529
androidTarget {
2630
publishLibraryVariants("release")
2731
}
@@ -43,6 +47,7 @@ kotlin {
4347

4448
sourceSets {
4549
val commonMain by getting
50+
val jsMain by getting
4651
val wasmJsMain by getting
4752
val nativeMain by getting
4853
val desktopMain by getting
@@ -65,10 +70,16 @@ kotlin {
6570
api(libs.kotlinx.datetime)
6671
}
6772

73+
val webMain by creating {
74+
dependsOn(commonMain)
75+
jsMain.dependsOn(this)
76+
wasmJsMain.dependsOn(this)
77+
}
78+
6879
val nonJvmMain by creating {
6980
dependsOn(commonMain)
7081
nativeMain.dependsOn(this)
71-
wasmJsMain.dependsOn(this)
82+
webMain.dependsOn(this)
7283
dependencies {
7384
api(libs.kotlinx.serialization.core)
7485
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.kizitonwose.calendar.core
2+
3+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getWeekInfo#firstday
4+
internal actual fun jsFirstDayFromTag(languageTag: String): Int = js("new Intl.Locale(languageTag).getWeekInfo().firstDay") as Int
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.kizitonwose.calendar.data
2+
3+
internal actual fun log(tag: String, message: String) =
4+
console.log("$tag : $message")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.kizitonwose.calendar.core
2+
3+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getWeekInfo#firstday
4+
internal actual fun jsFirstDayFromTag(languageTag: String): Int = js("new Intl.Locale(languageTag).weekInfo?.firstDay")
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,3 @@ public actual fun firstDayOfWeekFromLocale(locale: Locale): DayOfWeek {
1515
firstDayFromMap(locale)
1616
}
1717
}
18-
19-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getWeekInfo#firstday
20-
private fun jsFirstDayFromTag(languageTag: String): Int = js("new Intl.Locale(languageTag).weekInfo?.firstDay")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.kizitonwose.calendar.core
2+
3+
internal expect fun jsFirstDayFromTag(languageTag: String): Int

compose-multiplatform/sample/build.gradle.kts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import com.kizitonwose.calendar.buildsrc.Config
33
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
44
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
55
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
6+
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDistributionDsl
67
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
78

89
plugins {
@@ -30,6 +31,23 @@ kotlin {
3031
binaries.executable()
3132
}
3233

34+
js(IR) {
35+
outputModuleName = "composeJsApp"
36+
browser {
37+
commonWebpackConfig {
38+
outputFileName = "composeJsApp.js"
39+
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).apply {
40+
static = (static ?: mutableListOf()).apply {
41+
// Serve sources to debug inside browser
42+
add(project.projectDir.path)
43+
}
44+
}
45+
}
46+
useCommonJs()
47+
}
48+
binaries.executable()
49+
}
50+
3351
androidTarget {}
3452

3553
jvm("desktop")
@@ -49,6 +67,7 @@ kotlin {
4967

5068
sourceSets {
5169
val commonMain by getting
70+
val jsMain by getting
5271
val wasmJsMain by getting
5372
val nativeMain by getting
5473
val desktopMain by getting
@@ -70,11 +89,17 @@ kotlin {
7089
// implementation("com.kizitonwose.calendar:compose-multiplatform:2.6.0-alpha02")
7190
implementation(project(":compose-multiplatform:library"))
7291
implementation(libs.jetbrains.compose.navigation)
92+
implementation(libs.material.icons)
93+
}
94+
val webMain by creating {
95+
dependsOn(commonMain)
96+
jsMain.dependsOn(this)
97+
wasmJsMain.dependsOn(this)
7398
}
7499
val nonJvmMain by creating {
75100
dependsOn(commonMain)
76101
nativeMain.dependsOn(this)
77-
wasmJsMain.dependsOn(this)
102+
webMain.dependsOn(this)
78103
dependencies {}
79104
}
80105
desktopMain.dependsOn(jvmMain)

0 commit comments

Comments
 (0)