darcsden :: dbp -> snap-coffee -> browse

CoffeeScripts in Snap (based on duairc's snap-less)

root

readme

snap-coffee: CoffeeScript for Snap

This library is derived from and almost identical to Shane O'Brien's library for LessCSS, except of course that this processes CoffeeScripts and serves up Javascript, and the other library processed LessCSS and served up CSS. The only difference besides all the name changes is that this library will pass in the case of a missing file, which is ideal for the javascript / coffeescript situation because both static javascript and coffeescript compiled to javascript can live at the same address, with the coffeescript preferred and the static being the fallback. (I've also added this feature to the LessCSS library forked at https://github.com/dbp/snap-less)

This library and Shane's original are a definite candidate to creating a more general "processed-and-cached" library, where you could specify what the command to process the files would be and it would handle all the rest. Perhaps once the new extension framework for Snap is created, this will be something to make. For now, keep it simple.

In your Main.hs, do this:

main :: IO ()
main = do
    ld <- newCoffeeDirectory&#39;     "scripts"
    td <- newTemplateDirectory&#39; "templates"   $ withSplices splices
    quickServer $ lessHandler     ld lessReloadHandler    $ \ls ->
                  templateHandler td defaultReloadHandler $ \ts ->
          ifTop     (render     ts "index")
      <|> dir "js" (renderCoffee ls)
      <|> fileServe "static"

Now any requests matching /js/foobar.js will get served the result of putting the file /scripts/foobar.coffee in your project directory through coffee. The output of coffee is cached, so it isn't run on every request. If you want to refresh the cached stylesheets, send a request to /admin/reload/coffee. If you want a different reload handler, pass your own one to coffeeHandler instead of coffeeReloadHandler (or simply (const pass) if you wish to have no reload handler).