Hello everyone, I'm working on my first mpp proje...
# multiplatform
z
Hello everyone, I'm working on my first mpp project (Commons/JS/JVM) and have some trouble with importing the Webjar generated from the jsMain. How am i supposed to import the Webjar from my jsMain in my jvmMain?
I'm quite new to frontend in general, so it is possible that i didn't understand the concept of a WebJar. But i thougt you add a WebJar dependency to your jvmMain and then you can refer to the assets in your htmls with something like this /webjars/myscript.js
a
You misunderstand multiplatfom. Multiplatform is about sharing source codes. Meaning you write your code once and can use it in any platform. That being said, there is no need to compile your jsLib into a WebJar for you to access it on the jvm. If set correctly, jvm can access the code written in common. Since there is a huge possibility I haven't understood your case. Please, be explicit on what you are trying to accomplish and I will gladly help
👍 1
z
I think i misscomunicated my case. I'm not trying to call code from my JS-Project in my JVM-Project. (That codesharing between my JVM and JS via Commons is working like a charm!) But when i build my Project i get 4 Jar files: A FatJar, a WebJar fom my JS-Project, a JVM-Jar and a Metadata-Jar. What i want is to add the WebJar as "implementation" to the jvmMain-KotlinSourceSet-Dependencies. (I'm using Ktor for my backend and they have a feature for WebJars, but in order to use the feature in need this "implementation").
And thank you for your reply!
a
That I am not aware for now. Are you trying to use ktor to deploy your web application written with kotlin/js?
k
Maybe your project type is multiplatform library ? And it produces library files (jar)
a
My typical mpp projects, are kotlin-react with kotlin/js, ktor as a restfull api and an android client. Other less common ones I have seen are ktor with kotlin-html-jvm plugin together with kotlin-css Does any of your approach fit any of this discription?
z
@Kamil Kalisz Hello, i don't think so (at least when i managed to configure the JS part right)
Copy code
js {
        browser {
            runTask {
                devServer = devServer?.copy(
                        port = 8088,
                        proxy = mapOf("/api" to "<http://localhost:8080>")
                )
            }
        }
    }
Edit: Is there an other way to configure a project as multiplatform library?
@andylamax Hm, i'm using ktor with the HTML-DSL and for the JS part i'm using kotlin-react. I've not heard of Kotlin css, but i'll look into it. Edit: They say kotlin styled is more suitable for browsers
a
The gradle snippet above is not for a multiplatform library, but for a js executable. it is the equivalent of ("com.android.application"). Yes for the browser, if you are using react,
kotlin-styled
is perfect, if you are not using react,
kotllin-css-js
is the right choice.
If you are doing a simple project. you can create a multiplatform project, and put most of your code in common, and the react-part in js, while the ktor part goes in jvm. That way when you build, You don't have any need to copy your WebJar to the jvm sources. If you have a large project, you will create a core project (more like a multiplatform lib). Then create another gradle project with a js target (for the web). This target should depend on the core project. Also create another project with jvm target (for ktor) and make it depend on the MPP lib. So far that is the best way that you can share a kotlin-js, kotlin-jvm project. And that way, incase in the future you need to add more targets, say
android
you just create another gradle module and make it depend on the core lib
z
hm, that sounds good to me. Thanks!
d
Slightly off-topic, but do you have an example that exports Kotlin JS code as a library that can be used in an existing web/reactjs project as npm module? A simple sum function will do. All the samples I see are using kotlin js to create a standalone web application.
z
Sorry i don't have one, but the documentation is quite good on that behalf. Another possible source could be the kotlin 1.4-m2 plugin with the new project wizard.