如果您在 Windows 上进行开发,将 CoffeeScript 与 Django 结合使用的最佳方法是什么?
开始在 Django 中使用 Sass / Compass 再简单不过了< /a> 无论平台如何,都需要进行一些搜索才能找到在 Windows 开发环境中将 CoffeeScript 与 Django 结合使用的最佳方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
自从我发布了最初的答案(我将出于历史目的而保留该答案)以来,Windows 上的节点支持已经有了很大的改进,所以现在让它工作起来要容易得多。
下载并使用 Windows 安装程序安装 Node。您会自动将
node
和npm
命令添加到您的 Windows 路径(在 cmd.exe 中提供)。安装 CoffeeScript:
npm install -g Coffee-script
。然后只是为了测试,使用 cmd.exe...安装django-compressor:
pip install django-compressor
。添加到您的settings.py,以便 django-compressor 将预编译您的 CoffeeScript。
利润!现在,在 Django 模板中使用 *.coffee 文件或内联 CoffeeScript,并将其自动编译为 JavaScript 并与其他脚本合并到单个压缩文件中。
示例(取自django-compressor 文档):
原始答案(已过时):
目标是能够在 Django 模板内编写 CoffeeScript 并自动将其转换为 Javascript(以及 .coffee 文件)。 django-compressor 有一个预编译器可以在它最擅长的文件压缩之前执行此操作。
当然,问题是你想使用 Windows(你出了什么问题?),并且预编译器假设你有典型的 Linux 安装的 Node.js 和 Coffee-Script,能够从命令行调用“coffee”及其所有功能。标准选项。要获得与 Windows 相同的功能(无需借助 cygwin),您只需制作一个小 .bat 文件:
获取 node 的最新 Windows 二进制文件
将包含node.exe的路径添加到Windows系统环境变量中的PATH
选择以下之一:
鉴于 npm 不适用于 Windows,您可以使用 ryppi(一个最小的 Python 节点包管理器)来安装 Coffee-script 包。将 ryppi.py 放入 Python 脚本文件夹中。
只需从主站点下载coffee-script
添加路径\to\coffeescript\ bin(包含“cake”和“coffee”)到 Windows 系统环境变量中的 PATH
制作批处理文件,以便您可以从命令行使用“咖啡”(功劳)通过在上面的 path\to\coffeescript\bin 文件夹中创建一个 Coffee.bat 文件,以此作为其内容:
<前><代码>@pushd 。
@cd /d %~dp0
@节点咖啡%*
@popd
如果没有这个,你必须执行“node \path\to\bin\coffee”,而不仅仅是“coffee”。
尝试重新打开 cmd.exe 并输入...
现在您正在节点上使用真正的咖啡脚本程序。
设置 django-compressor 预编译器以使用 Coffee.bat:
我把它放在我的 local_settings.py 文件中。只需像往常一样在 Linux 生产服务器或开发盒使用的设置文件中保留 .bat 即可。 Windows 不喜欢没有 .bat。
利润!
现在,您可以在 Django 模板中使用内联 CoffeeScript,并将其自动编译为 javascript,并与所有其他脚本合并到一个压缩的 .js 文件中。我会将使用 django-compressor 的详细信息保留在它的文档中。
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
andnpm
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...Install django-compressor:
pip install django-compressor
.Add to your settings.py so django-compressor will precompile your CoffeeScript.
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):
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.
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:
Without this you have to do 'node \path\to\bin\coffee' instead of just 'coffee'.
Try reopening cmd.exe and type...
Now you're using the real coffee-script program on node.
Setup the django-compressor precompiler to use coffee.bat:
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.
您可以使用其中一个 CoffeeScript 编译器。
其中一些支持文件系统监视,例如官方节点包。因此,您可以启动控制台并执行操作
,src 中的所有 CoffeeScript 文件在更改时都会自动重新编译。你不需要与 django 进行任何特殊的集成,尽管它可能会很好。
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
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.
Django Pipeline (Django >= 1.5) 支持 CoffeeScript 编译,以及其他负载(例如 LESS 、SASS、JS/CSS 缩小等)。确保您安装了 CoffeeScript,然后
pip install django-pipeline
,将“pipeline”添加到您的 INSTALLED_APPS 中,然后创建以下配置条目:然后您可以根据链接的文档设置要编译的文件 -基本上只是源文件、目标文件和名称。您可以在模板中按此名称引用压缩文件,如下所示:
Django Pipeline (Django >= 1.5) supports CoffeeScript compilation, as well as loads of other stuff (e.g. LESS, SASS, JS/CSS minification, etc). Make sure you have CoffeeScript installed, then
pip install django-pipeline
, add 'pipeline' to your INSTALLED_APPS and then create the following config entry:Then you can set up files to compile as per the linked docs - basically just source file(s), destination file and a name. You can refer to the compressed files by this name in templates likes this:
这对我来说看起来很有希望: http://pypi.python.org/pypi/django-coffeescript/
This looks promising to me: http://pypi.python.org/pypi/django-coffeescript/
我发现通过压缩器编译增加的延迟太多了。所以我改为在客户端编译,并签入 js 文件。如果您在运行 runserver 命令时开始观看文件,那么即时且非常方便:
https://gist.github。 com/EmilStenstrom/4761479
I find the delay that compiling via compressor adds to be too much. So I compile on the client side instead, and check in the js files. Instant, and very convenient if you start watching files when the runserver command is run:
https://gist.github.com/EmilStenstrom/4761479