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

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

问题描述

在开始使用Sass / Compass和Django时,不太容易无论平台如何,它已经采取了一些搜索找到在Windows开发盒上使用CoffeeScript和Django的最佳方法。

解决方案

在Windows上的节点支持已经大大提高了,因为我张贴了我的原始答案(我将离开为历史的目的),所以现在更容易得到这个工作。


  1. 下载并使用Windows安装程序安装Node。您可以自动添加到Windows PATH中的节点 npm 命令(在cmd.exe中可用)。


  2. 安装CoffeeScript: npm install -g coffee-script 。然后只是测试,使用cmd.exe ...

      coffee --version 
    CoffeeScript版本1.4.0#甜!


  3. 安装django-compressor: pip install django-compressor


  4. 添加到您的settings.py,因此django-compressor将预编译您的CoffeeScript。

      COMPRESS_PRECOMPILERS =(
    ('text / coffeescript','coffee --compile --stdio'),


  5. 利润!现在在Django模板中使用* .coffee文件或内联CoffeeScript,并将其自动编译为javascript并与其他脚本合并为一个压缩文件。



    示例 django-compressor docs ):

      {%load compress%} 

    {%compress js%}
    < script type =text / coffeescriptcharset =utf-8 src =/ static / js / awesome.coffee/>
    < script type =text / coffeescriptcharset =utf-8>
    #函数:
    square =(x) - > x * x
    {%endcompress%}


原始答案(过时)



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



当然,问题是你想使用Windows(你有什么问题吗?),预编译器假定你有一个典型的Linux安装node.js和coffee-脚本,能够从命令行调用咖啡及其所有标准选项。要获得相同的功能Windows(不使用cygwin),你只需要一个小的.bat文件:


  1. Grab 节点

  2. 的最新Windows二进制文件
  3. 在Windows系统环境变量中将包含node.exe的路径添加到PATH

    / li>
  4. 选择以下选项之一:


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

        cd / d C:\Users\< USERNAME> \#'node_modules'文件夹可以住在这里
      ryppi.py安装coffee-script


    2. 只需下载


    3. 添加路径\to\coffeescript\

    4. 在Windows系统环境变量


    5. 创建批处理文件中将bin(包含'cake'和'coffee'可以使用命令行中的咖啡(此信息)通过在上面的path\to\coffeescript\bin文件夹中创建一个coffee.bat文件,并将其作为其内容:

        @pushd。 
      @cd / d%〜dp0
      @node coffee%*
      @popd

      没有这个,你必须做'node \path\to\bin\coffee'而不只是'coffee'。


    6. 尝试重新打开cmd.exe并输入...

        coffee --version 
      CoffeeScript版本1.1.2 #sweet !

      现在你正在节点上使用真正的咖啡脚本程序。

      >
    7. 设置django-compressor预编译器以使用coffee.bat:

        COMPRESS_PRECOMPILERS = 
      ('text / coffeescript','coffee.bat --compile --stdio'),

      我把它放在我的local_settings.py文件中。只需在Linux生产服务器或开发框所使用的设置文件中像往常一样离开.bat。 Windows没有.bat就不开心。


    8. 利润!



      现在,您可以在Django模板中使用内联CoffeeScript,并将其自动编译为javascript,并与所有其他脚本合并为一个压缩的.js文件。我将使用django-compressor的详细信息保留为文档



    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.

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

    2. Install CoffeeScript: npm install -g coffee-script. Then just to test, using cmd.exe...

      coffee --version
      CoffeeScript version 1.4.0 #sweet!
      

    3. Install django-compressor: pip install django-compressor.

    4. Add to your settings.py so django-compressor will precompile your CoffeeScript.

      COMPRESS_PRECOMPILERS = (
          ('text/coffeescript', 'coffee --compile --stdio'),
      )
      

    5. 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:

    1. Grab the latest Windows binary of node

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

    3. Pick one of:

      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
        

      2. Just download coffee-script from the main site

    4. Add the path\to\coffeescript\bin (containing 'cake' and 'coffee') to your PATH in Windows system environment variables

    5. 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'.

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

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

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

    这篇关于如果您在Windows上开发,使用CoffeeScript与Django的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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