2011-09-22 90 views
6

我想使用YUI壓縮器壓縮多個JS文件。用YUIcompressor壓縮多個JavaScript文件?

我認爲我得到的語法錯誤。我想壓縮以at_開頭的目錄中的所有文件。但是,當YUI Compressor運行時,我發現YUI Compressor只在輸出中放置了一個文件的壓縮版本。

具體而言,假設我有三個文件:at_1.js,at_2.js和at_3.js。 我想所有三個js文件中at_min.js

壓縮輸出我使用的語法如下:

java -jar c:\Tools\yuicompressor-2.4.2.jar --type js --charset utf-8 -o c:\temp\at_min.js c:\temp\scripts\at_* 

當我打開at_min.js,我只找到壓縮內容at_1.js。我究竟做錯了什麼?

回答

5

如果您使用Windows,您可以使用YUI Compressor for .Net來做到這一點。

或用一個簡單的命令壓縮之前組合文件:

copy /b at_1.js+at_2.js+at_3.js at_combined.js 
java -jar c:\Tools\yuicompressor-2.4.2.jar --type js --charset utf-8 -o at_min.js at_combined.js 
+5

對於unix,使用'cat src1 src2 src3> dest' –

0

我已經寫了一個小程序,壓縮使用的YUICompressor和節點JS多個JavaScript文件。

var compressor = require('yuicompressor'); 

//Compressor Options: 
var compressorOptions = { 

charset: 'utf8', 
type: 'js', 
nomunge: false 
} 

/* List of files and file path. Just replace the file names and path with yours */ 
    var file = [{ 
    "path": "assets/www/modules/eApp/controllers/", 
    "type": "js", 
    "name": ["BuyOnlineController", "CustomerDetailsController", "DashboardController", "DashboardListingController", "DocumentUploadController", "HomeController", "KYCDetailsController", "PaymentAcknowledgementController", "PaymentController", "ProductListingController", "ReviewAndAcceptanceController"] 
}, 
{ 
    "path": "assets/www/modules/login/controllers/", 
    "type": "js", 
    "name": ["EappLoginController", "InboxController", "LandingController", "LoginController", "MenuController", "MyAccountController", "SyncForEappController"] 
}, 
{ 
    "path": "assets/www/lib/vendor/general/", 
    "type": "js", 
    "name": ["overlays"] 
}]; 

function minify(i, j){ 
    i = (i == undefined) ? 0 : i; 
    j = (j == undefined) ? 0 : j; 
    filePath = file[i].path; 
    fileType = file[i].type; 
    name = file[i].name[j]; 
    fileName = filePath+name+"."+fileType; 
    minifiedFileName = filePath+name+".min."+fileType; 

    if(j == file[i].name.length - 1){ 
     i += 1; 
     j = 0; 
    } 
    else 
     j += 1; 

    compressor.compress(fileName, compressorOptions, function(err, data, extra) { 
     var fs = require('fs'); 
     fs.writeFile(minifiedFileName, data, function(err) { 
      if(err) { 
       console.log(err); 
      } else { 
       console.log("The file "+minifiedFileName+" was saved successfully!"); 
       if(i != file.length) 
        minify(i, j); 

      } 
     }); 
    }); 


} 

minify(0,0);