Last Updated | 30 September 2016 |
In this tutorial we'll see how to
Kotlin generate JavaScript code that is compatible with Asynchronous Module Definition (AMD), CommonJS and Universal Model Definition (UMD).
We can configure the Kotlin compiler option to use any of these. The last option (UMD) will generate UMD and fallback to the other options if one is not available. Currently Kotlin compiler options are per IntelliJ IDEA project as opposed to a Kotlin module.
If using Maven or Gradle, we can also configure the module output format. For more information see JavaScript Modules.
When using AMD, we set the compiler option to use AMD. Once we do that, we can then reference any modules that we've defined as if they were regular AMD ones.
For instance, given
class Customer(val id: Int, val name: String, val email: String) { var isPreferred = false fun makePreferred() { isPreferred = true } }
the following JavaScript code will be generated
define('customerBL', ['kotlin'], function (Kotlin) { 'use strict'; var _ = Kotlin.defineRootPackage(null, /** @lends _ */ { Customer: Kotlin.createClass(null, function Customer(id, name, email) { this.id = id; this.name = name; this.email = email; this.isPreferred = false; }, /** @lends _.Customer.prototype */ { makePreferred: function () { this.isPreferred = true; } }) }); Kotlin.defineModule('customerBL', _); return _; });
Assuming we have the following project layout
we could define our index.html
to load require.js
along with main.js
as the value of the data-main
attribute
<head> <meta charset="UTF-8"> <title>Sample AMD</title> <script data-main="scripts/main" src="scripts/require.js"></script> </head>
The contents of our main.js
would be:
requirejs.config({ paths: { kotlin: 'out/lib/kotlin.js', customerBL: 'out/customerBL' } }); requirejs(["customerBL"], function (customerBL) { console.log(customerBL) });
With this, we can then access any of the functionality defined inside customerBL
.
In order to use Kotlin with node.js, we need to set the compiler option to use CommonJS. Once we do that, the output of the application should be accessible using the node module system.
For instance, given
class Customer(val id: Int, val name: String, val email: String) { var isPreferred = false fun makePreferred() { isPreferred = true } }
the following JavaScript code will be generated
module.exports = function (Kotlin) { 'use strict'; var _ = Kotlin.defineRootPackage(null, /** @lends _ */ { Customer: Kotlin.createClass(null, function Customer(id, name, email) { this.id = id; this.name = name; this.email = email; this.isPreferred = false; }, /** @lends _.Customer.prototype */ { makePreferred: function () { this.isPreferred = true; } }) }); Kotlin.defineModule('customerBL', _); return _; }(require('kotlin'));
The last line is invoking the function itself and passing as argument kotlin
, which refers to the standard library. This can be obtained in one of two ways:
Local reference
When compiling, the compiler always outputs the kotlin.js file. The easiest way to reference this without having to refer to paths, is to set the output library folder for the compiler options to node_modules
. This way, Node will automatically pick it up as it does an exhaustive search for files under these folders
NPM Directory
The Kotlin standard library is available on npm and we can simply include it in our package.json
as a dependency.
{ "name": "node-demo", "description": "A sample of using Kotlin with node.js", "version": "0.0.1", "dependencies": { "kotlin": ">=1.0.4-eap-111" } }
We can simply refer to any class or member function inside our node.js code by simply importing the module using require
:
var customerBL = require('./scripts/customerBL') var customer = new customerBL.Customer(1, "Jane Smith", "[email protected]") console.dir(customer) customer.makePreferred() console.dir(customer)
In this case, we've set the output of our compilation to the scripts
folder. On running the application we should see the following output:
© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/tutorials/javascript/working-with-modules/working-with-modules.html