A nice and exciting feature about haXe is conditional compilation. That means one can specify which code to compile depending upon the target chosen at the compile time. Hah, that does not make sense ? Ya, I know. Let me explain again.
There is a compiler and while providing argument to it we have to specify which target we are compiling to. Like in case of haXe and NME we can compile it to a lot of different targets. The compiler argument for flash or swf output looks as below
haxelib run nme test app.nmml flash
Now we can compile the code to HTML5 with the following argument
haxelib run nme test app.nmml html5
Thats the beauty of the language. Most of the time its the same code, but then there are times when we need to write different codes for different targets (may be layout, view elements, can be anything). But then one does not need to keep a separate project for all the different cases. The solution is conditional compiling. What it does is depending upon the target type specified to compiler, it picks up the right code from the same file. Whoaa!! Magic
How does it do it or how we make it aware of this ?! You asked it. Lets see it.
#if flash //Code for SWF output #elseif js //Code for HTML5/js output #else #end
Thats simple. Its one file, completely in itself, but when it comes to compiler it picks up the right code part depending upon the target type.
One thing to remember though, that when specifying HTML5 target, it picks up conditions from "js", it does not have a "html5" option. This particular thing took me sometime to figure out. Just thought would point out here saving someone's time out there.
Happy haxe -ing