1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Merge pull request #181 from seejohnrun/simplify_uglify

Upgrade uglify and simplify usage
This commit is contained in:
John Crepezzi 2017-10-31 21:20:23 -04:00 committed by GitHub
commit cfef588283
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 13 deletions

22
package-lock.json generated
View file

@ -438,6 +438,11 @@
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz",
"integrity": "sha1-MAvG4OhjdPe6YQaLWx7NV/xlMto=" "integrity": "sha1-MAvG4OhjdPe6YQaLWx7NV/xlMto="
}, },
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="
},
"split": { "split": {
"version": "0.3.3", "version": "0.3.3",
"resolved": "https://registry.npmjs.org/split/-/split-0.3.3.tgz", "resolved": "https://registry.npmjs.org/split/-/split-0.3.3.tgz",
@ -489,9 +494,20 @@
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU="
}, },
"uglify-js": { "uglify-js": {
"version": "1.3.3", "version": "3.1.6",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-1.3.3.tgz", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.1.6.tgz",
"integrity": "sha1-3dPpiqJ/X0flic+z+VzduiYJYZA=" "integrity": "sha512-/rseyxEKEVMBo8279lqpoJgD6C/i/CIi+9TJDvWmb+Xo6mqMKwjA8Io3IMHlcXQzj99feR6zrN8m3wqqvm/nYA==",
"requires": {
"commander": "2.11.0",
"source-map": "0.6.1"
},
"dependencies": {
"commander": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz",
"integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ=="
}
}
}, },
"unpipe": { "unpipe": {
"version": "1.0.0", "version": "1.0.0",

View file

@ -21,7 +21,7 @@
"winston": "0.6.2", "winston": "0.6.2",
"redis-url": "0.1.0", "redis-url": "0.1.0",
"redis": "0.8.1", "redis": "0.8.1",
"uglify-js": "1.3.3", "uglify-js": "3.1.6",
"busboy": "0.2.4", "busboy": "0.2.4",
"pg": "4.1.1" "pg": "4.1.1"
}, },

View file

@ -1,6 +1,7 @@
var http = require('http'); var http = require('http');
var fs = require('fs'); var fs = require('fs');
var uglify = require('uglify-js');
var winston = require('winston'); var winston = require('winston');
var connect = require('connect'); var connect = require('connect');
var route = require('connect-route'); var route = require('connect-route');
@ -54,19 +55,14 @@ else {
// Compress the static javascript assets // Compress the static javascript assets
if (config.recompressStaticAssets) { if (config.recompressStaticAssets) {
var jsp = require('uglify-js').parser;
var pro = require('uglify-js').uglify;
var list = fs.readdirSync('./static'); var list = fs.readdirSync('./static');
for (var j = 0; j < list.length; j++) { for (var j = 0; j < list.length; j++) {
var item = list[j]; var item = list[j];
var orig_code, ast;
if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) { if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) {
var dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3); var dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3);
orig_code = fs.readFileSync('./static/' + item, 'utf8'); var orig_code = fs.readFileSync('./static/' + item, 'utf8');
ast = jsp.parse(orig_code);
ast = pro.ast_mangle(ast); fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8');
ast = pro.ast_squeeze(ast);
fs.writeFileSync('./static/' + dest, pro.gen_code(ast), 'utf8');
winston.info('compressed ' + item + ' into ' + dest); winston.info('compressed ' + item + ' into ' + dest);
} }
} }

View file

