Compile Kotlin to JavaScript - intellij-idea

I've started looking into Kotlin and I'd like to use it instead of TypeScript for my front end language. Both compile down to JavaScript so I'd like to set it up so that I make a Kotlin file, lets call it myFile.kt, then when I run the compiler it makes a myFile.js file.
Just like how TypeScript takes a myFile.ts and compiles it to a myFile.js.
I'm using the latest version of IntelliJ 15 with release candidate 1 of Kotlin.
I've been searching all over the internet for the way to do this but so far all I've found is setting up IntelliJ so that Kotlin creates a JavaScript library from your code in a JAR file. I also haven't been able to get that to compile any of the kt files in my project (they were previously js files).
Is what I'd like to do currently possible or am I thinking about this in the wrong way?

I think this should help:
https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject/libraryProject/build.gradle
There are sample of library project. JS sources can be founded in buildDir.
So yes, you can get js files from Kotlin sources.
Kotlin language a very different from JavaScript (even ES6), so you can't just rename js to kt, this will not work. You need rewrite javascript source files to Kotlin.
For example:
console.log('Hello, World!');
Should be rewritten as:
fun main(args: Array<String>) {
println("Hello, World!")
}

Related

Kotlin testing the restriction in DSL

We are writing a DSL in Kotlin, and we have added a few restrictions in that DSL.
For eg,
table {
tr {
td {
}
}
}
Here, we restrict td should not be called inside another td.
How do we test this? Is there any dsl-testkit or matcher library in kotlin that lets us do something like "shouldNot compile" does in scalatest
One solution is to use the Kotlin scripting engine, make it run a script with your DSL constructs that should be rejected and assert that the script didn't compile.
Links that you may find useful:
Run Kotlin Scripts (kts) from regular Kotlin programs by Kotlin Expertise Blog
s1monw1/KtsRunner, a library that encapsulates running Kotlin scripts programmatically
A newer and more powerful, though more complex scripting support implementation on the Kotlin side is described in KEEP-75

Kotlin/Native How to use a header-only C/C++ library

So I'm trying to use stb_image in my Kotlin/Native project and I am having trouble trying to include it in my project. It's a header only library and konan seems to expect a compiled object file anyways so I was wondering if there is any way of just generating the cstubs and then using the header for linking unless I have to compile a basic translation file since stb_image only requires you to have a translation unit that defines STB_IMAGE_IMPLEMENTATION however I have that defined in my compilerOpts -GSTB_IMAGE_IMPLEMENTATION. Would it be easier to just compile a translation unit, create the static object, and then link against it or does K/N have some way of doing that for me?
I am using Gradle Multiplatform so if there is some gradle script I can run then please let me know.
My -GSTB_IMAGE_IMPLEMENTATION is supposed to be -DSTB_IMAGE_IMPLEMENTATION and I needed to put my -I switch in my compilerOpts not linkerOpts.
I recommend actually creating a translation file but it's not required.
You can just give the header file with the compileropts as you've done and that should work.
You can look at this as a reference. I'm working on a wrapper in my free time.

Use kotlin DSL from a separate file

I'm trying to write a maven plugin with kotlin DSL that creates a configuration object. The plugin will depend on that configuration during its operation.
I think I need to use the DSL like following: in runtime load a separate file with kotlin script, execute it and assign result to a variable in the plugin code.
Is there any good way to do it?
It turns out there is a simple answer:
https://github.com/JetBrains/kotlin/blob/master/libraries/examples/kotlin-jsr223-local-example/

Error: Declaration annotated with #OptionalExpectation can only be used in common module sources

Update (2018-11-03)
This is a known issue that is being tracked here.
Original Post
I am playing around with Kotlin MPP, specifically with Kotlin 1.3 and the new structure. After converting a Kotlin 1.2 MPP to the new structure, I keep getting these errors:
Error:(3, 18) Kotlin: [MPP_jvmMain] Declaration annotated with '#OptionalExpectation' can only be used in common module sources
These are referring to the #JsName() annotations I have within my common module.
I have tried:
Starting a completely new MPP from scratch using the built-in wizard, and simply adding #JsName("test") to the hello() function that comes with the generated sample. Even that seems to cause problems.
Invalidating caches/restarting IntelliJ
Using the latest versions of everything (Kotlin 1.3, IntelliJ IDEA 2018.2.5 Ultimate)
Calling ./gradlew build from the command line. This works.
Sample project here
Link Kotlin Issue
This was fixed within v1.3.10 of the Kotlin plugin for IntelliJ.

Dynamically compiling AIR apps

I've been searching for a way to dynamically compile AIR apps on the go.
Specifically, I'd need the title of the program and a couple variables changed on compile. I'll be creating hundreds, if not thousands, of versions of what is essentially the same program so I'd like to avoid doing it by hand :)
Is this possible with AIR? If not, could I do it with something like Java?
Yes, this is possible using the mxml compiler in the Flex sdk. It requires some knowledge of a build tool like ant or maven.
Automate the build process with apache ant or maven.
Use Actionscript compiler directives to define place holders for your variable data in your code.
Modify the build scripts to compile the build, and pass in the variable data as compiler arguments
I've done this with ant build scripts to include a timestamp and other info in the app's version string. Here's example with a timestamp...
1. Define variables in the compiler argument options for your project
The syntax is:
-define=NAMESPACE::VARIABLE_NAME,'variableValue'
For example:
-define=APPDATA::TIMESTAMP,"xxx"
This is what a developer would put in their IDE. When they compile their apps, the version string will have "xxx" in it. When the automated build script compiles the app, it passes in an actual timestamp using a similar argument.
2. Reference the variable in your code.
I'm using it as a String, but you can do Boolean and I assume the other primitive types.
public var appVersion:String = "MyApp " + APPDATA::TIMESTAMP;
You can conditionally compile code into the SWF with booleans:
-define=CONFIG::DEBUG,true
Now wrap code blocks with this compiler directive, and if true it will be compiled into the swf:
public function something():int
{
var a:int = 1;
CONFIG::DEBUG
{
a=2;
}
return a;
}
While this is pretty handy, I recommend only using compiler directives for things you really need to do at compile time, and not for general configuration :)
Resources:
Using conditional compilation
mxmlc compiler options
flex ant tasks
flex mojos maven plugins