04. January 2015

HAXE JS 101

Hope you know what is HAXE, take a moment to go through the official HAXE documentation. We will discuss how and what to consider while working with JS in HAXE. Generally other languages, which compile to JS, actually compile each specific file to a respective JS file. HAXE actually compiles to one single JS file for our application. We can further take the generated JS file and minify it using popular JS minifiers.

The work flow for HAXE to JS is as simple as

  • Writing the normal HAXE application
  • Use the package structure and class as usual
  • Make a .hxml file difining what would be the final JS file
  • Compile with HAXE compiler to produce the JS file
haxe compile.hxml

The .hxml file looks something like

-main ApplicationEntry
-js app.js

Thats all to it. HAXE will take care of the import and package and class of the application. Finally the generted JS file is having everything our application needs. Include the JS file in the required HTML file and we are done.

Here is a sample basic application showing all there is to it.

Happy Coding.

31. December 2014

CoffeeScript basics

Just to set the expectations right, we need to know something before we begin here.

When I first heard about Class in Javascript it created curiosity on me and the expectation increased as to work like a Classical language(Where Classes are the root of everything) instead of Prototypical language(like Javascript). But then slowly I understood that its not Javascript but other languages on top of Javascript which create somekind of Class concept to mimic Classical languages. The point is everything boils down to Javascript if a language is written to compile or transpile to Javascript . Well, that may seem like a spoiler right now, but its actually not as we will see. Just keep these things in mind all the time, even if we are doing CoffeeScript.

Here is the direct link to the Github repository for the code reference to this post.

  • CoffeeScript is generally written in a .coffee file
  • Each .coffee file is compiled to a .js file
  • Each space (a blank space) in code has a meaning in the .coffee file
  • Semicolons are optional and generally avoided
  • In the beginning you may write more than one classes in a single .coffee file, and thats perfectly alright here
  • By default the CoffeeScript Classes you write are private

When compiled to JS, the contents of each file, are put inside a self invoking function. This is intentional to not pollute the Global variables. So if the content has some classes inside them, then those classes will also be inside the self-invoking function, restricting the access from outside.
The classes in CoffeeScript are compiled to as usual JS functions and the inheritence is done through the prototypical inheritence concepts of Javascript. Since all this happens behind the scenes and as a CoffeeScript developer if you do not know, how its working, it would be a pain later to expect anything else from this language.

Thats all to start with the new year. Happy new year.

Happy Coding.

31. December 2014

Cake and Coffee

While working with CoffeeScript, we generally tend to do the following,

  • write something in a .coffee file
  • compile that file with coffee compiler to .js file

Well, if we are a little smarter, then use the following command to compile all the .coffee files in a particular folder to respective .js file in another folder.

coffee --compile --output outputFolder/ inputFolder/

That may seem enough now, but actually there are a lot of things we could automate. The native way to do this in CoffeeScript is called Cake. We can look at it as a build system. Nice thing about this is, we can write every instruction in CoffeeScript itself.

  • Cake is an utility comes with CoffeeScript
  • its a task runner or build system
  • all the instructions are saved in a file with name Cakefile
  • Cakefile is the file name and there is no extension to it
  • Everything inside the Cakefile is a CoffeeScript expression

To run the Cake task runner, navigate to the folder, where our Cakefile is residing and fire up the command as below.

cake

That will display all the tasks available in that file to be run. To run a specific task, run

cake taskName

While defining tasks, we can make a relation between tasks. That means we can say taskOne is dependent on taskTwo. The basics of CoffeeScript project has a Cakefile for reference.

The direct link to the sample Cakefile is here. All the code are commented for easy reference.

Happy Coding.