Ever wanted to write code in CoffeeScript? It surely is great but you wonder why is this not automated or smaller. Actually there are two options:
First option is very easy but may vary on performance. Second is better in performance bad worse in usage. I'll teach you how to automate that process so it will be both fast on page load and easy too.
1st option - include CoffeeScript
This is the way:
<script type="text/coffeescript" src="js/general.coffeescript"></script>
Simple. CoffeScript is loaded first and then automatically compiled by script.
Minuses are that compiler have to be downloaded (about 200KB of minimzed code!) and scrip have to be compiled after the JS compiler is downloaded.
Let's forget about this.
There are probably other code editors as well which can do same thing but I'll present it in Sublime Text 2. I'm assuming you're using Windows OS, otherwise you're probably not interested in those instructions.
There are few steps to install it. The hard moment is when plugin doesn't work (and that's why you can read it) which about I'd like to share.
Better CoffeeScript plugin installation:
- Run Sublime Text 2
- Ctrl+Shift+P and then type "Install Package", hit Enter and wait until similiar window appears.
- Type "Better CoffeeScript" and hit Enter. The plugin will be installed
Now we need to install CoffeeScript compiler itself:
- Download node.js Windows Installer (in my case x64) and install it wherever you want (yes, other path than the default one works fine).
- Open Windows command line (hotkey Win+R and then type cmd).
- In command line type: npm install -g coffee-script
- Setup environment variable PATH so it will point at %APPDATA%/npm and C:/Program Files/nodejs (or wherever you have installed it)
OK, now theoretically the plugin should work (after this operation and Sublime Text 2 is restarted) BUT there's a problem of course. Try to create file with extension coffee, e.g. general.coffee and save it. Sublime Text 2 says then:
'coffe' is not recognized as an internal or external command
It sucks by no seing a path to coffee.cmd script located in %APPDATA%/npm. My workaround is following:
- Using some file explorer go to folder: %APPDATA%/Sublime Text 2/Packages/Better CoffeeScript
- Modify CoffeeScript.py file:
source_file = args[-1]
cmd = settings.get('binDir', '%APPDATA%/npm') + '/' + cmd
Wanted to make your files even smaller? Use YUI Compressor! There's also a plugin for it: http://leon.radley.se/2012/02/st2-yui-compressor/
Works great. Have fun.
Some time has passed - Sublime Text 3
If you have Sublime Text 3, then probably you don't have Package Control (and you won't see "Install Package" after CTRL+SHIFT+P).
Also current version of Better CoffeeScript seems to work on Windows but doesn't compile file on save. To change this copy configuration from Preferences->Package Settings->Better CoffeeScript->Settings-Default to Settings-User and set compileOnSave to true