Kotlin.browser import missing

Hi

I have an existing kotlin app, producing some html. This works very well, and I enjoy it quite a lot. I use some javascript in there as well, through unsafe+raw. Which is a bit hacky, and I’d like to try replacing it with kotlin-js.

I added the imports for kotlin-stdlib-js and kotlinx-html-js. I thought that’d be enough to get me the kotlin.browser import, but it seems it’s not. The import is red in intellij.

In debugging this I loaded up the kotlin-fullstack-sample, did a gradle clean build on command line - it works. BUT, the kotlin.browser. import is still red in intellij?!

(This is using the latest intellij, the build.gradle file from kotlin-fullstack, and I invalidated my caches and restarted)

Cannot reproduce. I have successfully opened kotlin-fullstack-example with IDEA 2017.3.3 and Kotlin 1.2.20 .
kotlin.browser is not red, and navigation to kotlin.browser.window works properly.

Could you report the actual versions of IDEA and kotlin plugin?

Also if the plugin version is older than 1.2.20, could you try upgrading to it and see if the problem is still present?

Intellij 2017.3.3 (ultimate), was runnnig 1.2.10, and upgrading to 1.2.20 works. Thanks!

Cool! I’ve heard there were some issues with Gradle synchronization in 1.2.10. Must have been one of them.

I see the ApplicationPage.kt is generating html in the same way I am in my app, but it’s using script { unsafe {}} to return some actual javascript for that page.

Is it possible using kotlin2js to have that return a lambda which is compiled to javascript and injected into the html response, or am I missing something here?

I’d like to not return strings :stuck_out_tongue:

If you want to compile Kotlin to JS on the fly, I don’t think there is an easy way. It should be possible though, because Kotlin web demo does exactly that. If you are curious check out the source code.

If you want to create the JS snippet beforehand, you could set up a separate Kotlin/JS module with the desired functionality and then inject it’s compilation output here.

There are some issues with this approach though. For example you’ll have to run DCE. The resulting JS code would probably be quite a bit bigger, because I doubt DCE will get rid of kotlin-stdlib completely. Also this means a more configuration. And you are probably going to have to bundle the .js file into your jar if you want to distribute the resulting server.

In my opinion although this might be possible, it is hardly reasonable to do so in this case. On the other hand if you find yourself injecting lots and lots of JavaScript code, then the benefit of using Kotlin might outweight the additional configuration and size cost.

Hm, this does make sense when thinking about it. Of course injecting a bunch of javascript into html instead of making it proper javascript files and linking to them is the wrong way to go about things - but for small project it really is easy :stuck_out_tongue:

Thanks a lot for your help!

You are welcome