javascript 在保存时编译 CoffeeScript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6286366/
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-10-25 20:09:51  来源:igfitidea点击:

Compile CoffeeScript on Save?

javascriptnode.jstextmatecoffeescript

提问by fancy

Is there anyway to easily compile CoffeeScript on save? I'm using TextMate or Sublime Text 2.

有没有办法在保存时轻松编译 CoffeeScript?我正在使用 TextMate 或 Sublime Text 2。

回答by Elf Sternberg

Coffeescript has a 'watch' feature. You could set up, as a semi-permanent process:

Coffeescript 具有“监视”功能。您可以设置,作为一个半永久性的过程:

coffee –wc -o media/js/ src/coffee/*.coffee

And for every file with an extension ".coffee", the corresponding compiled ".js" file will be put into the target directory every time you save.

并且对于每个扩展名为“.coffee”的文件,每次保存时都会将相应的编译后的“.js”文件放入目标目录。

That said, I use a makefile and a fabfile, because my HTML is HAML, my CSS is LessCSS, and my development directory is not my test path, so I needed something smart enough to "build and deploy."

也就是说,我使用了一个 makefile 和一个 fabfile,因为我的 HTML 是 HAML,我的 CSS 是 LessCSS,我的开发目录不是我的测试路径,所以我需要一些足够聪明的东西来“构建和部署”。

If your machine supports inotify, you could use inotifywait to watch your entire work path, and call Make as needed. But at that point, you're into hard-core geekery.

如果您的机器支持inotify,您可以使用inotifywait 来查看您的整个工作路径,并根据需要调用Make。但在这一点上,你进入了铁杆极客。

回答by Jed Schmidt

You can also accomplish this without the command line:

您也可以在没有命令行的情况下完成此操作:

  1. Add a build process to Sublime Text.
  2. Make sure that Save All on Buildis selected in the Toolsmenu.
  3. Use ?B instead of ?S when saving.
  1. 将构建过程添加到 Sublime Text
  2. 确保Save All on BuildTools菜单中选中。
  3. 保存时使用 ?B 而不是 ?S 。

So instead of compiling on save, you're saving on compile.

因此,不是在保存时编译,而是在编译时保存。

回答by matanster

The most straightforward solution with Sublime, is to install the Sublime package called Better Coffeescript (preferences --> package control --> install package...), and then make sure that its configuration includes "compileOnSave": true(preferences --> package settings --> Better Coffeescript...). Then restart Sublime.

Sublime 最直接的解决方案是安装名为 Better Coffeescript 的 Sublime 包(首选项 --> 包控制 --> 安装包...),然后确保其配置包括"compileOnSave": true(首选项 --> 包设置 -- > 更好的 Coffeescript...)。然后重启 Sublime。

For Sublime, anything else is not enough or too much extra components. Just came here after upgrading to Sublime 3, and it works like charm for Sublime 3 (as it did for Sublime 2, I just forgot about it at first).

对于 Sublime 来说,其他任何东西都是不够的,或者有太多额外的组件。升级到 Sublime 3 后才来到这里,它对 Sublime 3 来说就像魅力一样(就像对 Sublime 2 一样,我刚开始忘记了它)。

回答by gsklee

Well coffee --watchhas 2 major flaws:

Wellcoffee --watch有2个主要缺陷:

  • New files created after command has been issued aren't being watched
  • Requires manual initiation so there can be a chance you forget to do it, which doesn't sound more brilliant than forget to compile before you git commitit
  • 命令发出后创建的新文件不被监视
  • 需要手动启动这样可以有一个机会,你忘记这样做,这不健全的更加辉煌的比之前忘记你编译git commit

The solution I came up with is a rather simple Bash script that takes coffee --watcha few steps further which will allow your working directory tree to be watched ever since system login, and automatically get compiled into JavaScript on each file save/change or new file creation:

我想出的解决方案是一个相当简单的 Bash 脚本,它需要coffee --watch进一步执行几个步骤,这将允许自系统登录以来一直观察您的工作目录树,并在每个文件保存/更改或新文件创建时自动编译为 JavaScript:

http://blog.gantrithor.com/post/11609373640/carefree-coffeescript-auto-compiler

http://blog.gantrithor.com/post/11609373640/carefree-coffeescript-auto-compiler

There may be more elegant way to do this, but this implementation works great =)

可能有更优雅的方法来做到这一点,但这个实现效果很好 =)

回答by vaughan

gem install stasis
stasis -d

The best solution I have found compared with all other static compilation tools like StaticMatic, Middleman, etc.

与所有其他静态编译工具(如 StaticMatic、Middleman 等)相比,我发现的最佳解决方案。

Very flexible and configurable and does not rely on any preset folder structure. Just add controller.rband write some Ruby. Lots of helpers for doing clever things before/after compilation.

非常灵活和可配置,不依赖于任何预设的文件夹结构。只需添加controller.rb并编写一些 Ruby。很多帮助在编译之前/之后做聪明的事情。

https://github.com/winton/stasis

https://github.com/winton/stasis

回答by balupton

If you also want bundling, buildr offers watching and bundling too: https://github.com/balupton/buildr.npm

如果您还想要捆绑,buildr 也提供观看和捆绑:https: //github.com/balupton/buildr.npm

The (Java|Coffee)Script and (CSS|Less) (Builder|Bundler|Packer|Minifier|Merger|Checker)

(Java|Coffee)Script 和 (CSS|Less) (Builder|Bundler|Packer|Minifier|Merger|Checker)

回答by kimballfrank

I like codekit :) Simple and effective way to compile coffeescript, sass, less, haml, and more, with lots of niceties. http://incident57.com/codekit/

我喜欢 codekit :) 编译coffeescript、sass、less、haml等的简单有效的方法,有很多细节。http://incident57.com/codekit/