Question

I've got a bunch of .coffee files that I need to join into one file.

I have folders set up like a rails app:

/src/controller/log_controller.coffee
/src/model/log.coffee
/src/views/logs/new.coffee

Coffeescript has a command that lets you join multiple coffeescripts into one file, but it only seems to work with one directory. For example this works fine:

coffee --output app/controllers.js --join --compile src/controllers/*.coffee

But I need to be able to include a bunch of subdirectories kind of like this non-working command:

coffee --output app/all.js --join --compile src/*/*.coffee

Is there a way to do this? Is there a UNIXy way to pass in a list of all the files in the subdirectories?

I'm using terminal in OSX.

They all have to be joined in one file because otherwise each separate file gets compiled & wrapped with this:

(function() { }).call(this);

Which breaks the scope of some function calls.

Was it helpful?

Solution

You could write a shell script or Rake task to combine them together first, then compile. Something like:

find . -type f -name '*.coffee' -print0 | xargs -0 cat > output.coffee

Then compile output.coffee

Adjust the paths to your needs. Also make sure that the output.coffee file is not in the same path you're searching with find or you will get into an infinite loop.

http://man.cx/find | http://www.rubyrake.org/tutorial/index.html

Additionally you may be interested in these other posts on Stackoverflow concerning searching across directories:

OTHER TIPS

From the CoffeeScript documentation:

-j, --join [FILE] : Before compiling, concatenate all scripts together in the order they were passed, and write them into the specified file. Useful for building large projects.

So, you can achieve your goal at the command line (I use bash) like this:

coffee -cj path/to/compiled/file.js file1 file2 file3 file4

where file1 - fileN are the paths to the coffeescript files you want to compile.

I've just release an alpha release of CoffeeToaster, I think it may help you. http://github.com/serpentem/coffee-toaster

The most easy way to use coffee command line tool.

coffee --output public --join --compile app

app is my working directory holding multiple subdirectories and public is where ~output.js file will be placed. Easy to automate this process if writing app in nodejs

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

Use cake to compile them all in one (or more) resulting .js file(s). Cakefile is used as configuration which controls in which order your coffee scripts are compiled - quite handy with bigger projects.

Cake is quite easy to install and setup, invoking cake from vim while you are editing your project is then simply

:!cake build

and you can refresh your browser and see results.

As I'm also busy to learn the best way of structuring the files and use coffeescript in combination with backbone and cake, I have created a small project on github to keep it as a reference for myself, maybe it will help you too around cake and some basic things. All compiled files are in www folder so that you can open them in your browser and all source files (except for cake configuration) are in src folder. In this example, all .coffee files are compiled and combined in one output .js file which is then included in html.

Alternatively, you could use the --bare flag, compile to JavaScript, and then perhaps wrap the JS if necessary. But this would likely create problems; for instance, if you have one file with the code

i = 0
foo = -> i++
...
foo()

then there's only one var i declaration in the resulting JavaScript, and i will be incremented. But if you moved the foo function declaration to another CoffeeScript file, then its i would live in the foo scope, and the outer i would be unaffected.

So concatenating the CoffeeScript is a wiser solution, but there's still potential for confusion there; the order in which you concatenate your code is almost certainly going to matter. I strongly recommend modularizing your code instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top