Coffee script compilation

2019-01-21 14:22发布

I'm looking for simplest possible way to automatically recompile coffee scripts into JS.

Reading documentation but still having troubles to get exactly what I want.

I need it to watch folder src/ for any *.coffee files modifications and compile them into concatenated javascript file into lib/something.js.

Somehow can't combine watching, compiling and concatenating together. :/

11条回答
霸刀☆藐视天下
2楼-- · 2019-01-21 14:25

The coffee script documentation provides an example for this:

Watch a file for changes, and recompile it every time the file is saved:

coffee --watch --compile experimental.coffee

If you have a particular script you want to execute, you could use the linux command dnotify: http://linux.die.net/man/1/dnotify

dnotify --all src/ --execute=command

Edit: I had some problems with the --execute part of dnotify - might be a bug, but this is what I got working:

dnotify --all . -e `coffee -o lib/ --join --compile *.coffee`

That executed the compile command each time a file was modified.

If you append the command with an ampersand, like this:

dnotify --all . -e `coffee -o lib/ --join --compile *.coffee` &

it will be started in a separate process. To get the process ID, you can use this:

ps ux | awk '/dnotify/ && !/awk/ {print $2}'

And then, you can kill the process by using something like this:

kill `ps ux | awk '/dnotify/ && !/awk/ {print $2}'`

But if that's your objective (to kill by process name), you can do it in a simpler way by using:

killall dnotify
查看更多
家丑人穷心不美
3楼-- · 2019-01-21 14:25

I have found the command-line coffeescript compiler to be poorly suited for complex project structures.

If you and your build process are as needy as I am, check out Grunt - http://gruntjs.com/

It allows for highly complex build processes - for example, you might

  1. concatenate coffee to new file(s)
  2. compile
  3. concatenate some additional JS
  4. minify

Tasks can be strung together, and watched files/folders are highly customizable as well.

查看更多
萌系小妹纸
4楼-- · 2019-01-21 14:26

The short answer to your question is that the coffee utility wasn't designed for this; combining file-watching and concatenation is actually pretty complex. Expect more sophisticated build tools for CoffeeScript in the near future; until then, you might want to do your project's builds by writing a Cakefile or Ruby Watchr script. Then you can also throw in minification, documentation-generation, and whatever else you need for your particular project (not to mention guaranteeing a particular concatenation order).

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-21 14:31

Try jitter

https://github.com/TrevorBurnham/jitter

It watches a directory of .coffee files, and when it detects that a file has changed it automatically recompiles it to .js

jitter /path/to/coffee/dir /path/to/js/dir

I've been trying it out with a project using coffescript and sencha touch, it seems to work pretty well. Doesn't take care of the concatenation problem, but it's really simple to use for someone who just needs auto-compilation.

查看更多
乱世女痞
6楼-- · 2019-01-21 14:33

This helped me (-o output directory, -j join to project.js, -cw compile and watch coffeescript directory in full depth):

coffee -o web/js -j project.js -cw coffeescript
查看更多
劳资没心,怎么记你
7楼-- · 2019-01-21 14:34

Being one level above /src, this will work for all .coffee files found no matter the depth.

coffee -w -c src/
查看更多
登录 后发表回答