dvcolgan dvcolgan - 1 year ago 57
HTML Question

Is it possible to compile Coffeescript code in script tags in html files?

Possible Duplicate:

Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript there?

Is there a simple way to compile Coffeescript that is inside
tags inside html, or do you usually have all Coffeescript in separate files?

Answer Source

Responding to dvcolgan's clarifying comment:

So, you want to have an HTML file on the server with inline CoffeeScript that gets served as HTML with inline JavaScript. The CoffeeScript compiler doesn't support this directly, but you could write a Node script to do it fairly easily, using the coffee-script library and jsdom to do the HTML parsing.

Exactly how you want to implement this would depend on the web framework you're using. You probably don't want the CoffeeScript compiler to run on every request (it's pretty fast, but it's still going to reduce the number of requests/second your server can handle); instead, you'd want to compile your HTML once and then serve the compiled version from a cache. Again, I don't know of any existing tools that do this, but it shouldn't be too hard to write your own.