45

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

fancy
  • 48,619
  • 62
  • 153
  • 231
  • 1
    CoffeeScript compiler setup for WebStorm http://zsitro.com/coffeescript-compiler-setup-for-webstorm/ – zsitro Jan 04 '13 at 09:58

7 Answers7

66

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

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.

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

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.

Elf Sternberg
  • 16,129
  • 6
  • 60
  • 68
  • 4
    Elf's answer is correct. There's also Jitter, a small project of mine that adds a few extra features (like Growl notifications when there are compilation errors): http://github.com/TrevorBurnham/jitter – Trevor Burnham Jun 09 '11 at 02:19
  • If you are using the HAML, SASS/Compass and CoffeeScript combo you might as well give [StaticMatic 2](https://github.com/mindeavor/staticmatic2) a try. – jaime Jun 09 '11 at 04:11
  • Thanks this got me pointed in the right direction. I used the command `coffee -c -o dirToBuildTo --watch dirToBuildFrom` works great. – fancy Jun 09 '11 at 04:19
  • I wrote this, which does the trick for coffeescript and less. https://bitbucket.org/poulejapon/tumbler/downloads – fulmicoton Jul 19 '11 at 20:50
19

You can also accomplish this without the command line:

  1. Add a build process to Sublime Text.
  2. Make sure that Save All on Build is selected in the Tools menu.
  3. Use ⌘B instead of ⌘S when saving.

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

Jed Schmidt
  • 2,907
  • 23
  • 23
3

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.

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

matanster
  • 15,072
  • 19
  • 88
  • 167
2

Well coffee --watch has 2 major flaws:

  • 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 commit it

The solution I came up with is a rather simple Bash script that takes coffee --watch a 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:

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

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

gsklee
  • 4,774
  • 4
  • 39
  • 55
  • 6
    Try `coffee -wc -o . .` which will compile all old and new .coffee files. It's only when you do: `coffee -wc -o . ./*.coffee` that your first "flaw" appears. – corecursion Jun 10 '12 at 13:45
1

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

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

balupton
  • 47,113
  • 32
  • 131
  • 182
1
gem install stasis
stasis -d

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

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

https://github.com/winton/stasis

vaughan
  • 6,982
  • 6
  • 47
  • 63
0

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