繁体   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