From 0875ba625261bab4bdcdf1418362bf0a5f3eb46c Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Sun, 27 Nov 2011 15:49:17 -0500 Subject: [PATCH] Auto-compress javascript assets --- TODO.md | 1 - config.js | 2 ++ package.json | 3 ++- server.js | 20 ++++++++++++++++++++ static/ZeroClipboard.min.js | 1 + static/application.min.js | 1 + static/index.html | 4 ++-- 7 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 static/ZeroClipboard.min.js create mode 100644 static/application.min.js diff --git a/TODO.md b/TODO.md index 58fc476..0b18e20 100644 --- a/TODO.md +++ b/TODO.md @@ -5,7 +5,6 @@ * Add file extensions ourselves to push state * Proper markdown highlighting * Better about page text -* auto-compress assets * add feedback for errors to UI - esp. too long # shared version only diff --git a/config.js b/config.js index f6e97b3..0208e7f 100644 --- a/config.js +++ b/config.js @@ -9,6 +9,8 @@ "staticMaxAge": 86400, + "recompressStaticAssets": true, + "logging": [ { "level": "verbose", diff --git a/package.json b/package.json index a9b1cba..9c69eaa 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "dependencies": { "winston": "*", "hashlib": "*", - "connect": "*" + "connect": "*", + "uglify-js": "*" }, "devDependencies": { diff --git a/server.js b/server.js index 818712d..5f19095 100644 --- a/server.js +++ b/server.js @@ -37,6 +37,26 @@ if (!config.storage.type) { var Store = require('./lib/' + config.storage.type + '_document_store'); var preferredStore = new Store(config.storage); +// Compress the static javascript assets +if (config.recompressStaticAssets) { + var jsp = require("uglify-js").parser; + var pro = require("uglify-js").uglify; + var list = fs.readdirSync('./static'); + for (var i = 0; i < list.length; i++) { + var item = list[i]; + var orig_code, ast; + if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) { + dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3); + orig_code = fs.readFileSync('./static/' + item, 'utf8'); + ast = jsp.parse(orig_code); + ast = pro.ast_mangle(ast); + ast = pro.ast_squeeze(ast); + fs.writeFileSync('./static/' + dest, pro.gen_code(ast), 'utf8'); + winston.info('compressed ' + item + ' into ' + dest); + } + } +} + // Send the static documents into the preferred store, skipping expirations for (var name in config.documents) { var path = config.documents[name]; diff --git a/static/ZeroClipboard.min.js b/static/ZeroClipboard.min.js new file mode 100644 index 0000000..2aca3c6 --- /dev/null +++ b/static/ZeroClipboard.min.js @@ -0,0 +1 @@ +var ZeroClipboard={version:"1.0.7",clients:{},moviePath:"ZeroClipboard.swf",nextId:1,$:function(a){return typeof a=="string"&&(a=document.getElementById(a)),a.addClass||(a.hide=function(){this.style.display="none"},a.show=function(){this.style.display=""},a.addClass=function(a){this.removeClass(a),this.className+=" "+a},a.removeClass=function(a){var b=this.className.split(/\s+/),c=-1;for(var d=0;d-1&&(b.splice(c,1),this.className=b.join(" ")),this},a.hasClass=function(a){return!!this.className.match(new RegExp("\\s*"+a+"\\s*"))}),a},setMoviePath:function(a){this.moviePath=a},dispatch:function(a,b,c){var d=this.clients[a];d&&d.receiveEvent(b,c)},register:function(a,b){this.clients[a]=b},getDOMObjectPosition:function(a,b){var c={left:0,top:0,width:a.width?a.width:a.offsetWidth,height:a.height?a.height:a.offsetHeight};while(a&&a!=b)c.left+=a.offsetLeft,c.top+=a.offsetTop,a=a.offsetParent;return c},Client:function(a){this.handlers={},this.id=ZeroClipboard.nextId++,this.movieId="ZeroClipboardMovie_"+this.id,ZeroClipboard.register(this.id,this),a&&this.glue(a)}};ZeroClipboard.Client.prototype={id:0,ready:!1,movie:null,clipText:"",handCursorEnabled:!0,cssEffects:!0,handlers:null,glue:function(a,b,c){this.domElement=ZeroClipboard.$(a);var d=99;this.domElement.style.zIndex&&(d=parseInt(this.domElement.style.zIndex,10)+1),typeof b=="string"?b=ZeroClipboard.$(b):typeof b=="undefined"&&(b=document.getElementsByTagName("body")[0]);var e=ZeroClipboard.getDOMObjectPosition(this.domElement,b);this.div=document.createElement("div");var f=this.div.style;f.position="absolute",f.left=""+e.left+"px",f.top=""+e.top+"px",f.width=""+e.width+"px",f.height=""+e.height+"px",f.zIndex=d;if(typeof c=="object")for(addedStyle in c)f[addedStyle]=c[addedStyle];b.appendChild(this.div),this.div.innerHTML=this.getHTML(e.width,e.height)},getHTML:function(a,b){var c="",d="id="+this.id+"&width="+a+"&height="+b;if(navigator.userAgent.match(/MSIE/)){var e=location.href.match(/^https/i)?"https://":"http://";c+=''}else c+='';return c},hide:function(){this.div&&(this.div.style.left="-2000px")},show:function(){this.reposition()},destroy:function(){if(this.domElement&&this.div){this.hide(),this.div.innerHTML="";var a=document.getElementsByTagName("body")[0];try{a.removeChild(this.div)}catch(b){}this.domElement=null,this.div=null}},reposition:function(a){a&&(this.domElement=ZeroClipboard.$(a),this.domElement||this.hide());if(this.domElement&&this.div){var b=ZeroClipboard.getDOMObjectPosition(this.domElement),c=this.div.style;c.left=""+b.left+"px",c.top=""+b.top+"px"}},setText:function(a){this.clipText=a,this.ready&&this.movie.setText(a)},addEventListener:function(a,b){a=a.toString().toLowerCase().replace(/^on/,""),this.handlers[a]||(this.handlers[a]=[]),this.handlers[a].push(b)},setHandCursor:function(a){this.handCursorEnabled=a,this.ready&&this.movie.setHandCursor(a)},setCSSEffects:function(a){this.cssEffects=!!a},receiveEvent:function(a,b){a=a.toString().toLowerCase().replace(/^on/,"");switch(a){case"load":this.movie=document.getElementById(this.movieId);if(!this.movie){var c=this;setTimeout(function(){c.receiveEvent("load",null)},1);return}if(!this.ready&&navigator.userAgent.match(/Firefox/)&&navigator.userAgent.match(/Windows/)){var c=this;setTimeout(function(){c.receiveEvent("load",null)},100),this.ready=!0;return}this.ready=!0,this.movie.setText(this.clipText),this.movie.setHandCursor(this.handCursorEnabled);break;case"mouseover":this.domElement&&this.cssEffects&&(this.domElement.addClass("hover"),this.recoverActive&&this.domElement.addClass("active"));break;case"mouseout":this.domElement&&this.cssEffects&&(this.recoverActive=!1,this.domElement.hasClass("active")&&(this.domElement.removeClass("active"),this.recoverActive=!0),this.domElement.removeClass("hover"));break;case"mousedown":this.domElement&&this.cssEffects&&this.domElement.addClass("active");break;case"mouseup":this.domElement&&this.cssEffects&&(this.domElement.removeClass("active"),this.recoverActive=!1)}if(this.handlers[a])for(var d=0,e=this.handlers[a].length;d - - + +