Why can’t I create new instances of this from Kotl...
# javascript
m
Why can’t I create new instances of this from Kotlin?
Copy code
@JsModule("@emotion/stylis")
@JsNonModule
external class Stylis(
	options: StylisOptions = definedExternally,
)

val stylis = Stylis() // runtime: Stylis is not a constructor
At runtime
Stylis
is actually a Module and
Stylis.default
is the actual class 🤔
Mhh, I’m probably running into this: https://youtrack.jetbrains.com/issue/KT-41650
👌 1
b
Add @JsName("default") to pint that declaration to module default
m
Doesn’t work with the IR compiler b/c of a bug 😞
Looking for workarounds…
t
@Marc Knaup Please share workaround here too
m
I think I also misunderstood
@JsName
. It changes the name of the local variable for the import, it doesn’t change what it actually imported.
b
It points to what to import. At least it did before ir came
m
It changes both when I use
@Js(Non)Module
on a file. The local variable is now
default
and it also tries to import
.default
.
The former is not intended 🤔
b
On Legacy?
m
IR
b
Wait, try your (non)module annotation on file
m
That’s what I just did.
b
And the add jsname default on the class representing the entity that's actually exported as default
t
Last step:
Copy code
val module: JsClass<*> =  require("@emotion/stylis").default

fun Stylis(options: StylisOptions? = null) = js("(new module(options))")
b
Can you try that with legacy compiler?
t
It works on legacy
m
I abandoned legacy a while ago because I kept running into issues. I want to make it work with IR.
@JsName("_default")
results in:
Copy code
var _default = $module$_emotion_stylis._default;
expected:
Copy code
var Stylis = $module$_emotion_stylis._default;
t
Do you need workaround for final app only?
m
yes
no
well
I want to make it a lib at some point
t
Gradle plugin - bad WA in that case? Right?
m
I have no idea what you’re talking about :D
t
m
That’s quite an elaborate hack 😅
🧌 1
t
It works! 🙂
You can fix it manually
Copy code
@JsModule("@emotion/stylis")
@JsNonModule
external class Stylis(
	options: StylisOptions = definedExternally,
)

// quite an elaborate hack
fun fixStylis() {
    js("Stylis = Stylis.default")
}
m
In that case I have to make assumptions what the local variable is called 🤔 And how does it affect optimizations?
t
Another
Stylis
can kill WA 🙂
m
What’s WA? 🤔
t
WorkAround
m
lol
t
And how does it affect optimizations?
Webpack optimization?
m
JS minification for example
I’ve also created this one: https://youtrack.jetbrains.com/issue/KT-43312
I’ll go with the
Stylis = Stylis.default
workaround until KT-41650 is solved. There simply isn’t any other workaround apart from modifying the Stylis package itself.
t
JS minification for example
It my examples it works for
commonjs
Posibly such logic not supported in es modules (because sideeffect). But es modules still not supported 😞