While starting to use Sass / Compass with Django couldn't be much easier regardless of platform, it has taken a bit of searching around to find the best way to use CoffeeScript with Django on a Windows development box.
Node support on Windows has greatly improved since I posted my original answer (which I will leave for historical purposes), so now it's much easier to get this working.
Download and install Node using the Windows installer. You get node
and npm
commands added to your Windows PATH automatically (available in cmd.exe).
Install CoffeeScript: npm install -g coffee-script
. Then just to test, using cmd.exe...
coffee --version
CoffeeScript version 1.4.0 #sweet!
Install django-compressor: pip install django-compressor
.
Add to your settings.py so django-compressor will precompile your CoffeeScript.
COMPRESS_PRECOMPILERS = (
('text/coffeescript', 'coffee --compile --stdio'),
)
Profit! Now use *.coffee files or inline CoffeeScript in Django templates and have it automatically compiled to javascript and combined with your other scripts into a single compressed file.
Example (taken from django-compressor docs):
{% load compress %}
{% compress js %}
<script type="text/coffeescript" charset="utf-8" src="/static/js/awesome.coffee" />
<script type="text/coffeescript" charset="utf-8">
# Functions:
square = (x) -> x * x
</script>
{% endcompress %}
Original answer (obsolete):
The goal is to be able to write CoffeeScript right inside Django templates and have it get automatically converted to Javascript (along with .coffee files). django-compressor has a precompiler that does this, prior to the file compression it's known best for.
Of course the issue is you want to use Windows (what's wrong with you?), and the precompiler assumes you have a typical Linux installation of node.js and coffee-script, able to invoke 'coffee' from the command line with all its standard options. To get the same functionality Windows (without resorting to cygwin), you just have to make a little .bat file:
Grab the latest Windows binary of node
Add the path containing node.exe to PATH in Windows system environment variables
Pick one of:
Given that npm is not available for Windows, you can use ryppi, a minimal Python node package manager, to install the coffee-script package. Put ryppi.py in your Python scripts folder.
cd /d C:\Users\<USERNAME>\ #'node_modules' folder can live here or wherever
ryppi.py install coffee-script
Just download coffee-script from the main site
Add the path\to\coffeescript\bin (containing 'cake' and 'coffee') to your PATH in Windows system environment variables
Make a batch file so you can use 'coffee' from the command line (credit for this) by creating a coffee.bat file in path\to\coffeescript\bin folder above, with this as its contents:
@pushd .
@cd /d %~dp0
@node coffee %*
@popd
Without this you have to do 'node \path\to\bin\coffee' instead of just 'coffee'.
Try reopening cmd.exe and type...
coffee --version
CoffeeScript version 1.1.2 #sweet!
Now you're using the real coffee-script program on node.
Setup the django-compressor precompiler to use coffee.bat:
COMPRESS_PRECOMPILERS = (
('text/coffeescript', 'coffee.bat --compile --stdio'),
)
I put that in my local_settings.py file. Just leave off the .bat as usual in the settings file used by your Linux production server or development box. Windows wasn't happy without the .bat.
Profit!
Now you can use inline CoffeeScript in your Django templates, and have it automatically compiled to javascript and combined with all your other scripts into a single compressed .js file. I'll leave details of using django-compressor to it's documentation.
You can use one of these CoffeeScript compilers.
Some of them support file system watching, like the official node package. So you can start a console and do
coffee -c src/ -o /bin --watch
and all the coffeescript files in src will be automatically recompiled when they change. You don't need any special integration with django, although it might be nice.
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