How to compile a Kotlin file to JavaScript? - kotlin-js

Hi I am getting started with KotlinJS on Node and I've put a very simple kotlin file and I want to compile it using the raw kotlinc-js compiler. Without using gradle
package main
fun heavy() {
(1..10_000_000).forEach { it*it }
}
fun main() {
heavy()
println("Bye JS")
}
Here's the make command I've tried yet without success:
build-js:
kotlinc-js main.kt -output main.kt.js
It compiles fine, but when I attempt to run node main.kt.js:
throw new Error("Error loading module 'main.kt'. Its dependency
'kotlin' was not found. Please, check whether 'kotlin' is loaded prior
to 'main.kt'."); ^
Error: Error loading module 'main.kt'. Its dependency 'kotlin' was not
found. Please, check whether 'kotlin' is loaded prior to 'main.kt'.
at Object. (/home/nanospicer/KotlinProjects/KotlinScripting/main.kt.js:2:9)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47
Ok, so I figured it's missing the kotlin runtime. I'll try same argument as the JVM compiler: -include-runtime which leads to the error:
error: invalid argument: -include-runtime
info: use -help for more information
Then I tried:
build-js:
kotlinc-js main.kt -kotlin-home "/mnt/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2021.2.1/plugins/Kotlin/kotlinc/" -libraries "/mnt/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2021.2.1/plugins/Kotlin/kotlinc/lib/kotlin-stdlib-js.jar" -module-kind commonjs -main call -output main.kt.js
But it still leads to the same error. I also tried removing the libraries flag and it didnt work either

I've found the culprits.
I basically created a nodejs project from IntelliJ IDEA and took a quick read through the gradle tasks which hinted me that it was preparing a node environment for me under the hood.
I took a quick glance at npm.js and found that in fact there's a package called kotlin which is the javascript's stdlib for the compiler.
I finally tweaked the build command on my Makefile to use a module-kind of common-js and it worked out!
//Definitive main.kt I ended up using
fun heavy() {
(1..100_000_000).forEach { it*it }
}
fun main() {
heavy()
println("Bye JS")
}
Install kotlin's stdlib using npm:
$ npm i kotlin --save
And my Makefile build command looks like:
$ kotlinc-js main.kt -module-kind commonjs -main call -output main.kt.js
A small breakdown of the arguments:
-module-kind commonjs it's telling the compiler to generate the code-style for a node project (not quite, but you get the gist)
-main call determines whether the compiled module should invoke the main function. This is because if we set the value to noCall we're not gonna run the main function and it may behave just like a library.
-output main.kt.js the name I chose myself without any reason, as long as it's a .js file you'll be able to run it but I wanted to keep it like this to compare it to another main.js file I was generating!

Related

Error: Could not find or load main class MainKt

In Multiplatform project, I want to use main function inside jvmMain to test the library I’m working on, but when I’m trying to run this it returns an error Error: Could not find or load main class MainKt for following function in Main.kt
fun main() {
println("Hello World!")
}
I've got Main.kt file in a root directory of my module, but putting it under some package is also throwing this error.
I'm using Java 8 JDK:
Are there any additional steps I should make to be able to use main function?
EDIT:
I did additional checks and I found out that when in the project I have android target then in the jvm main function I’m getting this error above. When I’m creating a new project with exactly the same configuration, but without android target it’s working properly, main function compiles and it is printing a message.
Any guess what is causing this problem?

Error building Angular Library : Cannot read property 'toString' of undefined

I have created a custom library to be used in my main angular application...
However, when I try to build the library using ng build lib-name I am getting the error:
**Cannot read property 'toString' of undefined**.
I could not find an exact solution to get rid of this error.
Any suggested actions?
Some reference information:
Angular CLI: 8.3.5
Node: 10.16.3
OS: win32 x64
Angular:
...
Package Version
#angular-devkit/architect 0.803.5
#angular-devkit/core 8.3.5
#angular-devkit/schematics 8.3.5
#schematics/angular 8.3.5
#schematics/update 0.803.5
rxjs 6.4.0
For me, I get this because I prefer inline templates and styles but I forget to include -s -t flags when using ng generate (especially in lib projects where the global inline settings in angular.json don't seem to stick?).
After I do an ng generate I delete the foo.html and foo.scss files to avoid going into a trance while alt-tabbing 20 times in 5 seconds.
Then, I change templateUrl to template: <p>foo works</p> (note: replace the single quotes from the url string with the template's tickmarks).
I then often forget to change the styleUrl['foo.scss'] to styles:[``] and then this error message pops up. This is quite a lame error message from a great tool but I thinks it has something to do with the opacity of webpack.

Instant app with multivariant base module

I've started to implement instant app feature in my application. I managed to transition to base module and properly build an installable app. The base module has 3 build types:
buildTypes {
release {...}
acceptance {...}
debug {...}
When I try to build instant app as a separate module that has build.gradle file:
apply plugin: 'com.android.instantapp'
dependencies {
implementation project(':base')
}
I'm getting below error message:
Cannot choose between the following variants of project :app:
- inchargeAcceptanceBundleElements
- inchargeAcceptanceRuntime
- inchargeAcceptanceUnitTestCompile
...
(much much longer I can give full stacktrace if needed)
I tried to change instantapp/build.gradle:
implementation project(path: ':base', configuration: 'default')
but then I get:
Unable to resolve dependency for ':instantapp#debug/compileClasspath': Failed to transform file 'base-release.aar' to match attributes {artifactType=processed-aar} using transform IdentityTransform
Then the app module itself has 4 product flavors but it shouldn't matter I believe.
Any advice how to run instantapp module ?

PixiJS for Haxe won't import - Type Not Found error in IntelliJ

I'm trying to run the most basic PixiJS for Haxe sample program, but IntelliJ gives a Type Not Found error.
The failing code:
package ;
import pixi.core.Application;
The error message:
Main.hx:3: characters 7-28 : Type not found : pixi.core.Application
I think the issue is PixiJS-specific, as when I follow the same install-and-import steps for other Haxe modules (eg flambe, flixel) they behave as expected. I've tried with both 4.7.1 and 4.5.5, and as both global and project-specific libraries (separately).
(I hybridized several screenshots to save space)
When running from the console, using:
> haxe -main Main -lib pixijs -js output.js
Everything seems to compile (running the compiled code results in a PIXI is not defined error which is a seperate issue).
UPDATE 1, how I'm adding the dependency:
After executing haxelib install pixijs, I follow these steps:
UDPATE 2, the config being run:
If you can't get it working in the dependencies section you could try adding the haxelibs pixijs externs src/ folder as an additional sources root in Project Structure -> Modules -> (module name) -> Sources. That's how it worked for me:

Configuring protractor with cucumber in Intellij

I am trying to configure Protractor with Cucumber in Intellij But I have an error when I run the test with the current configuration.
run configuration
I am receiving the error bellow
Testing started at 18:30 ...
/usr/local/bin/node /Users/marianojover/IdeaProjects/AutomationTest_PRO/node_modules/cucumber/bin/cucumber /Users/marianojover/IdeaProjects/AutomationTest_PRO/features/test.feature --format "../../../Library/Application Support/IntelliJIdea2018.1/CucumberJavaScript/lib/cucumberjs_formatter_v2.js" --format-options "{\"cucumberLibPath\": \"/Users/marianojover/IdeaProjects/AutomationTest_PRO/node_modules/cucumber/lib\"}" --require /Users/marianojover/IdeaProjects/AutomationTest_PRO/features
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module '/Users/marianojover/IdeaProjects/AutomationTest_PRO/node_modules/cucumber/bin/cucumber'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:236:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)
Process finished with exit code 1
The error is not giving information that I can consider useful in order to solve the problem.
Running Cucumber.js 4.x is not currently supported, please follow WEB-33787 for updates
Please make sure you have cucumber added to the node_modules folder which is in your project directory. I hope it is there by looking at the paths in your run configuration.
You can also add cucumber cli path to "Cucumber.js arguments" in your run configuration. The path to add is ./node_modules/.bin/cucumber-js (should be modified based on your directory structure). Please see https://github.com/cucumber/cucumber-js/blob/master/docs/cli.md
If the above does not work then you can also use node.js runner to run your protractor test. Just add a new run configuration and add node.js and then add the parameters in configuration tab of the runner, some are given below -
Working Directory -> This will be the path to your project.
Javascript File -> node_modules\protractor\built\cli.js
Application Paramters -> --specs=<path to your feature file><space><path to your protractor.conf.js>