ice1000
ice1000

Reputation: 6579

How can I use KotlinJS without the stdlib?

Since in most cases we have this:

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

And the Kotlin standard library seems to large for me.
I want to minimize it by creating my own stdlib, which can be smaller, by declaring only external methods I need.

I tried to remove that method, it compiles, but the generated JS code has this:

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'.");
}

Which means that there must be something necessary in the stdlib, which I don't know.

As my requirements are described above, is there any workarounds?
Or how can I reduce the size of the generated code?

Upvotes: 4

Views: 1297

Answers (2)

user9017713
user9017713

Reputation:

Here's an alternative. You can kick out the kotlin runtime and make use of this method declaration:

external fun js(code: String): dynamic

Like, if you're using console.log, just write

js("console").log("Hey man")

And remove the check you mentioned in the description. It'll work.

Upvotes: 3

s1m0nw1
s1m0nw1

Reputation: 82007

You should make use of Kotlin's kotlin-dce-js Plugin, which does exactly what you want: Minimize the code to what you really use and eliminate the "dead code".

See here: 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."

Upvotes: 6

Related Questions