Search code examples
webpackkarma-runnerkotlinkarma-webpack

Karma can't resolve dependency 'kotlin' in kotlin-frontend-plugin


I have been trying to get this to work for a day or so now with no luck. Webpack doesn't seem to be injecting the kotlin npm module into my tests, even though it is loaded by webpack. So it throws the below error saying it can't find it.

Here's the error log:

                          Asset     Size  Chunks         Chunk Names
                           main  1.55 MB       0  [big]  main
classes/java/test/vuekt_test.js  1.37 MB       1  [big]  classes/java/test/vuekt_test.js
chunk    {0} main (main) 1.55 MB [entry]
    [0] ./~/process/browser.js 5.42 kB {0} {1}
    [1] ./~/kotlin/kotlin.js 1.31 MB {0} {1}
    [2] ./js/kitchensink/kotlin/MyComponent.html 2.59 kB {0} {1}
    [3] ./js/kitchensink/kotlin/VueApp.html 725 bytes {0} {1}
    [4] ./~/vue/dist/vue.runtime.esm.js 187 kB {0}
    [5] ./js/vuekt.js 40 kB {0}
    [6] (webpack)/buildin/global.js 509 bytes {0}
chunk    {1} classes/java/test/vuekt_test.js (classes/java/test/vuekt_test.js) 1.36 MB [entry]
    [0] ./~/process/browser.js 5.42 kB {0} {1}
    [1] ./~/kotlin/kotlin.js 1.31 MB {0} {1}
    [2] ./js/kitchensink/kotlin/MyComponent.html 2.59 kB {0} {1}
    [3] ./js/kitchensink/kotlin/VueApp.html 725 bytes {0} {1}
    [7] ./classes/java/test/vuekt_test.js 40.6 kB {1} [built]
webpack: Compiled successfully.
webpack: Compiling...
30 06 2017 08:36:42.464:INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: Connected on socket RaGM1IMTAMuV2JRuAAAA with id 80698172
PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
  Error: Error loading module 'vuekt_test'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'vuekt_test'.
  at classes/java/test/vuekt_test.js:35113

As you can see in the log at this line [1] ./~/kotlin/kotlin.js 1.31 MB {0} {1}, the kotlin-stdlib-js resource is being loaded by webpack as it should.

It is also included in the main bundle that you can see is loaded on this line

main  1.55 MB       0  [big]  main

However, it is not being injected into vuekt_test.js. That's why the error you see above gets thrown.

I don't know how to get it to inject kotlin into it though.

Here is my configuration in build.gradle:

kotlinFrontend {
    npm {
        dependency("vue", "2.3.4")

        devDependency("vue-template-compiler", "2.3.4")
        devDependency("vue-template-es2015-compiler", "1.5.2")
        devDependency("vue-template-loader", "0.3.1") //this requires the above 2 devDependencies

        devDependency("webpack", "2.6.1")
        devDependency("webpack-dev-server", "2.4.4")
        devDependency("css-loader", "0.28.4")
        devDependency("style-loader", "0.18.2")
        devDependency("to-string-loader", "1.1.5")
        devDependency("file-loader", "0.11.2")

        devDependency("karma")
    }

    webpackBundle {
        bundleName = "main"
        contentPath = file('src/web')
        port = 9002
    }

    karma {
        port = 9876
        runnerPort = 9100
        reporters = ["progress"]
        frameworks = ["qunit"]
    }
}

And here is the karma.conf.js file:

var webpackConfig = require("C:\\my_workspace\\vue.kt\\build\\webpack.config.js");
webpackConfig.resolve.modules.push("C:\\my_workspace\\vue.kt\\build\\classes\\java\\test\\vuekt_test.js");

module.exports = function (config) {
config.set({
    "basePath": "C:\\my_workspace\\vue.kt\\build",
    "frameworks": [
        "qunit"
    ],
    "reporters": [
        "progress"
    ],
    "files": [
        "C:\\my_workspace\\vue.kt\\build\\classes\\java\\test\\vuekt_test.js"
    ],
    "exclude": [
        "*~",
        "*.swp",
        "*.swo"
    ],
    "port": 9876,
    "runnerPort": 9100,
    "colors": false,
    "autoWatch": true,
    "browsers": [
        "PhantomJS"
    ],
    "captureTimeout": 5000,
    "singleRun": false,
    "preprocessors": {
        "C:\\my_workspace\\vue.kt\\build\\classes\\java\\test\\vuekt_test.js": [
            "webpack"
        ]
    },
    "plugins": [
        "karma-phantomjs-launcher",
        "karma-qunit",
        "karma-webpack"
    ],
    "client": {
        "clearContext": false,
        "qunit": {
            "showUI": true,
            "testTimeout": 5000
        }
    },
    "webpack": webpackConfig
})
};

What do I need to do to fix this issue?


Solution

  • After some additional trial and error I found that I needed to declare the type of module that the test should be compiled into.

    Adding this to my build.gradle file fixed the problem:

    compileTestKotlin2Js {
        kotlinOptions.metaInfo = true
        kotlinOptions.moduleKind = 'commonjs'
    }