2012-01-29 26 views
8

Tôi muốn có thể chạy một lệnh trong thư mục dự án của mình để nối và nén tất cả các tệp javascript của tôi (có thể với YUI Compressor) vào một tệp đầu ra duy nhất.Đệ quy nén thư mục của các tệp javascript thành một tệp đơn

Nếu có thể, tôi muốn xác định một phần thứ tự mà chúng được nối với nhau nhưng không phải theo dõi từng tệp javascript. Có lẽ một tập tin cấu hình có thể được xây dựng mà trông như thế này:

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

Có ai biết hoặc là một công cụ hiện có để làm một cái gì đó như thế này, có thể whip cùng một bash/ruby ​​/ node/perl script, hoặc thậm chí một tốt hơn phương pháp luận? Tôi đang xây dựng một ứng dụng trang đơn với việc sử dụng JS nặng (~ 40 tệp) để được những người có băng thông thấp sử dụng.

Tôi cần giải pháp có thể thực thi trên máy phát triển OS X của mình.

Trả lời

0

tôi đã kết thúc việc xây dựng một giải pháp trong đó sử dụng một tệp json để liệt kê tất cả các tập tin theo yêu cầu của ứng dụng. Trên môi trường dev, các tệp được trình duyệt tải riêng lẻ. Trên máy chủ sản xuất, tệp được biên dịch lớn được tải. Trên máy dev của tôi, tôi tự chạy một lệnh để lặp qua từng tệp, thêm nó vào tệp JS lớn và chạy Trình nén YUI.

Đó là một chút hacky, nhưng ở đây nó là: https://github.com/renownedmedia/js-compressor

+0

liên kết cho tôi 404 – Lucio

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

Đầu tiên ghép các tệp, sau đó nén chúng.

Nếu bạn thực sự quan tâm, bạn có thể muốn một trình tối ưu hóa JS thực như trình tối ưu hóa RequireJS.

0

Các kịch bản sau đây sẽ làm theo thứ tự của tập tin cấu hình của bạn và sử dụng các mô hình cho

#!/bin/bash 

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

cho một thư mục các tập tin 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 

với một tập tin cấu hình chút thay đổi:

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 

và điều này bash script:

$ 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 

bạn sẽ tìm thấy "sáp nhập" kịch bản trong tất cả-scripts sau khi runing kịch bản:

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 

Hãy cho tôi biết nếu bạn cần tôi để giải thích kịch bản hoặc nếu nó không thành công trên OS X.

Các vấn đề liên quan