I'm currentyl creating a kotlin mutliplatform project for jvm and js. My build.gradle.kts looks like this:
plugins {
id ("org.jetbrains.kotlin.multiplatform")
}
repositories {
mavenCentral()
}
kotlin {
jvm() {
compilations["main"].kotlinOptions{
jvmTarget = "1.8"
}
}
js() {
compilations["main"].kotlinOptions{
outputFile = "${buildDir}/nodejs/common.js"
moduleKind = "commonjs"
sourceMap = true
verbose = true
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation (kotlin("stdlib"))
implementation (kotlin("stdlib-common"))
}
}
val commonTest by getting {
dependencies {
implementation (kotlin("test-common"))
implementation (kotlin("test-annotations-common"))
}
}
jvm().compilations["main"].defaultSourceSet {
dependencies {
implementation (kotlin("stdlib-jdk8"))
}
}
jvm().compilations["test"].defaultSourceSet {
dependencies {
implementation (kotlin("test"))
implementation (kotlin("test-junit"))
}
}
js().compilations["main"].defaultSourceSet {
dependencies {
implementation (kotlin("stdlib-js"))
}
}
js().compilations["test"].defaultSourceSet {
dependencies {
implementation (kotlin("test-js"))
}
}
}
}
When I build the project a common.js is created which content looks like this:
(function (_, Kotlin) {
'use strict';
var Kind_CLASS = Kotlin.Kind.CLASS;
var ArrayList_init = Kotlin.kotlin.collections.ArrayList_init_287e2$;
var emptyList = Kotlin.kotlin.collections.emptyList_287e2$;
var NotImplementedError_init = Kotlin.kotlin.NotImplementedError;
function commonSharedCode(platform) {
return 'Common: Hi! ' + platform.greetingMethod + '(' + platform.name + ') AND NOW IN BEAUTIFUL ' + platform.beautifulPlatform();
}
function Platform(greetingMethod, name) {
this.greetingMethod = greetingMethod;
this.name = name;
}
Platform.prototype.beautifulPlatform = function () {
return '***' + this.name + '***';
};
// ... //
return _;
}(module.exports, require('kotlin')));
Now I created a simple javascript file outside the project and tried to use the created code (like you see in the tutorials - they create an app that uses the mutliplatform project as a dependency). This seems not to be possible because no function etc. are exported by the created code. I hoped to be able to do things like
const common = require('common');
function myTest () {
console.log(common.Platform('Hello', 'World'));
}
Do you have an idea on how to build the project to be able to do these things?