https://kotlinlang.org logo
c

Carter

05/28/2021, 12:13 PM
Let’s say I’m modularizing a compose web app being served by a JVM ktor server. So I’ve got: browserApp module contains all the compose UI and also image assets. serverApp module contains several rest endpoints plus the root route returns the compose JavaScript client. My question is: is there a way currently to get the image assets in browserApp to be served from serverApp? Currently they aren’t being packaged into the server JAR, and I don’t know if this is possible.
b

Big Chungus

05/28/2021, 12:26 PM
They should be packed. Do you have them in browser resources?
Here's my (non-compoae) fullstack setup. https://www.github.com/mpetuska/kamp/tree/master/app%2Fbuild.gradle.kts Might give you an idea how to serve embedded assets
c

Carter

05/28/2021, 12:41 PM
When you say “in browser resources” what does that mean? If I run the browserApp directly (task browserApp:jsBrowserRun) then the assets are loaded.
Also I’ll take a look at the example thanks! This is probably more of a gradle setup issue instead of compose
Thanks to your help, I did get it finally working. This is what I ended up with, which has improved performance for debug builds by disabling webpack until a flag is set. I also had to work around a minor security issue by stripping the webpack mapping file (otherwise it could accidentally be served if someone could guess the filename).
Copy code
tasks.getByName<Jar>("jvmJar") {
    val webpackTask = run {
        val webpackTaskName = if (isJavascriptMinificationEnabled) {
            "jsBrowserProductionWebpack"
        } else {
            "jsBrowserDevelopmentWebpack"
        }

        tasks.getByPath(":browserApp:$webpackTaskName") as org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
    }
    dependsOn(webpackTask)
    from(File(webpackTask.destinationDirectory, webpackTask.outputFileName)) {
        include("*.js") // ensures the mapping file doesn't get copied
        into("asset/js")
    }

    // Get the resources
    val browserDistributionTask = tasks.getByPath(":browserApp:jsBrowserProductionExecutableDistributeResources")
    dependsOn(browserDistributionTask)
    from(browserDistributionTask.outputs)
}
b

Big Chungus

06/07/2021, 7:49 PM
Glad to hear you've figured it out
9 Views