如果您在 Windows 上开发,将 CoffeeScript 与 Django 一起使用的最佳方式是什么? [英] What's the best way to use CoffeeScript with Django if you're developing on Windows?

查看:9
本文介绍了如果您在 Windows 上开发,将 CoffeeScript 与 Django 一起使用的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然开始在 Django 中使用 Sass/Compass 不能更容易无论平台如何,都需要进行一些搜索才能找到在 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.

推荐答案

自从我发布了我的原始答案(出于历史目的我将保留它)以来,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.

  1. 下载并使用 Windows 安装程序安装 Node.您会自动将 nodenpm 命令添加到您的 Windows PATH(在 cmd.exe 中可用).

  1. Download and install Node using the Windows installer. You get node and npm 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-compressor:pip install django-compressor.

    添加到您的 settings.py 以便 django-compressor 预编译您的 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 文档):p>

    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 %}
    

  • 原始答案(已过时):

    目标是能够直接在 Django 模板中编写 CoffeeScript 并使其自动转换为 Javascript(以及 .coffee 文件).django-compressor 有一个执行此操作的预编译器,在它最擅长的文件压缩之前.

    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 和咖啡脚本,能够从命令行调用咖啡"及其所有标准选项.要获得与 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:

    1. 获取 node

    将包含node.exe的路径添加到Windows系统环境变量的PATH中

    Add the path containing node.exe to PATH in Windows system environment variables

    选择以下之一:

    1. 鉴于 npm 不适用于 Windows,您可以使用 ryppi,一个最小的 Python 节点包管理器来安装 coffee-script 包.将 ryppi.py 放在 Python 脚本文件夹中.

    1. 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
    

  • 只需从主站点下载 coffee-script

  • 将 path ocoffeescriptin(包含cake"和coffee")添加到 Windows 系统环境变量中的 PATH

  • Add the path ocoffeescriptin (containing 'cake' and 'coffee') to your PATH in Windows system environment variables

    制作一个批处理文件,以便您可以从命令行使用咖啡"(credit for this) 通过在上面的 path ocoffeescriptin 文件夹中创建一个 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 ocoffeescriptin folder above, with this as its contents:

    @pushd .
    @cd /d %~dp0
    @node coffee %*
    @popd
    

    如果没有这个,你必须做 'node path oincoffee' 而不仅仅是 'coffee'.

    Without this you have to do 'node path oincoffee' 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-compressor 预编译器以使用 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-compressor 的详细信息留给它的文档.

    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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