Merge Coffeescript Into Your Node Project?
Solution 1:
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.
Post a Comment for "Merge Coffeescript Into Your Node Project?"