deezer/KustomExport — 69★ trên GitHub (Kotlin). DEPRECATED : A JS facade generator for KotlinJS / KMP
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
This tool was helpful with Kotlin 1.5 especially. Since then, Kotlin javascript support has evolved and this tool is not so relevant anymore:
the added cost doesn't deliver enough value (generated code impacts total bundle size).
the extra dependency to this tool is too risky for most projects
as the only developer on it, I'm not using it for years and don't plan to maintain it anymore.
KustomExport: a KSP generator of JS facade
Status: Experimental
At Deezer, we develop KMP libraries, this tool help us provide nicer API for Typescript since November 2021.
The generated code will change to support more features and/or stop supporting features provided by KotlinJS directly, so you may expect breaking changes when upgrading.
Motivation
Providing a nice Typescript API can sometimes be complex/verbose from a Kotlin Multiplatform Project.
A simple example:
// @JsExport // Cannot be annotated in Kotlin 1.6.10 and before
enum class StateEnum { IDLE, RUNNING }
@JsExport
data class SomeDataObject(
val timestamp: Long,
val state: StateEnum,
val idList: List<String>
)
Long will not produce a number but a kotlin.Long behing a any (doc), but web developers usually use number to store timestamp.
Enums are not handled yet (KT-37916) and so exported as any
List could be used in a majority of cases if it was exported in Arrays
Lots of Kotlin methods like componentX, copy, hashCode or equals coming from data class are not really relevant or unusable (copy() should define all fields in typescript anyway)
comments in the Typescript generated code is helpful but only comment and doesn't fail build when type changes
There are some good reasons why it's not supported by KotlinJs right now, but it's not practical to provide a clean Typescript API.
KustomExport wants to be the bridge to provide a nice Typescript API until KotlinJS produces an equivalent export.
Technical approach
A manual way to approach this problem is to write a facade in Kotlin and only @JsExport the facade.
// jsMain/StateEnumJs.kt
// Export the enum in a class, so it's providing a real class in JS instead of 'any'
@JsExport
class StateEnumJs internal constructor(internal val stateEnum: StateEnum) {
val name: String = stateEnum.name
}
fun StateEnumJs.import(): StateEnum = value
fun StateEnum.export(): StateEnumJs = Encoding(this)
// Object that exposes all possible values of the enum (note the 's')
@JsExport
object StateEnumsJs {
val IDLE: StateEnumJs = StateEnumJs(StateEnum.IDLE)
val RUNNING: StateEnumJs = StateEnumJs(StateEnum.RUNNING)
}
// And more methods like values(), valueOf()...
It doesn't scale easily: lots of boilerplate code to maintain.
New feature can be invisible when changes are non-breaking with the facade. A dangerous behaviour for library providers.
It requires calling import()/export() method in many places, leading to a "specific to web" code in your common code.
If you write similar facades yourself, this generator could help you avoid writing them manually. Please open issues with your needs!
Note that it's adding code to your existing codebase, so it will increase your JS bundle size.
Current status
The current project uses Typescript integration tests, feel free to check what's covered in Samples.
What is supported today:
Long to number (by using toLong/toDouble, so be careful with precision issues here!) (doc)
List<...> to Array<...> and it's ready to support more collections, please open a ticket with your needs. (doc)
enum classes (doc)
class / data class (equals/toString/componentX methods are removed) (doc)
functions and dynamic properties are wrapped (ex val rand: Long get() = Random.nextLong() will be wrapped and called again each time the exposed object is called in Typescript)
interfaces (doc)
sealed class
suspend methods with cooperative cancellation (via AbortController)
removing the namespace (optional) (doc)
mixing @JsExport with @KustomExport (experimental)
What is not supported yet:
generics (partially supported, you can generate a list of facades from a single generic class)
Feel free to open issues for more features!
You can have a look to the Samples to have a feel of how it can be used.
Setup
KustomExport use KSP (Kotlin Symbol Processor), and you need to install it in your build.gradle.kts
plugins {
kotlin("multiplatform")
id("com.google.devtools.ksp") version "1.6.21-1.0.5"
}
Then you need to define the dependency to the library
repositories {
mavenCentral()
maven(url = "https://raw.githubusercontent.com/Deezer/KustomExport/mvn-repo")
}
kotlin {
// jvm(), js() and other platforms...
sourceSets {
val commonMain by getting {
dependencies {
implementation("deezer.kustomexport:lib:<version>")
implementation("deezer.kustomexport:lib-coroutines:<version>")
}
}
}
}
/*
* Copyright 2021 Deezer.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
deezer/KustomExport có 69 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/deezer/KustomExport. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
deezer/KustomExport có phải mã nguồn mở không?
Có — deezer/KustomExport phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/deezer/KustomExport.
deezer/KustomExport có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho deezer/KustomExport. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
deezer/KustomExport có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho deezer/KustomExport. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
deezer/KustomExport còn đang phát triển không?
Commit gần nhất trên deezer/KustomExport là 1.6 năm trước (theo timestamp GitHub). Repo có 9 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc thêm về deezer/KustomExport ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/deezer/KustomExport là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về KustomExport?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về KustomExport.