如果您正在Windows上开发,使用CoffeeScript与Django最好的方式是什么? [英] What's the best way to use CoffeeScript with Django if you're developing on Windows?
问题描述
开始使用Sass / Compass与Django 不能更容易,无论平台如何,它已经进行了一些搜索,以找到在Windows开发框上使用具有Django的CoffeeScript的最佳方式。
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.
推荐答案
自从我发布了我的原始答案(我将出于历史目的)之后,Windows上的节点支持已经大大提高,所以现在更容易得到这个工作。
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.
-
下载并使用Windows安装Node安装程序。您将自动添加到您的Windows PATH(cmd.exe)中的
节点
和npm
命令。
Download and install Node using the Windows installer. You get
node
andnpm
commands added to your Windows PATH automatically (available in cmd.exe).
安装CoffeeScript: npm install -g coffee-script
。然后只是测试,使用cmd.exe ...
Install CoffeeScript: npm install -g coffee-script
. Then just to test, using cmd.exe...
coffee --version
CoffeeScript version 1.4.0 #sweet!
安装django-compression: pip install django-compression
。
Install django-compressor: pip install django-compressor
.
添加到您的settings.py,以便django-compression将预编译您的CoffeeScript。
Add to your settings.py so django-compressor will precompile your CoffeeScript.
COMPRESS_PRECOMPILERS = (
('text/coffeescript', 'coffee --compile --stdio'),
)
利润!现在在Django模板中使用* .coffee文件或内联CoffeeScript,并将其自动编译为javascript并将其他脚本与其他脚本组合成一个压缩文件。
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.
示例(取自 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 %}
原始答案(已过时):
目标是能够在Django模板中编写CoffeeScript,并将其自动转换为Javascript(以及.coffee文件)。 django-compression 有一个预编译器,在文件压缩之前,这是最好的。
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.
当然,您想要使用Windows(您怎么了?),而且预编译器假定您有典型的Linux安装node.js和coffee-脚本,能够使用其所有标准选项从命令行调用咖啡。要获得相同的功能Windows(不使用cygwin),您只需要制作一个.bat文件:
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:
-
抓取节点的最新Windows二进制
在Windows系统环境变量中将包含node.exe的路径添加到PATH
Add the path containing node.exe to PATH in Windows system environment variables
选择以下之一:
-
鉴于npm不适用于Windows ,您可以使用最小的Python节点包管理器 ryppi 来安装咖啡脚本包。将ryppi.py放在Python脚本文件夹中。
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
只需下载脚本
添加路径\to\coffeescript\ bin(包含蛋糕和咖啡)到Windows系统环境变量中的PATH
Add the path\to\coffeescript\bin (containing 'cake' and 'coffee') to your PATH in Windows system environment variables
创建批处理文件可以使用命令行中的咖啡(信用为此)通过在上面的path\to-\coffeescript\bin文件夹中创建一个coffee.bat文件,其内容如下:
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
没有这个,你必须做'node \path\to\bin\coffee'而不是'咖啡'。
Without this you have to do 'node \path\to\bin\coffee' instead of just 'coffee'.
尝试重新打开cmd.exe并键入...
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.
将django-compress预编译器设置为使用coffee.bat:
Setup the django-compressor precompiler to use coffee.bat:
COMPRESS_PRECOMPILERS = (
('text/coffeescript', 'coffee.bat --compile --stdio'),
)
我把它放在我的local_settings.py文件中。在Linux生产服务器或开发框使用的设置文件中,像往常一样放开.bat。如果没有.bat,Windows不开心。
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.
利润!
现在,您可以在Django模板中使用内联CoffeeScript,并将其自动编译为javascript,并将其与所有其他脚本组合到单个压缩的.js文件中。我将在文档中留下使用django-compression的详细信息。
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.
这篇关于如果您正在Windows上开发,使用CoffeeScript与Django最好的方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!