Domanda

Ho un mucchio di .coffee file che ho bisogno di unire in un unico file.

Ho cartelle impostato come un applicazione Rails:

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

CoffeeScript ha un comando che consente di unire più coffeescripts in un unico file, ma sembra solo a lavorare con una sola directory. Per esempio, questo funziona bene:

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

Ma ho bisogno di essere in grado di includere un gruppo di sottodirectory un po 'come questo comando non funzionante:

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

C'è un modo per fare questo? C'è un modo Unixy di passare in un elenco di tutti i file nelle sottodirectory?

Sto usando terminal in OSX.

Tutti devono essere uniti in un unico file, perché altrimenti ogni file separato viene compilato e avvolto con questo:

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

che rompe la portata di alcune chiamate di funzione.

È stato utile?

Soluzione

Si potrebbe scrivere un compito script di shell o Rake di combinare insieme prima, quindi compilare. Qualcosa di simile:

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

Poi compilazione output.coffee

Regolare i percorsi per le vostre esigenze. Assicurarsi inoltre che il file output.coffee non è nello stesso percorso che si sta cercando con find o si otterrà in un ciclo infinito.

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

Inoltre si può essere interessati a queste altre messaggi su StackOverflow riguardante la ricerca attraverso le directory:

Altri suggerimenti

CoffeeScript documentazione :

j, --join [FILE]: Prima di compilare, concatenare tutti gli script insieme nell'ordine in cui sono stati passati, e scriverli nel file specificato. Utile per la costruzione di grandi progetti.

Quindi, si può raggiungere il tuo obiettivo nella riga di comando (io uso bash) in questo modo:

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

dove file1 - fileN sono i percorsi dei file CoffeeScript si desidera compilare

.

Ho appena rilasciare una versione alpha di CoffeeToaster, penso che potrebbe aiutare. http://github.com/serpentem/coffee-toaster

Il modo più semplice per strumento a riga di comando l'uso del caffè.

caffè --output pubblico --join --compile app

applicazione è la mia directory di lavoro in possesso di molteplici sottodirectory e pubblico è dove verrà posizionato il file ~ output.js. Facile per automatizzare questo processo se la scrittura applicazione in nodejs

Questo mi ha aiutato (directory di output -o, -j uniscono per project.js, -cw compilazione e guardare directory CoffeeScript in tutta la profondità):

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top