russhwolf / multiplatform-settings

A Kotlin Multiplatform library for saving simple key-value data
Apache License 2.0
1.69k stars 67 forks source link

Can't resolve 'MultiplatformSettings-multiplatform-settings-jsLegacy' #83

Closed EmmanueleVilla closed 3 years ago

EmmanueleVilla commented 3 years ago

I'm building a js file using the target:

js("js") {
    useCommonJs()
}

I take the build/classes/kotlin/js/main/shared.js file and copy it inside the src folder of a react project. I then import it with

import './shared.js'

When I build the react project, I have the following error:

./src/shared.js
Module not found: Can't resolve 'MultiplatformSettings-multiplatform-settings-jsLegacy' in '/Users/emmanuele.villa/sdp/app-web/src'

Do you have idea what's the cause?

russhwolf commented 3 years ago

I'm not totally sure. Can you share a project so I could see the issue in action?

For reference, this is the setup that the sample project uses which is slightly different: https://github.com/russhwolf/multiplatform-settings/blob/master/sample/app-browser/build.gradle.kts It gets run from this html file: https://github.com/russhwolf/multiplatform-settings/blob/master/sample/app-browser/src/main/resources/index.html#L24

EmmanueleVilla commented 3 years ago

I guess the main difference is that you are building a "multiplarform js client app", while instead I'm building only the shared library and manually copy the generated .js file inside a native react project.

In fact, inside the js generated by your sample app, there's no reference to a 'MultiplatformSettings-multiplatform-settings-jsLegacy' module if I built it correctly, but if you move the "binaries.executable()" part inside the shared library and look at the shared.js, then that module import appears.

It seems that the js generated by a library and the one generated by an application are veeery different!

I don't think I can make a sample project 'till monday, meanwhile I'll experiment a bit and let you know, thanks!

EmmanueleVilla commented 3 years ago

Sorry for the late reply, I think that I was using the targets in a wrong way: if I only write "useCommonJs()", intellij literally explodes and can't find any dependencies in any file, I need to also add "browser()" to have it working.

Anyway, I've resolved the issue by adding "browser", and use the "jsBrowserProductionWebpack" task to generate a .js in the "correct" format. Then I simply throw away the auto-generated index.html and just use that shared.js in my native react project!

I still don't know if the problem with the target is an issue with that configuration or an intelliJ bug, nevertheless since there is a "workaround" (or maybe it's the way we're supposed to do that?) I think this issue could be closed.

russhwolf commented 3 years ago

Thanks for updating me!

Kotlin/JS does require that you explicitly enable one or both of the browser or node targets. See https://kotlinlang.org/docs/js-project-setup.html#execution-environments