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 #5458

Merged
merged 3 commits into from
Dec 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation(libs.android.plugin)
implementation(libs.gradle.japicmp.plugin)
implementation(libs.vespene)
implementation(libs.kotlinx.serialization.json)
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
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
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)
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}

@Suppress("UnstableApiUsage")
Expand Down
45 changes: 7 additions & 38 deletions build-logic/src/main/kotlin/Mpp.kt
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
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
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
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
@@ -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
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
Expand Up @@ -27,5 +27,10 @@ kotlin {
api(libs.kotlin.stdlib.js)
}
}
findByName("wasmJsMain")!!.apply {
dependencies {
api(libs.kotlin.stdlib.wasm.js)
}
}
}
}
@@ -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<*>)
@@ -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")
@@ -0,0 +1,5 @@
package com.apollographql.apollo3.api

actual inline fun <reified T> Any.apolloUnsafeCast(): T {
TODO()
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -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)
}
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
@@ -0,0 +1,5 @@
package com.apollographql.apollo3.ast

import okio.FileSystem

internal expect val HOST_FILESYSTEM: FileSystem
@@ -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
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
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
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 {
}
}
}

@@ -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
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
Expand Up @@ -24,7 +24,8 @@ expect fun currentThreadName(): String
enum class Platform {
Jvm,
Native,
Js
Js,
WasmJs
}

/**
Expand Down
@@ -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
@@ -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,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()
}
}