ankur2037
01/05/2024, 8:35 AMhello.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 ?Artem Kobzar
01/05/2024, 9:12 AM"sideEffects": false
into the package.json for js-kotlin
packageankur2037
01/05/2024, 12:53 PMMárton Matusek
01/05/2024, 1:14 PMankur2037
01/08/2024, 5:29 AMArtem Kobzar
01/08/2024, 9:50 AMankur2037
01/16/2024, 11:45 AMIlya Goncharov [JB]
01/22/2024, 2:22 PMIlya Goncharov [JB]
01/24/2024, 1:36 PM//region block: post-declaration
initMetadataForLambda(sendRequest$slambda, VOID, VOID, [1]);
//endregion
//region block: exports
export {
SayHi as SayHi,
sendRequest as sendRequest,
};
//endregion
And if I comment initMetadataForLambda
, webpack output will be more expectedArtem Kobzar
01/24/2024, 1:54 PMankur2037
02/07/2024, 9:05 AMArtem Kobzar
02/07/2024, 11:07 AMankur2037
02/07/2024, 12:46 PMArtem Kobzar
02/07/2024, 1:11 PMankur2037
02/07/2024, 1:12 PMArtem Kobzar
02/07/2024, 1:21 PMankur2037
02/17/2024, 5:38 AMArtem Kobzar
02/18/2024, 2:56 PMgradle.properties
like this:
kotlin.js.ir.output.granularity=per-file
ankur2037
02/19/2024, 6:41 AMArtem Kobzar
02/19/2024, 8:42 AMArtem Kobzar
02/19/2024, 8:42 AMankur2037
02/22/2024, 11:38 AMArtem Kobzar
02/22/2024, 12:04 PMArtem Kobzar
02/22/2024, 12:30 PMankur2037
02/24/2024, 8:31 AM