Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure with multiplatform projects #71

Open
jonninja opened this Issue May 21, 2018 · 9 comments

Comments

Projects
None yet
9 participants
@jonninja
Copy link

jonninja commented May 21, 2018

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

This comment has been minimized.

Copy link

NorbertSandor commented Jun 20, 2018

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

This comment has been minimized.

Copy link
Contributor

JakeWharton commented Jul 2, 2018

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

@LDVSOFT

This comment has been minimized.

Copy link

LDVSOFT commented Aug 31, 2018

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

@wem

This comment has been minimized.

Copy link
Contributor

wem commented Aug 31, 2018

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

This comment has been minimized.

Copy link

Lewik commented Nov 20, 2018

This helps for me in another project. Thanks.

@allanveloso

This comment has been minimized.

Copy link

allanveloso commented Dec 7, 2018

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

This comment has been minimized.

Copy link

piacenti commented Feb 6, 2019

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

This comment has been minimized.

Copy link

piacenti commented Feb 6, 2019

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

This comment has been minimized.

Copy link

Danilo-Araujo-Silva commented Feb 27, 2019

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"
                }
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.