I'm trying to use <https://github.com/lightningtgc...
# javascript
j
I'm trying to use https://github.com/lightningtgc/MProgress.js in my KotlinJS project. I'm using
org.jetbrains.kotlin.frontend
and have "mprogress" as a
dependency
. I've set up this external class:
Copy code
external class Mprogress(params: Json) {
    fun start()
    fun end()
    fun set(percent: Double)
    fun inc()
}
However, when I try to create a new Mprogress object, it says it can't locate Mprogress' JS definition. How can I make sure it's included?
g
What do you mean? You build system should handle dependencies and bundle them
j
Right... I'm pretty sure I bundled it properly in Gradle, but it still can't locate the JS definition. Here's my build.gradle: https://gist.github.com/jdkula/77756288b475e88a65581c63097cf6a5 We have:
Copy code
...
kotlinFrontend {
    ...
    npm {
        ...
        dependency "mprogress"
        ...
    }
    ...
}
...
g
Did you check your result js bundle?
j
Nope, sorry, that's something I probably shoulda checked -- lemme do that real fast.
Nope, it doesn't appear in the
Module.bundle.js
file. What other steps do I need to do? I thought that the dependency clause was it?
Hmm... the module.exports for
mprogress
uses a factory function: in its source file,
module.exports = factory()
, which later returns an object
MProgress
that has all the definitions. I just did this:
Copy code
@JsModule("mprogress")
external class Mprogress(params: Json) {
    fun start()
    fun end()
    fun set(percent: Double)
    fun inc()
}
Is there a way to reference that module's "main export" or something like that?
g
I'm not familiar with this library, but how would you you use this lib from JS code?
module.exports = factory ()
is just part of module declaration
Why do you have
@JsModule("mprogress")
instead of
@JsModule("Mprogress")
?
j
I used that because that's the npm module name (when you install it) -- I'll try it capitalized next time I'm at the computer.
g
npm module name is not always equal to CommonJS module name, and according the docs it should be “Mprogress” instead
j
Hm. I tried that, but still no dice. I think it's just a quirk with this module... other npm modules seem to work okay. Thanks for the help, though!
g
@jonathan I know what is problem with this dependency. Mprogress doesn’t provide commonjs module. To fix this you need something like that:
Copy code
@JsModule("mprogress/mprogress.min.js")
external class Mprogress() {
    fun start()
    fun end()
    fun set(percent: Double)
    fun inc()
}
JsModule declaration will be used by Webpack to generate proper module declaration
j
Excellent. Thanks so much!
g
Don’t forget to use the same name for external class as js class
Mprogress
in this case
👍 1