Kotlin / kotlin-frontend-plugin

Gradle Kotlin (http://kotlinlang.org) plugin for frontend development
Apache License 2.0
563 stars 69 forks source link

Failure with multiplatform projects #71

Open jonninja opened 6 years ago

jonninja commented 6 years ago

When running a multiplatform build, I get the following:

Uncaught Error: Error loading module 'sample-common-js'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'sample-common-js'.
at eval (sample-common-js.js:2)
at Object.../../../sample-common-js/build/classes/kotlin/main/sample-common-js.js (samplejs.bundle.js:81)
at __webpack_require__ (samplejs.bundle.js:20)
at eval (samplejs.js:14)
at Object../samplejs.js (samplejs.bundle.js:114)
at __webpack_require__ (samplejs.bundle.js:20)
at samplejs.bundle.js:69
at samplejs.bundle.js:72

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

NorbertSandor commented 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...)

JakeWharton commented 6 years ago

Changing the kotlin2js compiler output module type of sample-common-js to 'commonjs' instead of the default, 'plain', should resolve this.

LDVSOFT commented 6 years ago

Already has 'commonjs', did not work for me :(

wem commented 6 years ago

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.

Lewik commented 6 years ago

This helps for me in another project. Thanks.

allanveloso commented 5 years ago

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'
            }
        }
        ...
    }
}
piacenti commented 5 years ago

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')
                }
            }
        }
piacenti commented 5 years ago

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.

Danilo-Araujo-Silva commented 5 years ago

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"
                }
            }
        }
    }
theapache64 commented 4 years ago

Addition to @JakeWharton's comment

Try restarting the IDE