Open jonninja opened 6 years ago
I had the same issue but something solved it. (Although I didn't modified the project configuration, just wanted to try the previous plugin version - it doesn't work at all with Kotlin 1.2.50...)
Changing the kotlin2js compiler output module type of sample-common-js to 'commonjs' instead of the default, 'plain', should resolve this.
Already has 'commonjs', did not work for me :(
As JakeWharton mentioned, you miss the closure:
compileKotlin2Js { kotlinOptions.moduleKind = 'commonjs' }
In the file https://github.com/jonninja/kotlin-webpack-multimodule-error/blob/master/sample-common-js/build.gradle
This should solve it.
This helps for me in another project. Thanks.
Apply the plugin 'kotlin2js' or 'kotlin-platform-js' after apply the plugin 'kotlin-multiplatform' generates an error:
Cannot add extension with name 'kotlin', as there is an extension already registered with that name.
What did work was:
kotlin {
targets {
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.js, 'js'){
tasks.getByName(compilations.main.compileKotlinTaskName).kotlinOptions {
moduleKind = 'commonjs'
}
}
...
}
}
The commonjs didn't solve the problem for me at least not in the version I'm using (1.3.20). I know the configuration is right because other options like source map generation is working fine. Just in case here is a snippet of the code using default target.
js().compilations.main {
kotlinOptions {
moduleKind = "commonjs"
sourceMap = true
sourceMapEmbedSources = "always"
}
defaultSourceSet {
dependsOn commonMain
dependencies {
implementation kotlin('stdlib-js')
}
}
}
Things got fixed by themselves after I changed a few things. The problem I was having above happened when I use the code from the library above in the common module of another project but not explicitly on the modules that use that common module. The code above compiles a library to 3 jars one for each target js, jvm, common (metadata). On a separate project I have 3 modules backend (jvm), common, frontend (js). I added the correct dependencies to each one of them but then started to use the library code within the common module only. That causes things to break only for the JS code which has no explicit reference to the library classes. What caused the errors to go away was when I tried to use the library classes directly in the JS code. Once that happened, I tried to remove them again from the js code to replicate the errors (keeping the dependency) but I could no longer replicate it even after a clean build.
Using build.gradle.kts
and having a module called common
and one other one called reactApp
, I was able to solve this problem adding the following configuration in both build.gradle.kts
configuration files:
js("js") {
configure(listOf(compilations["main"], compilations["test"])) {
tasks.getByName(compileKotlinTaskName) {
kotlinOptions {
// languageVersion = "1.3"
metaInfo = true
// outputFile = "${project.buildDir.path}/js/${project.name}.js"
sourceMap = true
sourceMapEmbedSources = "always"
moduleKind = "umd"
}
}
}
configure(listOf(compilations["main"])) {
tasks.getByName(compileKotlinTaskName) {
kotlinOptions {
main = "call"
}
}
}
}
Addition to @JakeWharton's comment
Try restarting the IDE
When running a multiplatform build, I get the following:
Things were working fine before, so this appears to be relatively new. I have created a sample project that shows this behavior here: https://github.com/jonninja/kotlin-webpack-multimodule-error