gradle - 如何使用由 kotlin 多平台创建的 Javascript 工件

标签 gradle kotlin kotlin-multiplatform kotlin-js

我正在为 jvm 和 js 创建一个 kotlin mutliplatform 项目。我的 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"))
            }
        }
    }
}

当我构建项目时,会创建一个 common.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 文件并尝试使用创建的代码(就像你在教程中看到的 - 他们创建了一个使用 mutliplatform 项目作为依赖项的应用程序)。这似乎是不可能的,因为创建的代码没有导出函数等。我希望能够做这样的事情
const common = require('common');

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

您对如何构建项目以能够完成这些事情有想法吗?

最佳答案

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

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

  • 我在 Kotlin/JS 项目中创建了这样的 Kotlin 文件:
  • class TestLibraryClass {
        @JsName("appendNumberToString")
        fun appendNumberToString(string: String, number: Int) =
            "Appending: $string - $number"
    
        fun functionWithoutJsNameAnnotation(someStr: String)
                = someStr
    }
    
  • 我建了项目,得到了结果JS文件在build/distributions/project-name.js .
  • 我在带有 JS 控制台的 Web 浏览器中打开任何网页,并粘贴了 project-name.js 的代码。像这样的文件 - 这相当于您尝试使用 require :
  • importedKotlinLib = ...pasted the code here...
    [hit enter]
    
  • 我创建了一个 TestLibraryClass 的实例:
  • testLibraryClassInstance = new importedKotlinLib.TestLibraryClass()
    
  • 我能够调用这两个函数,第一个使用自定义名称以避免名称修改,第二个使用修改名称:

  • JS console dump

    我了解了名称修改(以及如何解决它)here .请注意类名TestLibraryClass没有被破坏。我测试了顶级函数的名称是否被破坏。

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

    关于gradle - 如何使用由 kotlin 多平台创建的 Javascript 工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60187271/

    相关文章:

    eclipse - 非多项目环境的 Gradle 依赖项

    android - Kotlin 变量被初始化两次

    java - 如何在Android Studio中获取用户输入的数值并将其转换为kotlin中的变量int?

    IntelliJ 中的 Kotlin 多平台 JVM 类型不匹配

    java - Gradle 无法使用 JavaFX 插件从 src/test 访问模块 src/main 中定义的类

    java - 带有 gradle 和 groovy 的 eclipselink 元模型

    java - 如何通过提交适当的 .YAML 文件以编程方式调用 Argo

    android - 线程中的 KMM In CorrectDereferenceException

    kotlin - 无法在 commonMain 中为 kotlin 多平台使用依赖项

    android - 为flutter项目添加Firebase无法正常工作