.NET CoffeeScript Handler

Posted by Liam McLennan on Geeks with Blogs See other posts from Geeks with Blogs or by Liam McLennan
Published on Mon, 14 Jun 2010 09:43:04 GMT Indexed on 2010/06/14 10:53 UTC
Read the original article Hit count: 454

Filed under:

coffee_poster After more time than I care to admit I have finally released a rudimentary Http Handler for serving compiled CoffeeScript from Asp.Net applications.

It was a long and painful road but I am glad to finally have a usable strategy for client-side scripting in CoffeeScript.

Why CoffeeScript?

As Douglas Crockford discussed in detail, Javascript is a mixture of good and bad features.

The genius of CoffeeScript is to treat javascript in the browser as a virtual machine.

By compiling to javascript CoffeeScript gets a clean slate to re-implement syntax, taking the best of javascript and ruby and combining them into a beautiful scripting language. The only limitation is that CoffeeScript cannot do anything that javascript cannot do.

Here is an example from the CoffeeScript website. First, the coffeescript syntax:

reverse: (string) ->
  string.split('').reverse().join ''

alert reverse '.eeffoC yrT'

and the javascript that it compiles to:

var reverse;
reverse = function(string) {
  return string.split('').reverse().join('');
};
alert(reverse('.eeffoC yrT'));

Areas For Improvement ;)

The current implementation is deeply flawed, however, at this point I’m just glad it works. When the server receives a request for a coffeescript file the following things happen:

  1. The CoffeeScriptHandler is invoked
  2. If the script has previously been compiled then the compiled version is returned.
  3. Else it writes a script file containing the CoffeeScript compiler and the requested coffee script
  4. The process shells out to CScript.exe to to execute the script.
  5. The resulting javascript is sent back to the browser.

This outlandish process is necessary because I could not find a way to directly execute the coffeescript compiler from .NET. If anyone can help out with that I would appreciate it.

© Geeks with Blogs or respective owner