簡體   English   中英

使用YUIcompressor壓縮多個JavaScript文件?

[英]Compressing multiple JavaScript files with YUIcompressor?

我正在嘗試使用YUI Compressor壓縮多個JS文件。

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

具體來說,假設我有三個文件:at_1.js,at_2.js和at_3.js。 我想在at_min.js中壓縮所有三個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的壓縮內容。 我究竟做錯了什么?

如果您使用的是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

我編寫了一個小程序來使用yuicompressor和node 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM