javascript - 我如何在没有标准库的情况下使用 KotlinJS?

标签 javascript kotlin

因为在大多数情况下我们有这个:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}

Kotlin 标准库对我来说似乎很大。
我想通过只声明我需要的 external 方法来创建我自己的 stdlib 来最小化它,它可以更小。

我试图删除那个方法,它编译了,但是生成的 JS 代码有这个:

if (typeof kotlin === 'undefined') {
  throw new Error("Error loading module 'streaking'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'streaking'.");
}

这意味着stdlib中一定有一些我不知道的东西。

我的要求如上所述,有什么解决方法吗?
或者如何减少生成代码的大小?

最佳答案

您应该使用 Kotlin 的 kotlin-dce-js 插件,它完全可以满足您的需求:将代码最小化到您真正使用的内容并消除“死代码”。

看这里: https://kotlinlang.org/docs/reference/javascript-dce.html#javascript-dce

"There are several ways you get unused declarations: [...] - You are using a shared library which provides much more functions than you actually need. For example, standard library (kotlin.js) contains functions for manipulating lists, arrays, char sequences, adapters for DOM, etc, which together gives about 1.3 mb file. A simple "Hello, world" application only requires console routines, which is only few kilobytes for the entire file."

关于javascript - 我如何在没有标准库的情况下使用 KotlinJS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509399/

相关文章:

javascript - 在 AngularJS 中使用 "this"

android - 使用 Camera X 和 ML Kit 进行人脸检测和跟踪 - Android

kotlin - 为什么 kotlin 不支持 suspend operator fun getValue?

javascript - Jquery根据用户对其他文本框的输入将值绑定(bind)到文本框

javascript - Angular 嵌套重复中断指令(packery + dragabilly)

javascript - 我想让div在几秒钟后自动显示

kotlin - 为什么 Intellij 不为新的 kotlin 项目创建 build.gradle.kts?

java - 动态加载spring xml配置

android - 为什么 fragment 中 `by lazy` 委托(delegate)的这种奇怪行为

javascript - 是否可以在初始化后取消初始化 jQuery 插件?