如果您在 Windows 上进行开发,那么在 Django 中使用 CoffeeScript 的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6990721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:45:03  来源:igfitidea点击:

What's the best way to use CoffeeScript with Django if you're developing on Windows?

windowsdjangocoffeescript

提问by Andrew Swihart

While starting to use Sass / Compass with Django couldn't be much easierregardless 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.

虽然开始在 Django 中使用 Sass / Compass并不容易,但无论平台如何,都需要进行一些搜索才能找到在 Windows 开发盒上使用 CoffeeScript 和 Django 的最佳方式。

回答by Andrew Swihart

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 上的节点支持有了很大的改进,所以现在让它工作起来要容易得多。

  1. Downloadand install Node using the Windows installer. You get nodeand npmcommands 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 %}
    
  1. 使用 Windows 安装程序下载并安装 Node。你得到nodenpm自动添加到您的Windows PATH命令(输入cmd.exe可用)。

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

    coffee --version
    CoffeeScript version 1.4.0 #sweet!
    
  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 文档):

    {% 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-compressorhas a precompiler that does this, prior to the file compression it's known best for.

目标是能够在 Django 模板中直接编写 CoffeeScript,并让它自动转换为 Javascript(以及 .coffee 文件)。django-compressor有一个预编译器可以做到这一点,在它最出名的文件压缩之前。

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(您有什么问题?),并且预编译器假定您有一个典型的 node.js 和咖啡脚本的 Linux 安装,能够从命令行调用“咖啡”及其所有标准选项。要获得相同的 Windows 功能(不求助于 cygwin),您只需要制作一个小 .bat 文件:

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

  1. 获取node的最新 Windows 二进制文件

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

  3. 选择其中之一:

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

      cd /d C:\Users\<USERNAME>\  #'node_modules' folder can live here or wherever
      ryppi.py install coffee-script
      
    2. 只需从主站点下载咖啡脚本

  4. 将 path\to\coffeescript\bin(包含 'cake' 和 'coffee')添加到 Windows 系统环境变量中的 PATH

  5. 制作一个批处理文件,以便您可以通过在上面的 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 version 1.1.2  #sweet!
    

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

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

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

    我把它放在我的 local_settings.py 文件中。只需像往常一样在 Linux 生产服务器或开发箱使用的设置文件中保留 .bat 即可。没有 .bat,Windows 会不高兴。

  8. 利润!

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

回答by liammclennan

You can use one of these CoffeeScript compilers.

您可以使用这些 CoffeeScript 编译器之一

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.

并且src中的所有coffeescript文件在更改时都会自动重新编译。您不需要与 django 进行任何特殊集成,尽管它可能很好。

回答by Rob Grant

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:

Django Pipeline(Django >= 1.5) 支持 CoffeeScript 编译,以及大量其他东西(例如 LESS、SASS、JS/CSS 缩小等)。确保您安装了 CoffeeScript,然后pip install django-pipeline将“管道”添加到您的 INSTALLED_APPS,然后创建以下配置条目:

PIPELINE_COMPILERS = (
  'pipeline.compilers.coffee.CoffeeScriptCompiler',
)

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:

然后,您可以根据链接的文档设置要编译的文件 - 基本上只是源文件、目标文件和名称。您可以在模板中通过此名称引用压缩文件,如下所示:

{% compressed_js 'my_compressed_js' %}

回答by aeikenberry

This looks promising to me: http://pypi.python.org/pypi/django-coffeescript/

这对我来说看起来很有希望:http: //pypi.python.org/pypi/django-coffeescript/

回答by Emil Stenstr?m

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:

我发现通过压缩器编译的延迟太多了。所以我改为在客户端编译,并检查 js 文件。即时,如果您在 runserver 命令运行时开始查看文件,则非常方便:

https://gist.github.com/EmilStenstrom/4761479

https://gist.github.com/EmilStenstrom/4761479