2012-01-29 8 views
8

Vorrei poter eseguire un singolo comando nella cartella del mio progetto per concatenare e comprimere tutti i miei file javascript (magari con YUI Compressor) in un singolo file di output.Comprime ricorsivamente la directory di file javascript in un singolo file

Se possibile, vorrei specificare parzialmente l'ordine in cui sono concatenati insieme ma non è necessario tenere traccia di ogni singolo file javascript. Forse un file di configurazione potrebbe essere costruito che assomiglia a questo:

application.js 
excanvas.js 
json2.js 
jquery*.js 
flot/* 
backbone*.js 
app/screen-*.js 
app/main.js 
app/crud-*.js 
app/* 
* 

Qualcuno sa di uno strumento esistente per fare qualcosa di simile, potrebbe frusta insieme un/ruby ​​/ node/script perl bash, o anche una migliore metodologia? Sto costruendo un'applicazione per pagina singola con un uso pesante di JS (~ 40 file) per essere utilizzata da persone con larghezza di banda ridotta.

Avrei bisogno che la soluzione fosse eseguibile sul mio computer di sviluppo OS X.

risposta

0

ho finito per la costruzione di una soluzione che utilizza un file JSON per elencare tutti i file richiesti dal app. Nell'ambiente dev i file vengono caricati singolarmente dal browser. Sul server di produzione, viene caricato il grande file compilato. Sulla mia macchina dev, eseguo manualmente un comando per iterare su ogni file, aggiungendolo a un grande file JS ed eseguendo YUI Compressor.

E 'un po' hacky, ma qui è: https://github.com/renownedmedia/js-compressor

+0

i collegamenti mi danno 404 – Lucio

3
find . -iname "*.js" -exec cat "{}" \; > singlefile.js 
[JS compressor] singlefile.js 

Prima concatenare i file, quindi li comprime.

Se davvero ti interessa, però, potresti volere un vero ottimizzatore JS come l'ottimizzatore RequireJS.

0

Il seguente script seguirà l'ordine dei file di configurazione e utilizzare i modelli di data

#!/bin/bash 

shopt -s nullglob; 
while read config; do 
    cat $config >> out.js 
done < /path/to/config/file 
2

dato una cartella di file javascript:

geee: ~/src/bash/js-files 
$ find . 
. 
./application.js 
./jquery-ui.js 
./all-scripts.js 
./cp.js 
./excanvas.js 
./backbone-worldwide.js 
./jquery-plugin.js 
./.found 
./app 
./app/crud-sel.js 
./app/screen-detach.js 
./app/aligator.js 
./app/crud-in.js 
./app/giraffe.js 
./app/screen-attach.js 
./app/main.js 
./app/crud-del.js 
./app/mouse.js 
./app/monkey.js 
./app/screen-shot.js 
./backbone-national.js 
./backbone23.js 
./ummap.js 
./CONFIG 
./backbone-ibm.js 
./ieee754.js 
./flot 
./flot/cow.js 
./flot/moo.js 
./flot/cat.js 
./flot/bull.js 
./flot/dog.js 
./flot/sheep.js 
./lines 
./droiddraw-r1b21 
./droiddraw-r1b21/._readme.txt 
./droiddraw-r1b21/readme.js 
./droiddraw-r1b21/LICENSE.js 
./jquery-1.7.js 
./ole.js 
./touch 
./json2.js 
./xls2txt.js 
./DO.sh 
./backbone-isp.js 

con un file di configurazione leggermente modificata:

geee: ~/src/bash/js-files 
$ cat CONFIG 
application.js 
excanvas.js 
json2.js 
jquery*.js 
flot/* 
backbone*.js 
app/screen-*.js 
app/main.js 
app/crud-*.js 
app/*js 
*js 

e questo script di bash:

$ cat DO.sh 
PROJECT=/home/jaroslav/src/bash/js-files  # top folder of the web-app 
SUPERJS=${PROJECT}/all-scripts.js 
CONFIG=${PROJECT}/CONFIG      # your the priority file (notice *js) 
FOUND=${PROJECT}/.found       # where to save results 
JSMIN=$HOME/bin/jsmin       # change to /usr/local/bin/jsmin or some other tool 

echo > $FOUND         # remove results from previous run 

if [ ! -x $JSMIN ] 
then 
     TMPJSMIN=/tmp/jsmin.c 
     wget -q https://github.com/douglascrockford/JSMin/raw/master/jsmin.c -O $TMPJSMIN & FOR=$? 
     echo "fetching jsmin (by Douglas Crockford) from github" 
     wait $FOR 
     gcc -o $JSMIN $TMPJSMIN 
fi 




cat $CONFIG | \ 
     while read priority 
     do 
       eval "find $priority|sort -n" | \ 
         while read amatch; 
         do 
           grep -q $amatch $FOUND || echo $amatch >> $FOUND 
         done 
     done 
echo minifying: 
cat $FOUND 
cat `cat $FOUND` | $JSMIN > $SUPERJS 

si trova il "fuse" scritto in tutti i-script dopo runing lo script:

geee: ~/src/bash/js-files 
$ . DO.sh 
fetching jsmin (by Douglas Crockford) from github 
[1]+ Done     wget -q https://github.com/douglascrockford/JSMin/raw/master/jsmin.c -O $TMPJSMIN 
minifying: 

application.js 
excanvas.js 
json2.js 
jquery-1.7.js 
jquery-plugin.js 
jquery-ui.js 
flot/bull.js 
flot/cat.js 
flot/cow.js 
flot/dog.js 
flot/moo.js 
flot/sheep.js 
backbone23.js 
backbone-ibm.js 
backbone-isp.js 
backbone-national.js 
backbone-worldwide.js 
app/screen-attach.js 
app/screen-detach.js 
app/screen-shot.js 
app/main.js 
app/crud-del.js 
app/crud-in.js 
app/crud-sel.js 
app/aligator.js 
app/giraffe.js 
app/monkey.js 
app/mouse.js 
all-scripts.js 
cp.js 
ieee754.js 
ole.js 
ummap.js 
xls2txt.js 

fatemi sapere se hai bisogno di me per spiegare lo script o se non riesce su OS X.

Problemi correlati