Adaltas

A simple connect middleware to transpile CoffeeScript files

This new module called connect-coffee-script is a Connect middleware used to serve javascript files written in CoffeeScript. This middleware is to be used by connect or any Connect compatible framework such as Express and Zappa. For those not familiar with CoffeeScript, it is a transpiler which compile into Javascript.

But first, let’s explain how I came to publish such a module in july 2012. Until now, I was extremely happy with the compiler middleware present in Connect. However, with the latest release of Connect and Express, things have changed. I found myself having much of a hard time finding a middleware which will render my client CoffeeScript files.

All my google attempt lead to the connect-coffee middleware which itself is deprecated in favor of connect-assets. Looking at the readme, I must confess I was not a big fan on a first sight. I just couldn’t grasp the all concept. Moreover, I keep thinking that the default layout approach used by this module is just wrong for my projects. But more googling didn’t help. If connect-assets was the one, let’s be it. However, maybe I was not patient enough, I couldn’t find any clear example on how to use it. After a few attemps and after reading those two posts “NodeJS + CoffeeScript, render coffeescript compiled js on request” and “the connect middleware for coffeescript?” on StackOverflow, I came to the conclusion that probably there wasn’t any simple alternative and that I might not be the only one interested.

To my own surprise, this is how I came to write this module. Also, I found it quite funny to write it at the time of a public flame war on the Node.js mailinglist which started with a completly different topic.

The code is highly inspired by the Stylus middleware and it work the same. For any GET http request which extension is “.js”, it look for a file of the same name but with a “.coffee” extension on a local directory, the src option. It also look for a file of the same name and same extension on a second local directory, the dest option. If the “.js” file doesnt exist or if its last modification time is less the the “.coffee” file, it is generated from the “.coffee” file.

This approach is quite performant, easy to configure and transparent in usage. It also work pretty well with the native Connect cache and static middlewares.

You will find examples on the GitHub project readme as well as inside the “sample” folder.

Comments

Fork me on GitHub