Query on Tree Shaking in Kotlin-JS Compilation with ES6 in Kotlin 1.9.0:
I'm working on a Kotlin-JS project where I have two Kotlin files (
hello.kt
and
request.kt
) and a specified Gradle configuration. After compiling to JavaScript and using the
dev.petuska.npm.publish
for publishing, I'm observing an unexpected behavior in the bundle size when importing different functions.
hello.kt
@JsExport
fun SayHi() {
console.log("hi")
}
request.kt
@JsExport
fun sendRequest() {
// Code using HttpClient and GlobalScope.launch
GlobalScope.launch {
HttpClient(Js).request {
url {
this.protocol = URLProtocol.HTTPS
this.host = "www.test.com"
}
}
}
}
Gradle Configuration:
plugins {
kotlin("js") version "1.9.0"
id("dev.petuska.npm.publish") version "3.4.1"
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0")
implementation("io.ktor:ktor-client-core:3.0.0-beta-1")
implementation("io.ktor:ktor-client-js:3.0.0-beta-1")
}
kotlin {
js {
binaries.library()
useEsModules()
moduleName = "js-kotlin"
browser {
webpackTask {
outputFileName = "js-kotlin.js"
output.library = "jsKotlin"
}
}
generateTypeScriptDefinitions()
}
}
tasks.withType<KotlinJsCompile>().configureEach {
kotlinOptions {
moduleKind = "es"
useEsClasses = true
}
}
import in JS project :
import {SayHi} from 'js-kotlin/js-kotlin.mjs'
import {sendRequest} from 'js-kotlin/js-kotlin.mjs'
When I import
{SayHi}
and
{sendRequest}
from my compiled module, I'm noticing that the import cost for both imports is similar. I was expecting tree shaking to reduce the size for the
{SayHi}
import, considering it doesn't use the heavier
ktor
dependency.
Am I missing something in my configuration that's preventing tree shaking from effectively reducing import cost ?