diff --git a/node/minify.js b/node/minify.js new file mode 100644 index 00000000..a1008f40 --- /dev/null +++ b/node/minify.js @@ -0,0 +1,233 @@ +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 compress=require("compress"); +var path = require('path'); +var Buffer = require('buffer').Buffer; +var gzip = require('gzip'); + +/** + * Answers a http request for the pad javascript + */ +exports.padJS = function(req, res) +{ + res.header("Content-Type","text/javascript"); + + var jsFiles = ["plugins.js", "undo-xpopup.js", "json2.js", "pad_utils.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"]; + + //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 + fs.readFile(".." + filename, "utf-8", function(err, data) + { + //compress the file + if(type == "JS") + { + embeds[item] = " - - - + - - + + + - @@ -40,7 +38,10 @@ var clientVars = {}; // ]]> - +--> + + + @@ -271,4 +272,4 @@ var clientVars = {}; // ]]> - \ No newline at end of file + diff --git a/var/.gitignore b/var/.gitignore index 7ce04197..91f8c095 100644 --- a/var/.gitignore +++ b/var/.gitignore @@ -1 +1,2 @@ -sqlite.db \ No newline at end of file +sqlite.db +minified* diff --git a/var/.placeholder b/var/.placeholder deleted file mode 100644 index e69de29b..00000000