icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.07k stars 121 forks source link

Multimodule project js can't resolve files, images, localizations #609

Open nevrozza opened 9 months ago

nevrozza commented 9 months ago

Module not found: Error: Can't resolve 'images/layer.png' in 'C:\Files\Code\Melody\build\js\packages\Melody-jsApp\kotlin' Did you mean './images/layer.png'?

Work good on jvm and android, but on JS i have this problem.

Requests that should resolve in the current directory need to start with './'. Requests that start with a name are treated as module requests and resolve within module directories (node_modules). If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

This error appears when i add these file to generated path.

utils build.gradle.kts `plugins { id("browser-setup") id("android-setup") id("multiplatform-setup") id("dev.icerock.mobile.multiplatform-resources") }

kotlin {

sourceSets {
    val commonMain by getting {
        dependencies {
            api(Deps.Moko.Resources.res)
        }

    }
    val androidMain by getting {
        dependsOn(commonMain)
    }
    val desktopMain by getting {
        dependsOn(commonMain)
    }
    val jsMain by getting {
        dependsOn(commonMain)
    }
    val iosMain by getting {
        dependsOn(commonMain)
    }

}

}

multiplatformResources { multiplatformResourcesPackage = "com.melody.melody" }`

Alex009 commented 9 months ago

hi! did you add id("dev.icerock.mobile.multiplatform-resources") plugin in your web app build.gradle ?

nevrozza commented 9 months ago

Hello! It works now (почти). For two days I thought that I had it as web app's plugin... Now I added it in web app build.gradle and changed utils build.gradle (multiplatformResourcesPackage -> "com.melody.melody.common.utils").

But now I have a different problem: ERROR in ./kotlin/Melody-common-utils.js 84:16-43 Module not found: Error: Can't resolve 'images/layer.png' in 'C:\Files\Code\Melody\build\js\packages\Melody-jsApp\kotlin' Did you mean './images/layer.png'? Requests that should resolve in the current directory need to start with './'. Requests that start with a name are treated as module requests and resolve within module directories (node_modules). If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too. I think problem in this: ` public actual object images : ResourceContainer { public actual val layer: ImageResource = ImageResource(fileUrl = js("require(\"images/layer.png\")") as String, fileName = "layer.png")

public override fun values(): List<ImageResource> = listOf(layer)

} `

feozdax commented 9 months ago

Hey,

Did you resolve your issue @nevrozza ? I encounter the exact same problem on my multi modules project.

nevrozza commented 8 months ago

@feozdax Did you encounter this problem in the first stage or in the second?

feozdax commented 8 months ago

I am stuck at this part Module not found: Error: Can't resolve 'images/ic_works_medium.svg' in '/Users/Documents/workspace/Synthese/build/js/packages/web/kotlin'.

I already added id("dev.icerock.mobile.multiplatform-resources") plugin to my web app build.gradle.kts. But still same issue

nevrozza commented 8 months ago

do you have a discord? I can drop you peaces of my project

feozdax commented 8 months ago

Sorry for the delay. I finally understood what was wrong.

What is really important, is to apply plugin dev.icerock.mobile.multiplatform-resources to every module involved into resources management. And second point, you need to expose your dependencies via api and not implementation. Otherwise, resources will not be available for final module (web app for instance). This is why I had this issue "Module not found: Error: Can't resolve 'images/ic_works_medium.svg' "

Now everything is fine. Thank for the help @nevrozza