https://kotlinlang.org logo
Title
o

OrfeasZ

04/12/2023, 2:46 AM
What's the proper way to do something like
import "some-library"
(or
require("some-library")
) for a polyfill library so it can be loaded and can inject its globals before
main()
is called?
a

andylamax

04/12/2023, 4:26 AM
fun main() {
   require("some-library")
   // do the rest here
}
o

OrfeasZ

04/12/2023, 10:40 AM
That doesn't work since the static initializers of another library I use run before this and require the polyfills.
Right now I've made a js file that requires the polyfills and I include that by adding it to the start of
config.entry.main
in my webpack config, but it doesn't feel like the best solution.
b

Big Chungus

04/12/2023, 11:04 AM
@JsModule("name") external val someShit: dynamic
Then just hold a reference to it in main() to avoid dce removal fun main() { someShit .... }
o

OrfeasZ

04/12/2023, 11:05 AM
Does it matter if the module doesn't export a field like that? (it actually doesn't export anything)
b

Big Chungus

04/12/2023, 11:05 AM
Nope
It's just loading it for side effects
o

OrfeasZ

04/12/2023, 11:05 AM
alright, will give this a try, thanks ❤️
b

Big Chungus

04/12/2023, 11:06 AM
FYI someShit holds a reference to the entire module object, not its inner entities like fields or functions
o

OrfeasZ

04/12/2023, 11:06 AM
Ooooh, that makes a lot more sense
It's only with
@file:JsModule
that we deal with actually exported values yeah?
b

Big Chungus

04/12/2023, 11:08 AM
Correct. @file unwraps module object and maps its properties to externals declared within the file
o

OrfeasZ

04/12/2023, 11:08 AM
Alright cool, didn't know there was a difference between the two. Good to know, thanks a lot.
This seems to work as expected 👍
b

Big Chungus

04/12/2023, 11:12 AM
You can use this trick to load any side effects like css or scss imports for example
o

OrfeasZ

04/12/2023, 11:12 AM
anything that webpack is configured to handle I assume?
b

Big Chungus

04/12/2023, 11:13 AM
Yes. This is basically kt equivalent to this in js import "some-module" Or import "./some/file.scss"
You get the gist
o

OrfeasZ

04/12/2023, 11:14 AM
Nice
Thanks again
b

Big Chungus

04/12/2023, 11:14 AM
No problem