Skip to content

Commit

Permalink
Add wasmJs target (#5458)
Browse files Browse the repository at this point in the history
* add wasmJs target

* add comment

* crash
  • Loading branch information
martinbonnin committed Dec 12, 2023
1 parent 84f0f92 commit 1c939f9
Show file tree
Hide file tree
Showing 42 changed files with 1,017 additions and 59 deletions.
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation(libs.android.plugin)
implementation(libs.gradle.japicmp.plugin)
implementation(libs.vespene)
implementation(libs.kotlinx.serialization.json)
implementation(libs.poet.java)
implementation(libs.poet.kotlin)
implementation(libs.intellij.plugin)
Expand Down
12 changes: 10 additions & 2 deletions build-logic/src/main/kotlin/CompilerOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,16 @@ fun Project.configureJavaAndKotlinCompilers() {
// Ensure "org.gradle.jvm.version" is set to "8" in Gradle metadata of jvm-only modules.
options.release.set(8)
}

allWarningsAsErrors(true)

/**
* Required because of:
*
* > Task :apollo-runtime:compileKotlinWasmJs
* w: duplicate library name: kotlin
*
* (maybe https://youtrack.jetbrains.com/issue/KT-51110?)
*/
allWarningsAsErrors(false)
}

@Suppress("UnstableApiUsage")
Expand Down
45 changes: 7 additions & 38 deletions build-logic/src/main/kotlin/Mpp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink

