I've found CoffeeScript to be a great help when writing JavaScript. Since most of my future posts on Ext JS will probably be in CoffeeScript, I thought I'd explain the basics of how I use it.

The easiest way to start is to download and install Node.js. Then, install CoffeeScript by opening a command line and running:

npm install -g coffee-script
		

That's all there is to the installation. To use CoffeeScript, you can perform a manual build at the command line to generate the equivalent JavaScript. I won't even bother showing this, because that sort of manual process is definitely something I don't want to do. Happily, CoffeeScript includes a simple build system using a file called Cakefile. Here is a starter version of this file:

fs = require 'fs'

{print} = require 'util'
{spawn} = require 'child_process'

build = (callback) ->
  coffee = spawn 'coffee.cmd', ['-c', '-o', 'app', 'coffee']
  coffee.stderr.on 'data', (data) ->
    process.stderr.write data.toString()
  coffee.stdout.on 'data', (data) ->
    print data.toString()
  coffee.on 'exit', (code) ->
    callback?() if code is 0

task 'build', 'Build /app from /coffee', ->
  build()
		

Running cake build at the command line will recursively find any .coffee files in the folder /coffee and generate JavaScript in the folder /app. Obviously, you're free to change the folder names and paths as you need to.

So that's better, but still not ideal. I don't want to have to manually run cake build over and over. We can take things further by adding a watch command to the Cakefile:

fs = require 'fs'

{print} = require 'util'
{spawn} = require 'child_process'

build = (callback) ->
  coffee = spawn 'coffee.cmd', ['-c', '-o', 'app', 'coffee']
  coffee.stderr.on 'data', (data) ->
    process.stderr.write data.toString()
  coffee.stdout.on 'data', (data) ->
    print data.toString()
  coffee.on 'exit', (code) ->
    callback?() if code is 0

task 'build', 'Build /app from /coffee', ->
  build()
  
task 'watch', 'Watch /coffee for changes', ->
  console.log "Starting CoffeeScript compiler (watching for changes in /coffee folder)..."
  coffee = spawn 'coffee.cmd', ['-w', '-c', '-o', 'app', 'coffee']
  coffee.stderr.on 'data', (data) ->
    process.stderr.write data.toString()
  coffee.stdout.on 'data', (data) ->
    print data.toString()
		

Now, if we run cake watch, a directory watcher is created which will compile to JavaScript any time one of the .coffee files is changed! So you can just run this command, then mostly forget about it and get on with your work.

CoffeeScript is a very simple, straightforward language. I was off and running with it in less than a day. The CoffeeScript site has good documentation, and you can find additional info in the free online version of the Little Book on CoffeeScript.

Comments Comments (2) | del.ico.us del.icio.us | Digg It! Digg It! | Linking Blogs Linking Blogs | 3574 Views

Related Blog Entries

Comments

  • # Posted By ash | 5/29/12 11:48 PM

    Have you researched the negatives to using coffeescript?

  • # Posted By Brian Kotek | 5/30/12 12:51 AM

    Haven't found any negatives to be honest. It's pretty much just pure win. ;-)