Hi, I try to create a wrapper around expressjs (wi...
# javascript
m
Hi, I try to create a wrapper around expressjs (with enhancements). I have this code :
Copy code
@JsModule("express")
external class Express {
    fun get(route: String, callback: (req: Request, res: Response) -> Unit)
    fun listen(port: Int, callback: Callback?)
}
And I create a class ExpressKt to overload express :
Copy code
class ExpressKt(private val express: Express = Express()) {
    fun listen(port: Int, callback: Callback) {
        express.listen(port, callback)
    }
}
And finally the code to run :
Copy code
fun main(args: Array<String>) {
    //val app = Express()
    val app = ExpressKt()
    app.listen(3000, {
        println("Listening on port 3000")
    })
}
But when i compile code, the index import the wrong library (
module.exports, require('kotlin'), require('lib'))
If I use directly Express in place of ExpressKt, it's good, I have :
module.exports, require('kotlin'), require('express'))
Do you know why ?
Express and ExpressKt are in another gradle module
Ok, ifI move all the code in the same module, it works. It comes from gradle module split...
a
So did the compiler generate
require('express')
in the
lib.js
originally?
m
Ok, I should set output also in the lib ? You can see the project here : https://github.com/m-maillot/expresskt
I don't have any lib.js
Yes, I have a lib.js in lib/build/classes/kotlin/mail/lib with express
a
If it has
require('express')
then it works as intended
The logic being that you don't need to require a library if you don't use any declarations
If you use it in a seprate module, that module itself should require the library.
Does that make some sense?
m
Not really, I have a subproject gradle to be able to reuse this code. And I want to link my gralde module to this submodule to use this code. KotlinJS can't merge these two modules in one file ?
a
Do you just want to be able to run your code in, say, Node? Or do you want to create a self-contained
js
file? How familiar are you with JS tools (e.g. npm, webpack, etc)?
(there is a number of solutions, not sure which to suggest)
Unless you are a seasoned JS developer, I would suggest starting with https://github.com/Kotlin/kotlin-frontend-plugin
It 'just works' for a lot of cases.
m
I'm a java dev, not a JS dev. I want to use kotlin to dev backend for firebase. I'll look for JS tools to accomplish what I want. Thank for your help !
a
@mmaillot In that case take a look at the kotlin-frontend-plugin I've mentioned. 😃 It sets things up for you so that you can run and test your code, pack it into a bundle, etc. I imagine it should work well for your case.
And the configuration files it generates (inside the
build
directory) is also a reasonable starting point to get familiar with webpack, npm, karma, etc.
Good luck!
👍 1
m
Finally, I use the yarn link and change config to make it works, almost... 😄 Generated index.js code require('index') in place of require('expresskt')... I don't know why if you have an idea ? https://github.com/m-maillot/expresskt/blob/master/demo/output/index.js#L36
a
m
Yes, thank you, it works now 🙂 Is it a good practices to publish lib on npmjs and maven repo and to use it in other project to add dependency into package.json and build.gradle ?
a
Well, that's what we do. If you do that, don't forget to publish
.meta.js
at the minimum, and optionally
js.map
, and
*.kjsm
files for best experience.
m
Yes, thank you !