Gradle 如何使用kotlin multiplatform创建的Javascript工件

Gradle 如何使用kotlin multiplatform创建的Javascript工件,gradle,kotlin,kotlin-multiplatform,kotlin-js,Gradle,Kotlin,Kotlin Multiplatform,Kotlin Js,我正在为jvm和js创建一个kotlin多平台项目。我的build.gradle.kts如下所示: plugins { id ("org.jetbrains.kotlin.multiplatform") } repositories { mavenCentral() } kotlin { jvm() { compilations["main"].kotlinOptions{ jvmTarget = "1.8" }

我正在为jvm和js创建一个kotlin多平台项目。我的build.gradle.kts如下所示:

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"))
            }
        }
    }
}
(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')));
当我构建项目时,会创建一个common.js,其内容如下所示:

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"))
            }
        }
    }
}
(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')));
现在,我在项目外部创建了一个简单的javascript文件,并尝试使用创建的代码(如教程中所示,他们创建了一个应用程序,该应用程序使用mutlipplatform项目作为依赖项)。这似乎是不可能的,因为创建的代码没有导出任何函数等。我希望能做像这样的事情

const common = require('common');

function myTest () {
    console.log(common.Platform('Hello', 'World'));
}

您对如何构建项目以实现这些目标有何想法?

您是否尝试过实际运行上述代码?如果是,会发生什么?这个问题有些含糊不清,所以很难确定到底是什么问题

无论如何,让我以一种快速而肮脏的方式来分享我所做的事情。也许这个概念的证明会帮你解除障碍

  • 我在我的Kotlin/JS项目中创建了这样的Kotlin文件:
  • 我构建了这个项目,并在
    build/distributions/project name.JS
    中得到了结果JS文件

  • 我在具有JS控制台的web浏览器中打开了任何网页,并像这样粘贴了
    project name.JS
    文件的代码-这相当于您尝试使用
    require

  • 我创建了一个
    TestLibraryClass
    的实例:
  • 我能够调用这两个函数,第一个函数使用自定义的给定名称以避免名称损坏,第二个函数使用损坏的名称:
  • 我学习了名称混乱(以及如何解决它)。请注意,类名
    TestLibraryClass
    没有损坏。我测试了顶级函数的名称是否被破坏



    关于大局——如果官方支持准备用Kotlin编写的JS库——我这里没有完全清楚。一旦我发现这个问题,我会很乐意更新这个答案。我也在考虑将Kotlin/JS合并到我公司的项目中。

    你能给你的项目附加一个链接吗?
    testLibraryClassInstance = new importedKotlinLib.TestLibraryClass()