@ -1 +1 @@
var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(e){return e.replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;")},haste_document.prototype.load=function(e,t,n){var r=this;$.ajax("/documents/"+e,{type:"get",dataType:"json",success:function(i){r.locked=!0,r.key=e,r.data=i.data;try{var s;n==="txt"?s={value:r.htmlEscape(i.data)}:n?s=hljs.highlight(n,i.data):s=hljs.highlightAuto(i.data)}catch(o){s=hljs.highlightAuto(i.data)}t({value:s.value,key:e,language:s.language||n,lineCount:i.data.split("\n").length})},error:function(){t(!1)}})},haste_document.prototype.save=function(e,t){if(this.locked)return!1;this.data=e;var n=this;$.ajax("/documents",{type:"post",data:e,dataType:"json",contentType:"application/json; charset=utf-8",success:function(r){n.locked=!0,n.key=r.key;var i=hljs.highlightAuto(e);t(null,{value:i.value,key:r.key,language:i.language,lineCount:e.split("\n").length})},error:function(e){try{t($.parseJSON(e.responseText))}catch(n){t({message:"Something went wrong!"})}}})};var haste=function(e,t){this.appName=e,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=t,this.configureShortcuts(),this.configureButtons(),t.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(e){var t=e?this.appName+" - "+e:this.appName;document.title=t},haste.prototype.showMessage=function(e,t){var n=$('<li class="'+(t||"info")+'">'+e+"</li>");$("#messages").prepend(n),setTimeout(function(){n.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","raw"])},haste.prototype.configureKey=function(e){var t,n=0;$("#box2 .function").each(function(){t=$(this);for(n=0;n<e.length;n++)if(t.hasClass(e[n]))return t.addClass("enabled"),!0;t.removeClass("enabled")})},haste.prototype.newDocument=function(e){this.$box.hide(),this.doc=new haste_document,e||window.history.pushState(null,this.appName,"/"),this.setTitle(),this.lightKey(),this.$textarea.val("").show("fast",function(){this.focus()}),this.removeLineNumbers()},haste.extensionMap={rb:"ruby",py:"python",pl:"perl",php:"php",scala:"scala",go:"go",xml:"xml",html:"xml",htm:"xml",css:"css",js:"javascript",vbs:"vbscript",lua:"lua",pas:"delphi",java:"java",cpp:"cpp",cc:"cpp",m:"objectivec",vala:"vala",sql:"sql",sm:"smalltalk",lisp:"lisp",ini:"ini",diff:"diff",bash:"bash",sh:"bash",tex:"tex",erl:"erlang",hs:"haskell",md:"markdown",txt:"",coffee:"coffee",json:"javascript",swift:"swift"},haste.prototype.lookupExtensionByType=function(e){for(var t in haste.extensionMap)if(haste.extensionMap[t]===e)return t;return e},haste.prototype.lookupTypeByExtension=function(e){return haste.extensionMap[e]||e},haste.prototype.addLineNumbers=function(e){var t="";for(var n=0;n<e;n++)t+=(n+1).toString()+"<br/>";$("#linenos").html(t)},haste.prototype.removeLineNumbers=function(){$("#linenos").html("&gt;")},haste.prototype.loadDocument=function(e){var t=e.split(".",2),n=this;n.doc=new haste_document,n.doc.load(t[0],function(e){e?(n.$code.html(e.value),n.setTitle(e.key),n.fullKey(),n.$textarea.val("").hide(),n.$box.show().focus(),n.addLineNumbers(e.lineCount)):n.newDocument()},this.lookupTypeByExtension(t[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var e=this.doc.data;this.newDocument(),this.$textarea.val(e)}},haste.prototype.lockDocument=function(){var e=this;this.doc.save(this.$textarea.val(),function(t,n){if(t)e.showMessage(t.message,"error");else if(n){e.$code.html(n.value),e.setTitle(n.key);var r="/"+n.key;n.language&&(r+="."+e.lookupExtensionByType(n.language)),window.history.pushState(null,e.appName+"-"+n.key,r),e.fullKey(),e.$textarea.val("").hide(),e.$box.show().focus(),e.addLineNumbers(n.lineCount)}})},haste.prototype.configureButtons=function(){var e=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(e){return e.ctrlKey&&e.keyCode===83},action:function(){e.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&e.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(e){return e.ctrlKey&&e.keyCode===78},shortcutDescription:"control + n",action:function(){e.newDocument(!e.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(t){return e.doc.locked&&t.ctrlKey&&t.keyCode===68},shortcutDescription:"control + d",action:function(){e.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(e){return e.ctrlKey&&e.shiftKey&&e.keyCode===82},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+e.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(t){return e.options.twitter&&e.doc.locked&&t.shiftKey&&t.ctrlKey&&t.keyCode==84},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var t=0;t<this.buttons.length;t++)this.configureButton(this.buttons[t])},haste.prototype.configureButton=function(e){e.$where.click(function(t){t.preventDefault(),!e.clickDisabled&&$(this).hasClass("enabled")&&e.action()}),e.$where.mouseenter(function(){$("#box3 .label").text(e.label),$("#box3 .shortcut").text(e.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),e.$where.mouseleave(function(){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var e=this;$(document.body).keydown(function(t){var n;for(var r=0;r<e.buttons.length;r++){n=e.buttons[r];if(n.shortcut&&n.shortcut(t)){t.preventDefault(),n.action();return}}})},$(function(){$("textarea").keydown(function(e){if(e.keyCode===9){e.preventDefault();var t=" ";if(document.selection){this.focus();var n=document.selection.createRange();n.text=t,this.focus()}else if(this.selectionStart||this.selectionStart=="0"){var r=this.selectionStart,i=this.selectionEnd,s=this.scrollTop;this.value=this.value.substring(0,r)+t+this.value.substring(i,this.value.length),this.focus(),this.selectionStart=r+t.length,this.selectionEnd=r+t.length,this.scrollTop=s}else this.value+=t,this.focus()}})}) var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(t){return t.replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;")},haste_document.prototype.load=function(t,e,o){var n=this;$.ajax("/documents/"+t,{type:"get",dataType:"json",success:function(s){n.locked=!0,n.key=t,n.data=s.data;try{var i;i="txt"===o?{value:n.htmlEscape(s.data)}:o?hljs.highlight(o,s.data):hljs.highlightAuto(s.data)}catch(t){i=hljs.highlightAuto(s.data)}e({value:i.value,key:t,language:i.language||o,lineCount:s.data.split("\n").length})},error:function(){e(!1)}})},haste_document.prototype.save=function(t,e){if(this.locked)return!1;this.data=t;var o=this;$.ajax("/documents",{type:"post",data:t,dataType:"json",contentType:"application/json; charset=utf-8",success:function(n){o.locked=!0,o.key=n.key;var s=hljs.highlightAuto(t);e(null,{value:s.value,key:n.key,language:s.language,lineCount:t.split("\n").length})},error:function(t){try{e($.parseJSON(t.responseText))}catch(t){e({message:"Something went wrong!"})}}})};var haste=function(t,e){this.appName=t,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=e,this.configureShortcuts(),this.configureButtons(),e.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(t){var e=t?this.appName+" - "+t:this.appName;document.title=e},haste.prototype.showMessage=function(t,e){var o=$('<li class="'+(e||"info")+'">'+t+"</li>");$("#messages").prepend(o),setTimeout(function(){o.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","raw"])},haste.prototype.configureKey=function(t){var e,o=0;$("#box2 .function").each(function(){for(e=$(this),o=0;o<t.length;o++)if(e.hasClass(t[o]))return e.addClass("enabled"),!0;e.removeClass("enabled")})},haste.prototype.newDocument=function(t){this.$box.hide(),this.doc=new haste_document,t||window.history.pushState(null,this.appName,"/"),this.setTitle(),this.lightKey(),this.$textarea.val("").show("fast",function(){this.focus()}),this.removeLineNumbers()},haste.extensionMap={rb:"ruby",py:"python",pl:"perl",php:"php",scala:"scala",go:"go",xml:"xml",html:"xml",htm:"xml",css:"css",js:"javascript",vbs:"vbscript",lua:"lua",pas:"delphi",java:"java",cpp:"cpp",cc:"cpp",m:"objectivec",vala:"vala",sql:"sql",sm:"smalltalk",lisp:"lisp",ini:"ini",diff:"diff",bash:"bash",sh:"bash",tex:"tex",erl:"erlang",hs:"haskell",md:"markdown",txt:"",coffee:"coffee",json:"javascript",swift:"swift"},haste.prototype.lookupExtensionByType=function(t){for(var e in haste.extensionMap)if(haste.extensionMap[e]===t)return e;return t},haste.prototype.lookupTypeByExtension=function(t){return haste.extensionMap[t]||t},haste.prototype.addLineNumbers=function(t){for(var e="",o=0;o<t;o++)e+=(o+1).toString()+"<br/>";$("#linenos").html(e)},haste.prototype.removeLineNumbers=function(){$("#linenos").html("&gt;")},haste.prototype.loadDocument=function(t){var e=t.split(".",2),o=this;o.doc=new haste_document,o.doc.load(e[0],function(t){t?(o.$code.html(t.value),o.setTitle(t.key),o.fullKey(),o.$textarea.val("").hide(),o.$box.show().focus(),o.addLineNumbers(t.lineCount)):o.newDocument()},this.lookupTypeByExtension(e[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var t=this.doc.data;this.newDocument(),this.$textarea.val(t)}},haste.prototype.lockDocument=function(){var t=this;this.doc.save(this.$textarea.val(),function(e,o){if(e)t.showMessage(e.message,"error");else if(o){t.$code.html(o.value),t.setTitle(o.key);var n="/"+o.key;o.language&&(n+="."+t.lookupExtensionByType(o.language)),window.history.pushState(null,t.appName+"-"+o.key,n),t.fullKey(),t.$textarea.val("").hide(),t.$box.show().focus(),t.addLineNumbers(o.lineCount)}})},haste.prototype.configureButtons=function(){var t=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(t){return t.ctrlKey&&83===t.keyCode},action:function(){""!==t.$textarea.val().replace(/^\s+|\s+$/g,"")&&t.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(t){return t.ctrlKey&&78===t.keyCode},shortcutDescription:"control + n",action:function(){t.newDocument(!t.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(e){return t.doc.locked&&e.ctrlKey&&68===e.keyCode},shortcutDescription:"control + d",action:function(){t.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(t){return t.ctrlKey&&t.shiftKey&&82===t.keyCode},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+t.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(e){return t.options.twitter&&t.doc.locked&&e.shiftKey&&e.ctrlKey&&84==e.keyCode},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var e=0;e<this.buttons.length;e++)this.configureButton(this.buttons[e])},haste.prototype.configureButton=function(t){t.$where.click(function(e){e.preventDefault(),!t.clickDisabled&&$(this).hasClass("enabled")&&t.action()}),t.$where.mouseenter(function(){$("#box3 .label").text(t.label),$("#box3 .shortcut").text(t.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),t.$where.mouseleave(function(){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var t=this;$(document.body).keydown(function(e){for(var o,n=0;n<t.buttons.length;n++)if((o=t.buttons[n]).shortcut&&o.shortcut(e))return e.preventDefault(),void o.action()})},$(function(){$("textarea").keydown(function(t){if(9===t.keyCode){t.preventDefault();if(document.selection)this.focus(),document.selection.createRange().text=" ",this.focus();else if(this.selectionStart||"0"==this.selectionStart){var e=this.selectionStart,o=this.selectionEnd,n=this.scrollTop;this.value=this.value.substring(0,e)+" "+this.value.substring(o,this.value.length),this.focus(),this.selectionStart=e+" ".length,this.selectionEnd=e+" ".length,this.scrollTop=n}else this.value+=" ",this.focus()}})});