Skip to content

Commit b1d936f

Browse files
Add extra information about ES modules inside the js-interop document (#4932)
* Add extra information about ES modules inside the js-interop document * chore: apply community suggestions on clarifying the module systems that expose package structure * update: js-interop review * chore: add links to module systems --------- Co-authored-by: alepedroza <[email protected]>
1 parent fe36ae9 commit b1d936f

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

docs/topics/js/js-to-kotlin-interop.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,58 @@ all declarations are available to JavaScript via the `myModule` object. For exam
1414
fun foo() = "Hello"
1515
```
1616

17-
Can be called from JavaScript like this:
17+
This function can be called from JavaScript like this:
1818

1919
```javascript
2020
alert(myModule.foo());
2121
```
2222

23-
This is not applicable when you compile your Kotlin module to JavaScript modules like UMD (which is the default setting
24-
for both `browser` and `nodejs` targets), CommonJS or AMD. In this case, your declarations will be exposed in the format
25-
specified by your chosen JavaScript module system. When using UMD or CommonJS, for example, your call site could look
23+
Calling the function directly like this is not applicable when you compile your Kotlin module to JavaScript modules like [UMD](https://github.com/umdjs/umd) (the default setting
24+
for both `browser` and `nodejs` targets), [ESM](https://tc39.es/ecma262/#sec-modules), [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules), or [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD).
25+
In these cases, your declarations are exposed according to the chosen JavaScript module system.
26+
For example, when using UMD, ESM, or CommonJS, your call site would look
2627
like this:
2728

2829
```javascript
2930
alert(require('myModule').foo());
3031
```
3132

32-
Check the article on [JavaScript Modules](js-modules.md) for more information on the topic of JavaScript module systems.
33+
For more information about JavaScript module systems, see [JavaScript Modules](js-modules.md).
3334

3435
## Package structure
3536

36-
Kotlin exposes its package structure to JavaScript, so unless you define your declarations in the root package,
37-
you have to use fully qualified names in JavaScript. For example:
37+
For most of the module systems (CommonJS, Plain, and UMD), Kotlin exposes its package structure to JavaScript.
38+
Unless you define your declarations in the root package, you have to use fully qualified names in JavaScript.
39+
For example:
3840

3941
```kotlin
4042
package my.qualified.packagename
4143

4244
fun foo() = "Hello"
4345
```
4446

45-
When using UMD or CommonJS, for example, your callsite could look like this:
47+
For example, when using UMD or CommonJS, your call site could look like this:
4648

4749
```javascript
4850
alert(require('myModule').my.qualified.packagename.foo())
4951
```
5052

51-
Or, in the case of using `plain` as a module system setting:
53+
When using `plain` as a module system setting, the call site would be:
5254

5355
```javascript
5456
alert(myModule.my.qualified.packagename.foo());
5557
```
5658

59+
When targeting ECMAScript Modules (ESM), package information is not preserved
60+
to improve the application bundle size and match the typical layout of ESM packages.
61+
In this case, the consumption of the Kotlin declarations with ES modules looks like this:
62+
63+
```javascript
64+
import { foo } from 'myModule';
65+
66+
alert(foo());
67+
```
68+
5769
### @JsName annotation
5870

5971
In some cases (for example, to support overloads), the Kotlin compiler mangles the names of generated functions and attributes
@@ -179,7 +191,7 @@ See how Kotlin types are mapped to JavaScript ones:
179191
| `List`, `MutableList` | `KtList`, `KtMutableList` | Exposes an `Array` via `KtList.asJsReadonlyArrayView` or `KtMutableList.asJsArrayView`. |
180192
| `Map`, `MutableMap` | `KtMap`, `KtMutableMap` | Exposes an ES2015 `Map` via `KtMap.asJsReadonlyMapView` or `KtMutableMap.asJsMapView`. |
181193
| `Set`, `MutableSet` | `KtSet`, `KtMutableSet` | Exposes an ES2015 `Set` via `KtSet.asJsReadonlySetView` or `KtMutableSet.asJsSetView`. |
182-
| `Unit` | Undefined | Exportable when used as return type, but not when used as parameter type. |
194+
| `Unit` | undefined | Exportable when used as return type, but not when used as parameter type. |
183195
| `Any` | `Object` | |
184196
| `Throwable` | `Error` | |
185197
| `enum class Type` | `Type` | Enum entries are exposed as static class properties (`Type.ENTRY`). |

0 commit comments

Comments
 (0)