2011-05-28 22:05:43 +02:00
|
|
|
/**
|
2011-05-30 16:53:11 +02:00
|
|
|
* This Module manages all /minified/* requests. It controls the
|
|
|
|
* minification && compression of Javascript and CSS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-05-28 22:05:43 +02:00
|
|
|
* 2011 Peter 'Pita' Martischka
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-05-28 19:09:17 +02:00
|
|
|
var settings = require('./settings');
|
|
|
|
var async = require('async');
|
|
|
|
var fs = require('fs');
|
|
|
|
var cleanCSS = require('clean-css');
|
|
|
|
var jsp = require("uglify-js").parser;
|
|
|
|
var pro = require("uglify-js").uglify;
|
|
|
|
var path = require('path');
|
|
|
|
var Buffer = require('buffer').Buffer;
|
|
|
|
var gzip = require('gzip');
|
2011-05-28 21:43:00 +02:00
|
|
|
var server = require('./server');
|
2011-05-28 19:09:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Answers a http request for the pad javascript
|
2011-05-30 16:53:11 +02:00
|
|
|
* @param req the Express request
|
|
|
|
* @param res the Express response
|
2011-05-28 19:09:17 +02:00
|
|
|
*/
|
|
|
|
exports.padJS = function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Content-Type","text/javascript");
|
|
|
|
|
2011-07-19 18:41:04 +02:00
|
|
|
var jsFiles = ["jquery.min.js", "pad_utils.js", "plugins.js", "undo-xpopup.js", "json2.js", "pad_cookie.js", "pad_editor.js", "pad_editbar.js", "pad_docbar.js", "pad_modals.js", "ace.js", "collab_client.js", "pad_userlist.js", "pad_impexp.js", "pad_savedrevs.js", "pad_connectionstatus.js", "pad2.js", "jquery-ui.js", "chat.js"];
|
2011-05-28 19:09:17 +02:00
|
|
|
|
|
|
|
//minifying is enabled
|
|
|
|
if(settings.minify)
|
|
|
|
{
|
|
|
|
var fileValues = {};
|
|
|
|
var embeds = {};
|
|
|
|
var latestModification = 0;
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
//find out the highest modification date
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
var folders2check = ["../static/css","../static/js"];
|
|
|
|
|
|
|
|
//go trough this two folders
|
|
|
|
async.forEach(folders2check, function(path, callback)
|
|
|
|
{
|
|
|
|
//read the files in the folder
|
|
|
|
fs.readdir(path, function(err, files)
|
|
|
|
{
|
|
|
|
if(err) { callback(err); return; }
|
|
|
|
|
|
|
|
//we wanna check the directory itself for changes too
|
|
|
|
files.push(".");
|
|
|
|
|
|
|
|
//go trough all files in this folder
|
|
|
|
async.forEach(files, function(filename, callback)
|
|
|
|
{
|
|
|
|
//get the stat data of this file
|
|
|
|
fs.stat(path + "/" + filename, function(err, stats)
|
|
|
|
{
|
|
|
|
if(err) { callback(err); return; }
|
|
|
|
|
|
|
|
//get the modification time
|
|
|
|
var modificationTime = stats.mtime.getTime();
|
|
|
|
|
|
|
|
//compare the modification time to the highest found
|
|
|
|
if(modificationTime > latestModification)
|
|
|
|
{
|
|
|
|
latestModification = modificationTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
},
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//check the modification time of the minified js
|
|
|
|
fs.stat("../var/minified_pad.js", function(err, stats)
|
|
|
|
{
|
|
|
|
if(err && err.code != "ENOENT") callback(err);
|
|
|
|
|
|
|
|
//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)
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
//the minified file is still up to date, stop minifying
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback("stop");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//load all js files
|
|
|
|
function (callback)
|
|
|
|
{
|
|
|
|
async.forEach(jsFiles, function (item, callback)
|
|
|
|
{
|
|
|
|
fs.readFile("../static/js/" + item, "utf-8", function(err, data)
|
|
|
|
{
|
|
|
|
fileValues[item] = data;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
},
|
|
|
|
//find all includes in ace.js and embed them
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
var founds = fileValues["ace.js"].match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"]+\)/gi);
|
|
|
|
|
|
|
|
//go trough all includes
|
|
|
|
async.forEach(founds, function (item, callback)
|
|
|
|
{
|
|
|
|
var filename = item.match(/"[^"]*"/g)[0].substr(1);
|
|
|
|
filename = filename.substr(0,filename.length-1);
|
|
|
|
|
|
|
|
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
|
|
|
|
|
|
|
|
var quote = item.search("_Q") != -1;
|
|
|
|
|
|
|
|
//read the included file
|
2011-07-25 15:38:54 +02:00
|
|
|
fs.readFile(filename, "utf-8", function(err, data)
|
2011-05-28 19:09:17 +02:00
|
|
|
{
|
|
|
|
//compress the file
|
|
|
|
if(type == "JS")
|
|
|
|
{
|
|
|
|
embeds[item] = "<script>\n" + compressJS([data])+ "\n\\x3c/script>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
embeds[item] = "<style>" + compressCSS([data])+ "</style>";
|
|
|
|
}
|
|
|
|
|
|
|
|
//do the first escape
|
|
|
|
embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\\"/g, "\"");
|
|
|
|
embeds[item] = embeds[item].substr(1);
|
|
|
|
embeds[item] = embeds[item].substr(0, embeds[item].length-1);
|
|
|
|
|
|
|
|
//add quotes, if wished
|
|
|
|
if(quote)
|
|
|
|
{
|
|
|
|
embeds[item] = "'" + embeds[item] + "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
//do the second escape
|
|
|
|
embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\"/g, "\"");
|
|
|
|
embeds[item] = embeds[item].substr(1);
|
|
|
|
embeds[item] = embeds[item].substr(0, embeds[item].length-1);
|
|
|
|
embeds[item] = "'" + embeds[item] + "'";
|
|
|
|
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}, function(err)
|
|
|
|
{
|
|
|
|
//replace the include command with the include
|
|
|
|
for(var i in embeds)
|
|
|
|
{
|
|
|
|
fileValues["ace.js"]=fileValues["ace.js"].replace(i, embeds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//put all together and write it into a file
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//put all javascript files in an array
|
|
|
|
var values = [];
|
2011-06-30 19:06:07 +02:00
|
|
|
for(var i in jsFiles)
|
2011-05-28 19:09:17 +02:00
|
|
|
{
|
2011-06-30 19:06:07 +02:00
|
|
|
values.push(fileValues[jsFiles[i]]);
|
2011-05-28 19:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//minify all javascript files to one
|
|
|
|
var result = compressJS(values);
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
//write the results plain in a file
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
fs.writeFile("../var/minified_pad.js", result, "utf8", callback);
|
|
|
|
},
|
|
|
|
//write the results compressed in a file
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
gzip(result, 9, function(err, compressedResult){
|
|
|
|
if(err) {callback(err); return}
|
|
|
|
|
|
|
|
fs.writeFile("../var/minified_pad.js.gz", compressedResult, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],callback);
|
|
|
|
}
|
|
|
|
], function(err)
|
|
|
|
{
|
|
|
|
if(err && err != "stop") throw err;
|
|
|
|
|
|
|
|
//check if gzip is supported by this browser
|
|
|
|
var gzipSupport = req.header('Accept-Encoding', '').indexOf('gzip') != -1;
|
|
|
|
|
|
|
|
var pathStr;
|
|
|
|
if(gzipSupport)
|
|
|
|
{
|
|
|
|
pathStr = path.normalize(__dirname + "/../var/minified_pad.js.gz");
|
|
|
|
res.header('Content-Encoding', 'gzip');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pathStr = path.normalize(__dirname + "/../var/minified_pad.js");
|
|
|
|
}
|
|
|
|
|
2011-06-30 13:40:37 +02:00
|
|
|
res.sendfile(pathStr, { maxAge: server.maxAge });
|
2011-05-28 19:09:17 +02:00
|
|
|
})
|
|
|
|
}
|
2011-07-24 21:18:47 +02:00
|
|
|
//minifying is disabled, so put the files together in one file
|
2011-05-28 19:09:17 +02:00
|
|
|
else
|
|
|
|
{
|
2011-07-24 21:18:47 +02:00
|
|
|
var fileValues = {};
|
2011-07-14 21:11:03 +02:00
|
|
|
|
2011-07-24 21:18:47 +02:00
|
|
|
//read all js files
|
|
|
|
async.forEach(jsFiles, function (item, callback)
|
2011-05-28 19:09:17 +02:00
|
|
|
{
|
2011-07-24 21:18:47 +02:00
|
|
|
fs.readFile("../static/js/" + item, "utf-8", function(err, data)
|
|
|
|
{
|
|
|
|
fileValues[item] = data;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//send all files together
|
|
|
|
function(err)
|
|
|
|
{
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
for(var i=0;i<jsFiles.length;i++)
|
|
|
|
{
|
|
|
|
var fileName = jsFiles[i];
|
|
|
|
res.write("\n\n\n/*** File: static/js/" + fileName + " ***/\n\n\n");
|
|
|
|
res.write(fileValues[fileName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
2011-05-28 19:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function compressJS(values)
|
|
|
|
{
|
|
|
|
var complete = values.join("\n");
|
|
|
|
var ast = jsp.parse(complete); // parse code and get the initial AST
|
|
|
|
ast = pro.ast_mangle(ast); // get a new AST with mangled names
|
|
|
|
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
|
|
|
|
return pro.gen_code(ast); // compressed code here
|
|
|
|
}
|
|
|
|
|
|
|
|
function compressCSS(values)
|
|
|
|
{
|
|
|
|
var complete = values.join("\n");
|
|
|
|
return cleanCSS.process(complete);
|
|
|
|
}
|