fix issue #281; remove white spaces

This commit is contained in:
booo 2011-12-21 18:18:22 +01:00
parent ddf1cd345c
commit 22332dbe9d
2 changed files with 52 additions and 55 deletions

View file

@ -1,7 +1,7 @@
/** /**
* This Module manages all /minified/* requests. It controls the * This Module manages all /minified/* requests. It controls the
* minification && compression of Javascript and CSS. * minification && compression of Javascript and CSS.
*/ */
/* /*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd) * 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
@ -28,7 +28,7 @@ var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify; var pro = require("uglify-js").uglify;
var path = require('path'); var path = require('path');
var Buffer = require('buffer').Buffer; var Buffer = require('buffer').Buffer;
var gzip = require('gzip'); var zlib = require('zlib');
var server = require('../server'); var server = require('../server');
var os = require('os'); var os = require('os');
@ -44,7 +44,7 @@ var timesliderJS = ["jquery.min.js", "plugins.js", "undo-xpopup.js", "json2.js",
exports.minifyJS = function(req, res, jsFilename) exports.minifyJS = function(req, res, jsFilename)
{ {
res.header("Content-Type","text/javascript"); res.header("Content-Type","text/javascript");
//choose the js files we need //choose the js files we need
if(jsFilename == "pad.js") if(jsFilename == "pad.js")
{ {
@ -58,20 +58,20 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
throw new Error("there is no profile for creating " + name); throw new Error("there is no profile for creating " + name);
} }
//minifying is enabled //minifying is enabled
if(settings.minify) if(settings.minify)
{ {
var fileValues = {}; var fileValues = {};
var embeds = {}; var embeds = {};
var latestModification = 0; var latestModification = 0;
async.series([ async.series([
//find out the highest modification date //find out the highest modification date
function(callback) function(callback)
{ {
var folders2check = ["../static/css","../static/js"]; var folders2check = ["../static/css","../static/js"];
//go trough this two folders //go trough this two folders
async.forEach(folders2check, function(path, callback) async.forEach(folders2check, function(path, callback)
{ {
@ -79,27 +79,27 @@ exports.minifyJS = function(req, res, jsFilename)
fs.readdir(path, function(err, files) fs.readdir(path, function(err, files)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//we wanna check the directory itself for changes too //we wanna check the directory itself for changes too
files.push("."); files.push(".");
//go trough all files in this folder //go trough all files in this folder
async.forEach(files, function(filename, callback) async.forEach(files, function(filename, callback)
{ {
//get the stat data of this file //get the stat data of this file
fs.stat(path + "/" + filename, function(err, stats) fs.stat(path + "/" + filename, function(err, stats)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//get the modification time //get the modification time
var modificationTime = stats.mtime.getTime(); var modificationTime = stats.mtime.getTime();
//compare the modification time to the highest found //compare the modification time to the highest found
if(modificationTime > latestModification) if(modificationTime > latestModification)
{ {
latestModification = modificationTime; latestModification = modificationTime;
} }
callback(); callback();
}); });
}, callback); }, callback);
@ -116,7 +116,7 @@ exports.minifyJS = function(req, res, jsFilename)
ERR(err, callback); ERR(err, callback);
return; return;
} }
//there is no minfied file or there new changes since this file was generated, so continue generating this file //there is no minfied file or there new changes since this file was generated, so continue generating this file
if((err && err.code == "ENOENT") || stats.mtime.getTime() < latestModification) if((err && err.code == "ENOENT") || stats.mtime.getTime() < latestModification)
{ {
@ -128,48 +128,48 @@ exports.minifyJS = function(req, res, jsFilename)
callback("stop"); callback("stop");
} }
}); });
}, },
//load all js files //load all js files
function (callback) function (callback)
{ {
async.forEach(jsFiles, function (item, callback) async.forEach(jsFiles, function (item, callback)
{ {
fs.readFile("../static/js/" + item, "utf-8", function(err, data) fs.readFile("../static/js/" + item, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
fileValues[item] = data; fileValues[item] = data;
callback(); callback();
}); });
}, callback); }, callback);
}, },
//find all includes in ace.js and embed them //find all includes in ace.js and embed them
function(callback) function(callback)
{ {
//if this is not the creation of pad.js, skip this part //if this is not the creation of pad.js, skip this part
if(jsFilename != "pad.js") if(jsFilename != "pad.js")
{ {
callback(); callback();
return; return;
} }
var founds = fileValues["ace.js"].match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"]+\)/gi); var founds = fileValues["ace.js"].match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"]+\)/gi);
//go trough all includes //go trough all includes
async.forEach(founds, function (item, callback) async.forEach(founds, function (item, callback)
{ {
var filename = item.match(/"[^"]*"/g)[0].substr(1); var filename = item.match(/"[^"]*"/g)[0].substr(1);
filename = filename.substr(0,filename.length-1); filename = filename.substr(0,filename.length-1);
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length); var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
var quote = item.search("_Q") != -1; var quote = item.search("_Q") != -1;
//read the included file //read the included file
fs.readFile(filename, "utf-8", function(err, data) fs.readFile(filename, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//compress the file //compress the file
if(type == "JS") if(type == "JS")
{ {
embeds[item] = "<script>\n" + compressJS([data])+ "\n\\x3c/script>"; embeds[item] = "<script>\n" + compressJS([data])+ "\n\\x3c/script>";
@ -178,36 +178,36 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
embeds[item] = "<style>" + compressCSS([data])+ "</style>"; embeds[item] = "<style>" + compressCSS([data])+ "</style>";
} }
//do the first escape //do the first escape
embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\\"/g, "\""); embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\\"/g, "\"");
embeds[item] = embeds[item].substr(1); embeds[item] = embeds[item].substr(1);
embeds[item] = embeds[item].substr(0, embeds[item].length-1); embeds[item] = embeds[item].substr(0, embeds[item].length-1);
//add quotes, if wished //add quotes, if wished
if(quote) if(quote)
{ {
embeds[item] = "'" + embeds[item] + "'"; embeds[item] = "'" + embeds[item] + "'";
} }
//do the second escape //do the second escape
embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\"/g, "\""); embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\"/g, "\"");
embeds[item] = embeds[item].substr(1); embeds[item] = embeds[item].substr(1);
embeds[item] = embeds[item].substr(0, embeds[item].length-1); embeds[item] = embeds[item].substr(0, embeds[item].length-1);
embeds[item] = "'" + embeds[item] + "'"; embeds[item] = "'" + embeds[item] + "'";
callback(); callback();
}); });
}, function(err) }, function(err)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//replace the include command with the include //replace the include command with the include
for(var i in embeds) for(var i in embeds)
{ {
fileValues["ace.js"]=fileValues["ace.js"].replace(i, embeds[i]); fileValues["ace.js"]=fileValues["ace.js"].replace(i, embeds[i]);
} }
callback(); callback();
}); });
}, },
@ -220,29 +220,27 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
values.push(fileValues[jsFiles[i]]); values.push(fileValues[jsFiles[i]]);
} }
//minify all javascript files to one //minify all javascript files to one
var result = compressJS(values); var result = compressJS(values);
async.parallel([ async.parallel([
//write the results plain in a file //write the results plain in a file
function(callback) function(callback)
{ {
fs.writeFile("../var/minified_" + jsFilename, result, "utf8", callback); fs.writeFile("../var/minified_" + jsFilename, result, "utf8", callback);
}, },
//write the results compressed in a file //write the results compressed in a file
function(callback) function(callback)
{ {
//spawn a gzip process if we're on a unix system //gzip file if we're on a unix system
if(os.type().indexOf("Windows") == -1) if(os.type().indexOf("Windows") == -1)
{ {
gzip(result, 9, function(err, compressedResult){ zlib.gzip(result, function(err, compressedResult){
//weird gzip bug that returns 0 instead of null if everything is ok
err = err === 0 ? null : err;
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
fs.writeFile("../var/minified_" + jsFilename + ".gz", compressedResult, callback); fs.writeFile("../var/minified_" + jsFilename + ".gz", compressedResult, callback);
}); });
} }
//skip this step on windows //skip this step on windows
@ -259,10 +257,10 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
if(ERR(err)) return; if(ERR(err)) return;
} }
//check if gzip is supported by this browser //check if gzip is supported by this browser
var gzipSupport = req.header('Accept-Encoding', '').indexOf('gzip') != -1; var gzipSupport = req.header('Accept-Encoding', '').indexOf('gzip') != -1;
var pathStr; var pathStr;
if(gzipSupport && os.type().indexOf("Windows") == -1) if(gzipSupport && os.type().indexOf("Windows") == -1)
{ {
@ -273,7 +271,7 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
pathStr = path.normalize(__dirname + "/../../var/minified_" + jsFilename ); pathStr = path.normalize(__dirname + "/../../var/minified_" + jsFilename );
} }
res.sendfile(pathStr, { maxAge: server.maxAge }); res.sendfile(pathStr, { maxAge: server.maxAge });
}) })
} }
@ -281,29 +279,29 @@ exports.minifyJS = function(req, res, jsFilename)
else else
{ {
var fileValues = {}; var fileValues = {};
//read all js files //read all js files
async.forEach(jsFiles, function (item, callback) async.forEach(jsFiles, function (item, callback)
{ {
fs.readFile("../static/js/" + item, "utf-8", function(err, data) fs.readFile("../static/js/" + item, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
fileValues[item] = data; fileValues[item] = data;
callback(); callback();
}); });
}, },
//send all files together //send all files together
function(err) function(err)
{ {
if(ERR(err)) return; if(ERR(err)) return;
for(var i=0;i<jsFiles.length;i++) for(var i=0;i<jsFiles.length;i++)
{ {
var fileName = jsFiles[i]; var fileName = jsFiles[i];
res.write("\n\n\n/*** File: static/js/" + fileName + " ***/\n\n\n"); res.write("\n\n\n/*** File: static/js/" + fileName + " ***/\n\n\n");
res.write(fileValues[fileName]); res.write(fileValues[fileName]);
} }
res.end(); res.end();
}); });
} }

View file

@ -5,7 +5,7 @@
"keywords" : ["etherpad", "realtime", "collaborative", "editor"], "keywords" : ["etherpad", "realtime", "collaborative", "editor"],
"author" : "Peter 'Pita' Martischka <petermartischka@googlemail.com> - Primary Technology Ltd", "author" : "Peter 'Pita' Martischka <petermartischka@googlemail.com> - Primary Technology Ltd",
"contributors": [ "contributors": [
{ "name": "John McLear", { "name": "John McLear",
"name": "Hans Pinckaers", "name": "Hans Pinckaers",
"name": "Robin Buse"} "name": "Robin Buse"}
], ],
@ -17,7 +17,6 @@
"express" : "2.5.0", "express" : "2.5.0",
"clean-css" : "0.2.4", "clean-css" : "0.2.4",
"uglify-js" : "1.1.1", "uglify-js" : "1.1.1",
"gzip" : "0.1.0",
"formidable" : "1.0.7", "formidable" : "1.0.7",
"log4js" : "0.3.9", "log4js" : "0.3.9",
"jsdom-nocontextifiy" : "0.2.10", "jsdom-nocontextifiy" : "0.2.10",