Overview of Kilua
main@Composable functions and state management) and KVision users (using component-based APIs with some imperative interaction capabilities).repository·main·Indexed 18 days ago
https://github.com/rjaros/kiluaA modern, composable web framework for Kotlin/Wasm and Kotlin/JS powered by the Compose Runtime. Kilua provides a declarative UI API, fullstack RPC capabilities, and true Server-Side Rendering (SSR) with support for server modules including Ktor, Spring Boot, Micronaut, Javalin, Jooby, and Vert.x. It allows developers to build high-performance web applications using @Composable functions and state management.
@Composable functions and state management) and KVision users (using component-based APIs with some imperative interaction capabilities).Kilua supports true SSR by allowing the same application code to run in both the browser and a Node.js environment. This is achieved by using a compatible router class.
RichText, Tabulator) render as simple HTML placeholders on the server.To build and run Kilua examples, you must have JDK 21 or later installed. Use ./gradlew on Unix-like systems or gradlew.bat on Windows.
Use the -t flag for continuous build mode (webpack dev server) on http://localhost:3000.
For fullstack or SSR applications, additional tasks are available for running backend servers and exporting static sites.
# Run JS target in continuous build mode
./gradlew -t :examples:[exampleName]:jsBrowserDevelopmentRun
# Run Wasm target in continuous build mode
./gradlew -t :examples:[exampleName]:wasmJsBrowserDevelopmentRun
# Build production JS application
./gradlew :examples:[exampleName]:jsBrowserDistribution
# Build production Wasm application
./gradlew :examples:[exampleName]:wasmJsBrowserDistribution
# Run backend for fullstack/SSR development
./gradlew :examples:[exampleName]:jvmRun
# Export static site with JS frontend
./gradlew :examples:[exampleName]:exportWithJsTo build and run Kilua applications, you must have JDK 21 or later installed. The project provides different Gradle tasks depending on your target platform (JS or Wasm) and your preferred build tool (Vite or Webpack).
On Windows, use gradlew.bat instead of ./gradlew.
# Example: Run the Vite dev server for the JS target
./gradlew jsViteRunA basic Kilua application extends the Application class and overrides the start() method. Inside start(), you use the root(id) function to define the application entry point in the DOM. State management is handled using Compose primitives like remember and mutableStateOf.
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import dev.kilua.Application
import dev.kilua.CoreModule
import dev.kilua.compose.root
import dev.kilua.html.button
import dev.kilua.html.div
import dev.kilua.html.unaryPlus
import dev.kilua.startApplication
class App : Application() {
override fun start() {
root("root") {
var state by remember { mutableStateOf("Hello, world!") }
div {
+state
}
button("Add an exclamation mark") {
onClick {
state += "!"
}
}
}
}
}
fun main() {
startApplication(::App, CoreModule)
}Kilua uses Gradle tasks to manage development servers and production builds for both JavaScript (JS) and WebAssembly (Wasm) targets. Tasks are categorized by the build tool used (Vite vs. Webpack/Browser Distribution).
### Vite Tasks
# JS Target
./gradlew jsViteRun # Run Vite dev server on http://localhost:3000
./gradlew -t jsViteCompileDev # Continuous development compilation
./gradlew jsViteBuild # Build production to build/vite/js/dist
# Wasm Target
./gradlew wasmJsViteRun # Run Vite dev server on http://localhost:3000
./gradlew -t wasmJsViteCompileDev # Continuous development compilation
./gradlew wasmJsViteBuild # Build production to build/vite/wasmJs/dist
### Webpack / Browser Distribution Tasks
# JS Target
./gradlew -t jsBrowserDevelopmentRun # Continuous build mode on http://localhost:3000
./gradlew jsBrowserDistribution # Build production to build/dist/js/productionExecutable
# Wasm Target
./gradlew -t wasmJsBrowserDevelopmentRun # Continuous build mode on http://localhost:3000
./gradlew wasmJsBrowserDistribution # Build production to build/dist/wasmJs/productionExecutableTo launch a Kilua application, use the startApplication function. It requires a reference to a class that extends Application and an optional Hot configuration object (typically used for Hot Module Replacement in development environments).
fun main() {
startApplication(::App, webpackHot())
}Within the start method of an Application, use the root(id: String) function to define the application's root container. The id parameter must match an existing element in your HTML (e.g., <div id="root"></div>). Inside the root block, you can use Kilua's HTML DSL components like div, button, and text nodes (using the + operator) to build your interface.
root("root") {
div {
+"Counter value: $counter"
}
button("Increment") {
onClick {
counter++
}
}
}To create a Kilua application, extend the Application class and implement the start and dispose methods:
start(state: String?): The entry point for your UI logic. It receives an optional state string, which can be used to restore application state (e.g., from SSR or a previous session). Inside start, use root("id") to attach your component tree to a specific HTML element.dispose(): String: Called when the application is being shut down. You should return a string representation of the current application state so it can be passed back into start during the next initialization.class App : Application() {
var counter by mutableStateOf(0)
override fun start(state: String?) {
if (state != null) {
counter = state.toInt()
}
root("root") {
// UI components go here
}
}
override fun dispose(): String {
return counter.toString()
}
}