I'm currently experiencing an issue where I am unable to call any Kotlin JS function and receiving an error stating that 'something' is not defined.
Initially, I attempted compiling the project with Gradle but found success after following this tutorial and compiling it with NPM. You can find my project attached here
EDIT: I tested it with Maven and it worked. However, since Maven is deprecated, I prefer using Gradle or NPM.
Here is the HTML code:
<body>
<script src="test.js"></script> // the file is generated in bin/bundle
<script>
(function() {
let a = new test.Test(); //test - module, Test - my class, the error occurs at this line
a.test(); //test - method in the class Test
})()
</script>
</body>
However, I always encounter:
Uncaught ReferenceError: test is not defined
The package.json looks like this:
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "shx rm -rf bin && webpack && shx rm -rf bin/build/kotlin-test*",
"test": "mocha bin/test"
},
"author": "",
"license": "ISC",
"dependencies": {
"kotlin": "^1.3.70"
},
"devDependencies": {
"@jetbrains/kotlin-webpack-plugin": "^3.0.2",
"kotlin-test": "^1.3.70",
"mocha": "^7.1.0",
"shx": "^0.3.2",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11"
},
"description": ""
}
This is the webpack.config.js:
const KotlinWebpackPlugin = require('@jetbrains/kotlin-webpack-plugin');
module.exports = {
entry: 'test',
resolve: {
modules: ['bin/build', 'node_modules'],
},
output: {
path: __dirname + '/bin/bundle',
filename: 'test.js',
},
plugins: [
new KotlinWebpackPlugin({
src: __dirname,
output: 'bin/test',
moduleName: 'test',
moduleKind: 'commonjs',
librariesAutoLookup: true,
packagesContents: [require('./package.json')],
}),
new KotlinWebpackPlugin({
src: __dirname + '/src',
output: 'bin/build',
moduleName: 'test',
moduleKind: 'commonjs',
metaInfo: true,
sourceMaps: true,
librariesAutoLookup: true,
packagesContents: [require('./package.json')],
}),
],
};
And here's my class:
class Test {
fun test() {
println("test")
}
}
EDIT: Here is the npm compilation of test.js:
(function (_, Kotlin) {
'use strict';
var println = Kotlin.kotlin.io.println_s8jyv4$;
var Kind_CLASS = Kotlin.Kind.CLASS;
function Test() {
}
Test.prototype.test = function () {
println('test');
};
Test.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'Test',
interfaces: []
};
_.Test = Test;
Kotlin.defineModule('test', _);
return _;
}(module.exports, require('kotlin'))); //error: module is not defined
//# sourceMappingURL=test.js.map
The Maven Kotlin plugin output of test.js looks like this:
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'test'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'test'.");
}var test = function (_, Kotlin) {
'use strict';
var println = Kotlin.kotlin.io.println_s8jyv4$;
var Kind_CLASS = Kotlin.Kind.CLASS;
function Test() {
}
Test.prototype.test = function () {
println('test');
};
Test.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'Test',
interfaces: []
};
_.Test = Test;
Kotlin.defineModule('test', _);
return _;
}(typeof test === 'undefined' ? {} : test, kotlin);