COMMUNITY

Can you compile node modules to Hashlink target?

Hi,
I started to play a bit with Haxe in the last weeks and I am confused by the compilation.
My question is, is it possible to write Haxe/Heaps code that uses Node modules and compile it to Hashlink so the different targets of Hashlink are available.
I have looked around, but the conversations I found on the subject didn’t clarify the point for me.

Reading from the Wikipedia page:
Code written in the Haxe language can be source-to-source compiled into ActionScript 3, JavaScript, Java, C++, C#, PHP, Python, Lua and Node.js. Haxe can also directly compile SWF, HashLink and Neko bytecode.

The first part is clear enough, I think. It means that an application programmed in Haxe can take, for example, Node code and compile it to any of the targets listed in the first part.
The second part is not as clear. The way I understand it is that Haxe code can be compiled in Hashlink, but I am not sure that it can be done with code from another language than Haxe (such as Node).

I am not certain either if Heaps influence what source languages can be used when compiling to Hashlink.

I hope that this haven’t been answered elsewhere.

Thanks in advance.

Have a good one.

What the wiki page is stating is that Haxe can be transpiled (source-to-source compile) into those languages. (Haxe -> JavaScript for example)

Haxe is also directly compiled (source-to-bytecode) into SWF, HashLink and Neko. Meaning there’s no source-code to then compile for those 3 targets, the output bytecode can be consumed immediately.

You cannot take non-Haxe languages and compile them to HashLink or vice versa.

HashLink is a VM specifically for running HashLink bytecode which can only be produced from Haxe source-code. Unless you can get node/npm code/modules to transpile into Haxe, getting them to HashLink isn’t possible.

Also, HashLink has no targets (well, other than HL/C), it is a target. It will run on any platform that has the hl native executable. If you want it to run without HashLink installed, compile to HL/C; compilation (probably) takes longer, but you end up with an executable.

Hi Nemo!

The Haxe compiler (haxe) can compile Haxe code to those various target languages (and also to HashLink (and even JVM) bytecode). It cannot compile some other language to Haxe code.

HashLink is a VM that runs HashLink bytecode.

(haxe (the compiler) can also generate C code (by first generating HashLink bytecode and then automatically converting that to C code).)

AFAIK, Heaps is a Haxe library. You include it in your project, and call out to it from your project, and then when you build your project with haxe (the compiler), at that point you can decide which target language (or bytecode) you wish to compile the whole enchilada to.

If you want to use Node.js, that means you would be targeting JavaScript — not HashLink. If targetting JavaScript and using Node, I think you have two options:

  • You can have an existing JavaScript and Node project, within which you write some Haxe code, and use haxe to compile your code into JavaScript, then run the JavaScript+Node project as usual.

  • You create a Haxe project that makes use of Node and JS libs via the various Haxe externs libraries that bind to and allow you to use JavaScript code/libraries.

Note, I’m only just learning about JS and Node, but I found some docs about those by Matthijs Kamstra that might be useful:

Thank you for your fast answers Piboistudios and Uvtc.

That pretty much confirms what I thought, but it is way clearer now.
Especially the differentiation between compiling and transpiling, that was the missing point in understanding the Wikipedia page.
Thanks for explaining the steps from Haxe to executable, that is very helpful.

Thanks for the links as well, Uvtc.
I will still try to figure how to convert JS to Haxe however, because there is a lot of useful modules that I would like to have access to in my applications, but I’m keep then at hand, I’m sure it will be handy some time in the future.

I am now trying to figure out how to transpile Javascript to Haxe. I found the ‘refactor (4.2.0)’ Haxe Library (https://lib.haxe.org/p/refactor/).
It states on the project page that it can convert Javascript code to Haxe.
Would I be right in saying that this is the kind of code I should look at to better understand how to transpile JS to Haxe code?
I suppose that it may not be working too well anymore as it is already a year old and Haxe 4 has been released since, but it may be a good starting point.
The other thing is that I’m pretty sure that not every modules could be transpiled that way, especially modules affecting hardware.

Your help is very appreciated!

Have a good day.

Good luck with that. I heard there might be a depressing talk about exactly that topic later this month.

Really?
Would you have more information about the talk? I’d be interested in hearing that.

You’ll need to have a little bit of patience.

No problem, I’m not in a hurry, I am just curious.
Thank you.