Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript there?
<script type="text/coffeescript"> square = (x) -> x * x list = [1, 2, 3, 4, 5] squares = (square num for num in list) </script>
The CoffeeScript compiler is written in JavaScript, so can I send it to the client to compile/run this code in the client's browser?
Bottom Line. One crucial difference between the two languages is that TypeScript is the superset of JavaScript while CoffeeScript is a language which is an enhanced version of JavaScript. Not just these two languages but there are other languages such as Dart, Kotlin, etc. which can be compiled into JavaScript.
If you are looking to implement coffee script in html, take a look at this. You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file.
Jeremy already has this one, but let me add some important details and caveats:
coffee-script.js
is a big file; so unless you're actually letting your users run their own CoffeeScript, you really shouldn't use it in production.squares
wouldn't be visible outside of the script. Instead, you'd want to change it to window.squares = ...
.coffee-script.js
doesn't read your <script type="text/coffeescript>
tags until after the document is ready, by which time your JavaScripts have already run.XMLHTTPRequest
, which means that they must be hosted on the same domain as your site. (Certain browsers—Chrome, at least—also have a problem with doing XMLHTTPRequest
s on file://
paths.)So, you might want to look at some alternatives for serving CoffeeScript as compiled JavaScript instead. If you're developing for a Ruby or Python server, there are plugins available. I've tried to list them all at http://github.com/jashkenas/coffee-script/wiki/Web-framework-plugins.
If you're developing a site without a backend, a tool I highly recommend looking at is Middleman, which lets you work with CoffeeScript (as well as Haml and Sass, if you want) during development, then compile and minify it for production deployment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With