How do I force "require" a module when a function ...
# javascript
j
How do I force "require" a module when a function is called, for npm-provided polyfills?
Copy code
@SomethingGoesHere?
external fun polyfilled(args: Int)
Using @JsModule("npmmodule") at least doesn't work, there's no
require('npmmodule')
output the JS file. I can call
require
manually but that doesn't seem like the right solution.
g
JsModule and JsNonModule annotations are required. Additionally, in gradle you have to include the dependancy implementation(npm("Something")) and you have to include the files in the resources if targeting the browsers too (and then of course call import them with html script tags. Its a little confusing but kind of makes sense. The dependancy is for the compiler, code assist and node, and the external interface declarations. But that's all... so its strictly for development. The two annotations seem to tell the compiler where to look dependant on whether node or browser is running it.
j
Yes, the source gets included fine. If I use @JsModule and @JsNonModule the requirement gets added, but then it looks for polyfilled() export in the module, while the module actually polyfills elsewhere. I just need kotlin to emit the require, nothing else.
g
You can use require("") too but I haven't used that or figured it out. I guess you could also do js("require('something')") if that's really what your after.
j
That's what I'm doing right now, but I'd like a better way as I have to manually call it. There's no init { } call or something of the like to exactly random code if a file is merely imported, is there?
g
I don't think so... I think KJS and JS are kind of isolated inside KJS, definining a var in KJS, it doesn't seem to be visible to code in js("")... apparently. The right way is to use external interfaces. The kotlin folk obviously love strongly typed programming... I'm still trying to understand the advantage of strong typing in HTML/CSS/JS when the end result isn't typed... because this kind of stuff seems to be a clear drawback... there are certainly advantages to strong and dynamic types... but trans-piling a strong to a dynamic or vice versa... it's either gonna be tons of work or you are going to end up with the WORST of both worlds not the best.
IMHO
When you don't want to put too much work into an external interface, use the dynamic type in the declaration and jsObject in the implementation... I've been learning/doing this stuff all week.
a
If you define a
main()
in a Javascript module, it will be executed when the compiled JS is loaded/imported, so you can stick arbitrary module setup code in there.
(for me, it’s always got in the way, as it gets called even when testing etc, but it sounds like it fits here)
j
Oh that's good to know for when I've spun it to its own module! For now I've solved it like this:
Copy code
private var something = js("require(...)")
That gets executed right before main() does, so covers my purposes for now.
If you do the same in Kotlin it'll get delegated until the variable is accessed 😕