1

Is setting a bunch of watchers to compile to js still the best approach in development or is there something more elegant?

I'm looking for techniques or packages that handle CoffeeScript in development and make it really nice instead of just watching and compiling to another js folder. Anything out there?

Thanks for any ideas!

fancy
  • 48,619
  • 62
  • 153
  • 231
  • [Here](http://stackoverflow.com/a/4683431/838871)'s an answer that briefly explains how to use CS without compiling it manually to JS. Hope it helps. – Ivan Zarea Jan 25 '12 at 06:40
  • Oh awesome.... +1 Ivan, thanks this is a good start! Does this work for client side js being servered too? – fancy Jan 25 '12 at 06:42

1 Answers1

2

node.js server code in CoffeeScript

So for writing your node.js server code, you don't need to do anything special. Just start your app with coffee server.coffee instead of node server.js and coffeescript will transpile your code to javascript on the fly without ever needing to write .js files to disk.

Browser code in CoffeeScript (Basic)

For taking .coffee files on disk and serving the transpiled .js files to the browser, you can use the coffee-script node module to do the transpiling and serve the output. Coding it manually is just a few lines, but those few lines already exist as connect compatible middleware. Use the connect-coffee-script module. There are basic examples at that link, but it boils down to app.use(connectCoffeeScript({src: "#{__dirname}/public"})) or some variation thereof. This is compatible with express version 3.x. When a request for a .js URL comes in, the middleware will locate the corresponding .coffee file and transpile it from src to dest if needed. You should have the connect static middleware configured to serve files from your dest directory further down your middleware chain and it will be the connect static middleware that actually servers the .js file to the browser.

Full-on Asset Management

For a more advanced solution including dependency management, cache busting, concatenation, minifaction, etc, etc inspired by the Ruby on Rails asset pipeline, you can use connect-assets. It's a more complex solution, but the asset management problem in general is complex and this will fully solve many of the tricky problems for you. This will handle CoffeeScript for JS, Stylus for CSS as well as other transpilers and preprocessors.

Community
  • 1
  • 1
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • awesome, so `enable: ["coffeescript"]` does the same as `coffee app.coffee` but for client-side stuff? – fancy Jan 25 '12 at 07:33
  • express.compiler() no longer exists but you can use https://github.com/TrevorBurnham/connect-assets to do automatic coffeescript compilation now – Adam Soltys Nov 07 '12 at 21:02