Unable to access kotlin from javascript

I’m trying to build a kotlin project which is supposed to be a base library for other smaller script which may be written in kotlin or in the javascript. When I build the project with maven everything works as expected. However when I build it with npm or gradle I always get a weir results.

When compiling with npm together with webpack, the final bundle file contains all required code. However names of my functions and variables get mangled so they are no longer accessible from javascript.

Compiling with gradle is similar except it doesn’t even include my sources into a bundled js file, but only include the kotlin std lib. And if I try to load the kotlin file first (from build/kotlin-js-min/main) and then js file in build/js/mymodule.js I get an error saing “module is not defined”

I wen’t through probably all available example projects or tutorials but without success.

I posted an example project here
In gradle branch is gradle project

My js code:

<body>
<script src="test.js"></script> //file generated in bin/bundle
<script>
    (function() {
        let a = new test.Test(); //test - module, Test - my class, error occurrs at this line
        a.test(); //test - method in class Test
    })()
</script>
</body>

Thank you

Hi there,

This is a Webpack module type configuration issue, if I set the output.libraryTarget to this, I can do

new this['Test']().test()

in my browser console and it displays the expected output. Unfortunately my skills webpack-wise are incredibly limited so I can’t help you any further.

1 Like

Thank you, I set it to plain and now it works correctly.