private val allAppleTargets = setOf(
internal val allAppleTargets = setOf(
"macosX64",
"macosArm64",
"iosArm64",
Expand All @@ -33,49 +33,13 @@ private val enableLinux = System.getenv("APOLLO_JVM_ONLY")?.toBoolean()?.not() ?
private val enableJs = System.getenv("APOLLO_JVM_ONLY")?.toBoolean()?.not() ?: true
private val enableApple = System.getenv("APOLLO_JVM_ONLY")?.toBoolean()?.not() ?: true

fun Project.configureMppDefaults(
withJvm: Boolean,
withJs: Boolean,
withLinux: Boolean,
withAndroid: Boolean,
withApple: Boolean,
) {
configureMpp(
withJvm = withJvm,
withJs = withJs,
browserTest = false,
withLinux = withLinux,
appleTargets = if (!withApple) emptySet() else allAppleTargets,
withAndroid = withAndroid,
)
}


/**
* Same as [configureMppDefaults] but without Linux targets. Apple targets can be configured.
* Tests only run on the JVM, JS and macOS
*/
fun Project.configureMppTestsDefaults(
withJs: Boolean,
withJvm: Boolean,
browserTest: Boolean,
appleTargets: Collection<String>,
) {
configureMpp(
withJvm = withJvm,
withJs = withJs,
browserTest = browserTest,
withLinux = false,
withAndroid = false,
appleTargets = appleTargets,
)
}

fun Project.configureMpp(
withJvm: Boolean,
withJs: Boolean,
withLinux: Boolean,
withAndroid: Boolean,
withWasm: Boolean,
appleTargets: Collection<String>,
browserTest: Boolean,
) {
Expand Down Expand Up @@ -138,6 +102,11 @@ fun Project.configureMpp(
}
}
}
if (withWasm) {
wasmJs {
nodejs()
}
}

configureSourceSetGraph()
addTestDependencies()
Expand Down
14 changes: 10 additions & 4 deletions build-logic/src/main/kotlin/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fun Project.apolloLibrary(
withLinux: Boolean = true,
withApple: Boolean = true,
withJvm: Boolean = true,
withWasm: Boolean = true,
publish: Boolean = true
) {
group = property("GROUP")!!
Expand Down Expand Up @@ -35,12 +36,14 @@ fun Project.apolloLibrary(
}

if (extensions.findByName("kotlin") is KotlinMultiplatformExtension) {
configureMppDefaults(
configureMpp(
withJvm = withJvm,
withJs = withJs,
browserTest = false,
withLinux = withLinux,
appleTargets = if (!withApple) emptySet() else allAppleTargets,
withAndroid = extensions.findByName("android") != null,
withApple = withApple,
withWasm = withWasm
)
}

Expand Down Expand Up @@ -68,11 +71,14 @@ fun Project.apolloTest(
configureTesting()

if (extensions.findByName("kotlin") is KotlinMultiplatformExtension) {
configureMppTestsDefaults(
withJs = withJs,
configureMpp(
withJvm = withJvm,
withJs = withJs,
browserTest = browserTest,
withLinux = false,
withAndroid = false,
appleTargets = appleTargets,
withWasm = false
)
}
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ org.gradle.caching=true
# org.gradle.configuration-cache=true

org.gradle.parallel=true

kotlin.wasm.stability.nowarn=true
15 changes: 8 additions & 7 deletions gradle/libraries.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ kotlin-plugin-min = "1.8.0"
kotlin-plugin = "2.0.0-Beta1"
kotlin-plugin-max = "2.0.0-Beta1"
kotlin-stdlib = "2.0.0-Beta1"
kotlinx-coroutines = "1.7.3"
kotlinx-datetime = "0.4.1"
kotlinx-serialization-runtime = "1.5.0"
kotlinx-coroutines = "1.8.0-RC"
kotlinx-datetime = "0.5.0"
kotlinx-serialization-runtime = "1.6.2"
ksp = "2.0.0-Beta1-1.0.14"
ktor = "2.3.3"
okio = "3.6.0"
ktor = "2.3.5"
okhttp = "4.11.0"
rx-android = "2.0.1"
rx-java2 = "2.2.21"
rx-java3 = "3.1.6"
sqldelight = "2.0.0"
sqldelight = "2.0.1"
truth = "1.1.3"
moshix = "0.14.1"

Expand Down Expand Up @@ -83,7 +83,7 @@ apollo-testingsupport = { group = "com.apollographql.apollo3", name = "apollo-te
# Used by the apollo-tooling project which uses a published version of Apollo
apollo-testingsupport-published = { group = "com.apollographql.apollo3", name = "apollo-testing-support", version.ref = "apollo-published" }
assertj = { group = "org.assertj", name = "assertj-core", version = "3.24.2" }
atomicfu = { group = "org.jetbrains.kotlinx", name = "atomicfu", version = "0.20.1" }
atomicfu = { group = "org.jetbrains.kotlinx", name = "atomicfu", version = "0.23.1" }
benmanes-versions = { group = "com.github.ben-manes", name = "gradle-versions-plugin", version = "0.33.0" }
clikt = { group = "com.github.ajalt.clikt", name = "clikt", version = "3.5.2" }
compose-runtime = { group = "androidx.compose.runtime", name = "runtime", version.ref = "compose" }
Expand Down Expand Up @@ -126,6 +126,7 @@ kotlin-reflect = { group = "org.jetbrains.kotlin", name = "kotlin-reflect" } # t
kotlin-stdlib-common = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-common", version.ref = "kotlin-stdlib" }
kotlin-stdlib-jvm = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlin-stdlib" }
kotlin-stdlib-js = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-js", version.ref = "kotlin-stdlib" }
kotlin-stdlib-wasm-js = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-wasm-js", version.ref = "kotlin-stdlib" }
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test" } # the Kotlin plugin resolves the version
kotlin-test-annotations-common = { group = "org.jetbrains.kotlin", name = "kotlin-test-annotations-common" } # the Kotlin plugin resolves the version
kotlin-test-common = { group = "org.jetbrains.kotlin", name = "kotlin-test-common" } # the Kotlin plugin resolves the version
Expand Down Expand Up @@ -173,7 +174,7 @@ sqldelight-jvm = { group = "app.cash.sqldelight", name = "sqlite-driver", versio
sqldelight-native = { group = "app.cash.sqldelight", name = "native-driver", version.ref = "sqldelight" }
sqldelight-plugin = { group = "app.cash.sqldelight", name = "gradle-plugin", version.ref = "sqldelight" }
sqldelight-runtime = { group = "app.cash.sqldelight", name = "runtime", version.ref = "sqldelight" }
sqldelight-sqlite = "app.cash.sqldelight:sqlite-dialect:2.0.0-alpha01"
sqldelight-sqlite = { group = "app.cash.sqldelight", name = "sqlite-dialect", version.ref = "sqldelight" }
sqlite-jdbc = "org.xerial:sqlite-jdbc:3.43.2.0"
jar-relocator = "me.lucko:jar-relocator:1.5"
benchmark-gradle-plugin = "androidx.benchmark:benchmark-gradle-plugin:1.1.1"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-rc-1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
3 changes: 2 additions & 1 deletion libraries/apollo-adapters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ plugins {

apolloLibrary(
javaModuleName = "com.apollographql.apollo3.adapter",
withLinux = false
withLinux = false,
withWasm = false
)

kotlin {
Expand Down
5 changes: 5 additions & 0 deletions libraries/apollo-annotations/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ kotlin {
api(libs.kotlin.stdlib.js)
}
}
findByName("wasmJsMain")!!.apply {
dependencies {
api(libs.kotlin.stdlib.wasm.js)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.apollographql.apollo3.annotations

import kotlin.reflect.AssociatedObjectKey
import kotlin.reflect.ExperimentalAssociatedObjects
import kotlin.reflect.KClass

@OptIn(ExperimentalAssociatedObjects::class)
@AssociatedObjectKey
@Retention(AnnotationRetention.BINARY)
actual annotation class ApolloAdaptableWith(public actual val adapter: KClass<*>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.apollographql.apollo3.api

import okio.FileSystem

internal actual val systemFileSystem: FileSystem
get() = throw IllegalStateException("There is no SYSTEM filesystem on JS")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.apollographql.apollo3.api

actual inline fun <reified T> Any.apolloUnsafeCast(): T {
TODO()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.apollographql.apollo3.api

import com.apollographql.apollo3.annotations.ApolloAdaptableWith
import com.apollographql.apollo3.api.json.JsonWriter
import kotlin.reflect.ExperimentalAssociatedObjects
import kotlin.reflect.findAssociatedObject

@OptIn(ExperimentalAssociatedObjects::class)
actual fun Operation.Data.toJson(
jsonWriter: JsonWriter,
customScalarAdapters: CustomScalarAdapters,
) {
@Suppress("UNCHECKED_CAST")
val adapter = this::class.findAssociatedObject<ApolloAdaptableWith>() as Adapter<Any>

adapter.obj(false).toJson(jsonWriter, customScalarAdapters, this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import okio.IOException
import okio.Path.Companion.toPath
import okio.buffer

internal expect val HOST_FILESYSTEM: FileSystem

open class SourceAwareException(
val error: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.apollographql.apollo3.ast

import okio.FileSystem

internal expect val HOST_FILESYSTEM: FileSystem
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.apollographql.apollo3.ast

import okio.FileSystem

internal actual val HOST_FILESYSTEM: FileSystem
get() = TODO("Not yet implemented")
1 change: 1 addition & 0 deletions libraries/apollo-debug-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ apolloLibrary(
withLinux = false,
withApple = false,
withJs = false,
withWasm = false
)

kotlin {
Expand Down
3 changes: 2 additions & 1 deletion libraries/apollo-engine-ktor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ plugins {

apolloLibrary(
javaModuleName = "com.apollographql.apollo3.engine.ktor",
withLinux = false
withLinux = false,
withWasm = false
)

kotlin {
Expand Down
7 changes: 4 additions & 3 deletions libraries/apollo-mockserver/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ plugins {
id("org.jetbrains.kotlin.multiplatform")
}

apolloLibrary (
javaModuleName = "com.apollographql.apollo3.mockserver",
withLinux = false
apolloLibrary(
javaModuleName = "com.apollographql.apollo3.mockserver",
withLinux = false,
)

kotlin {
Expand Down Expand Up @@ -52,3 +52,4 @@ kotlin {
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.apollographql.apollo3.mockserver

import com.apollographql.apollo3.annotations.ApolloExperimental

@ApolloExperimental
actual fun TcpServer(port: Int): TcpServer {
TODO("Not yet implemented")
}
1 change: 1 addition & 0 deletions libraries/apollo-mpp-utils/api/apollo-mpp-utils.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ public final class com/apollographql/apollo3/mpp/Platform : java/lang/Enum {
public static final field Js Lcom/apollographql/apollo3/mpp/Platform;
public static final field Jvm Lcom/apollographql/apollo3/mpp/Platform;
public static final field Native Lcom/apollographql/apollo3/mpp/Platform;
public static final field WasmJs Lcom/apollographql/apollo3/mpp/Platform;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/apollographql/apollo3/mpp/Platform;
public static fun values ()[Lcom/apollographql/apollo3/mpp/Platform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ expect fun currentThreadName(): String
enum class Platform {
Jvm,
Native,
Js
Js,
WasmJs
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.apollographql.apollo3.mpp

private fun currentTimeMillis2(): Double = js("(new Date()).getTime()")
actual fun currentTimeMillis(): Long {
return currentTimeMillis2().toLong()
}

actual fun currentTimeFormatted(): String = js("(new Date()).toISOString()")

actual fun currentThreadId(): String {
return "wasm-js"
}

actual fun currentThreadName(): String {
return currentThreadId()
}

actual fun platform() = Platform.WasmJs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.apollographql.apollo3.cache.normalized.api.internal

internal actual class CacheLock actual constructor() {
actual fun <T> lock(block: () -> T): T {
return block()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.apollographql.apollo3.cache.normalized.api.internal

internal actual class CacheLock actual constructor() {
actual fun <T> lock(block: () -> T): T {
return block()
}
}

0 comments on commit 1c939f9

Please sign in to comment.