Skip to content

Commit b29e8d6

Browse files
update: adding info about configuring web targets and when to use them (#5032)
* update: adding info about configuring web targets and when to use them * chore: rephrased target explanation * SME review * Aleksey review
1 parent c4a6277 commit b29e8d6

File tree

1 file changed

+96
-4
lines changed

1 file changed

+96
-4
lines changed

docs/topics/gradle/gradle-configure-project.md

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,68 @@ Learn more about [Kotlin Multiplatform for different platforms](https://www.jetb
635635
636636
It's recommended to use Android Studio for creating Android applications. [Learn how to use the Android Gradle plugin](https://developer.android.com/studio/releases/gradle-plugin).
637637
638-
## Targeting JavaScript
638+
## Targeting the web
639639
640-
When targeting JavaScript, use the `kotlin-multiplatform` plugin as well. [Learn more about setting up a Kotlin/JS project](js-project-setup.md)
640+
Kotlin, through Kotlin Multiplatform, offers two approaches for web development:
641+
642+
* JavaScript-based (using the Kotlin/JS compiler)
643+
* WebAssembly-based (using the Kotlin/Wasm compiler)
644+
645+
Both approaches use the Kotlin Multiplatform plugin but support different use cases.
646+
The sections below explain how to configure each target in your Gradle build and when to use them.
647+
648+
### Targeting JavaScript
649+
650+
Use Kotlin/JS if your goal is to:
651+
652+
* Share business logic with a JavaScript/TypeScript codebase
653+
* Build non-shareable web apps with Kotlin
654+
655+
For more information, see [Choose the right web target for a Kotlin Multiplatform project](https://www.jetbrains.com/help/kotlin-multiplatform-dev/choosing-web-target.html).
656+
657+
When targeting JavaScript, use the `kotlin-multiplatform` plugin:
658+
659+
<tabs group="build-script">
660+
<tab title="Kotlin" group-key="kotlin">
661+
662+
```kotlin
663+
plugins {
664+
kotlin("multiplatform") version "%kotlinVersion%"
665+
}
666+
```
667+
668+
</tab>
669+
<tab title="Groovy" group-key="groovy">
670+
671+
```groovy
672+
plugins {
673+
id 'org.jetbrains.kotlin.multiplatform' version '%kotlinVersion%'
674+
}
675+
```
676+
677+
</tab>
678+
</tabs>
679+
680+
Configure the JavaScript target by specifying whether it should run in the browser or the Node.js environment:
681+
682+
```kotlin
683+
kotlin {
684+
js().browser { // or js().nodejs
685+
/* ... */
686+
}
687+
}
688+
```
689+
690+
> See [further details about the Gradle configuration for JavaScript](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-dsl-reference.html#web-targets) and learn more about [setting up a Kotlin/JS project](js-project-setup.md).
691+
>
692+
{style="note"}
693+
694+
### Targeting WebAssembly
695+
696+
Use Kotlin/Wasm if you want to share both logic and UI across multiple platforms. For more information,
697+
see [Choose the right web target for a Kotlin Multiplatform project](https://www.jetbrains.com/help/kotlin-multiplatform-dev/choosing-web-target.html).
698+
699+
As with JavaScript, use the `kotlin-multiplatform` plugin when targeting WebAssembly (Wasm):
641700
642701
<tabs group="build-script">
643702
<tab title="Kotlin" group-key="kotlin">
@@ -660,9 +719,42 @@ plugins {
660719
</tab>
661720
</tabs>
662721
663-
### Kotlin and Java sources for JavaScript
722+
Depending on your requirements, you can target:
723+
724+
* **`wasmJs`**: for running in browsers or Node.js
725+
* **`wasmWasi`**: for running in Wasm environments supporting [WASI (WebAssembly System Interface)](https://wasi.dev/), such as Wasmtime, WasmEdge, and so on.
726+
727+
Configure the `wasmJs` target for web browsers or Node.js:
728+
729+
```kotlin
730+
kotlin {
731+
wasmJs {
732+
browser { // or nodejs
733+
/* ... */
734+
}
735+
}
736+
}
737+
```
738+
739+
For WASI environments, configure the `wasmWasi` target:
740+
741+
```kotlin
742+
kotlin {
743+
wasmWasi {
744+
nodejs {
745+
/* ... */
746+
}
747+
}
748+
}
749+
```
750+
751+
> [See further details about the Gradle configuration for Wasm](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-dsl-reference.html#web-targets).
752+
>
753+
{style="note"}
754+
755+
### Kotlin and Java sources for the web target
664756
665-
This plugin only works for Kotlin files, so it is recommended that you keep Kotlin and Java files separate (if the
757+
The KGP only works for Kotlin files, so it is recommended that you keep Kotlin and Java files separate (if the
666758
project contains Java files). If you don't store them separately, specify the source folder in the `sourceSets{}` block:
667759
668760
<tabs group="build-script">

0 commit comments

Comments
 (0)