diff --git a/bundler/cli.js b/bundler/cli.js index 93ad0adf..5533342d 100644 --- a/bundler/cli.js +++ b/bundler/cli.js @@ -1,11 +1,12 @@ "use strict" var fs = require("fs"); +var zlib = require("zlib") var bundle = require("./bundle") var minify = require("./minify") -var aliases = {o: "output", m: "minify", w: "watch", a: "aggressive", s: "save"} +var aliases = {o: "output", m: "minify", w: "watch", s: "save"} var params = {} var args = process.argv.slice(2), command = null for (var i = 0; i < args.length; i++) { @@ -24,26 +25,31 @@ function add(value) { command = null } +function format(n) { + return n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") +} + bundle(params.input, params.output, {watch: params.watch}) if (params.minify) { - minify(params.output, params.output, {watch: params.watch, advanced: params.aggressive}, function(stats) { - function format(n) { - return n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") - } + // mFiles = { original: String(mithril.js), compressed: String(mithril.min.js) } + var mFiles = minify(params.output, {watch: params.watch}) + var originalSize = mFiles.original.length + var compressedSize = mFiles.compressed.length + var originalGzipSize = zlib.gzipSync(mFiles.original).byteLength + var compressedGzipSize = zlib.gzipSync(mFiles.compressed).byteLength - console.log("Original size: " + format(stats.originalGzipSize) + " bytes gzipped (" + format(stats.originalSize) + " bytes uncompressed)") - console.log("Compiled size: " + format(stats.compressedGzipSize) + " bytes gzipped (" + format(stats.compressedSize) + " bytes uncompressed)") + console.log("Original size: " + format(originalGzipSize) + " bytes gzipped (" + format(originalSize) + " bytes uncompressed)") + console.log("Compiled size: " + format(compressedGzipSize) + " bytes gzipped (" + format(compressedSize) + " bytes uncompressed)") - if (params.save) { - var readme = fs.readFileSync("./README.md", "utf8") - var kb = stats.compressedGzipSize / 1000 + if (params.save) { + var readme = fs.readFileSync("./README.md", "utf8") + var kb = compressedGzipSize / 1000 - fs.writeFileSync("./README.md", - readme.replace( - /()(.+?)()/, - "$1" + (kb % 1 ? kb.toFixed(2) : kb) + " KB$3" - ) + fs.writeFileSync("./README.md", + readme.replace( + /()(.+?)()/, + "$1" + (kb % 1 ? kb.toFixed(2) : kb) + " KB$3" ) - } - }) -} + ) + } +} \ No newline at end of file diff --git a/bundler/minify.js b/bundler/minify.js index 3b1d3cae..987d378a 100644 --- a/bundler/minify.js +++ b/bundler/minify.js @@ -1,69 +1,26 @@ "use strict" -var http = require("https") -var querystring = require("querystring") var fs = require("fs") +var UglifyES = require("uglify-es") -module.exports = function(input, output, options, done) { - function minify(input, output) { - var code = fs.readFileSync(input, "utf8") +module.exports = function(filePath, options) { + function minify(filePath) { + var original = fs.readFileSync(filePath, "utf8"), + uglified = UglifyES.minify(original), + compressed = uglified.code + + if (uglified.error) throw new Error(uglified.error) - var data = { - output_format: "json", - output_info: ["compiled_code", "warnings", "errors", "statistics"], - compilation_level: options.advanced ? "ADVANCED_OPTIMIZATIONS" : "SIMPLE_OPTIMIZATIONS", - warning_level: "default", - output_file_name: "default.js", - js_code: code, - } - - var body = querystring.stringify(data) - - var response = "" - var req = http.request({ - method: "POST", - hostname: "closure-compiler.appspot.com", - path: "/compile", - headers: { - "Content-Type": "application/x-www-form-urlencoded;charset=utf-8", - "Content-Length": body.length - } - }, function(res) { - res.on("data", function(chunk) { - response += chunk.toString() - }) - - res.on("end", function() { - try { - var results = JSON.parse(response) - } catch(e) { - console.error(response); - - throw e; - } - - if (results.errors) { - for (var i = 0; i < results.errors.length; i++) console.log(results.errors[i]) - } - else { - fs.writeFileSync(output, results.compiledCode, "utf8") - - console.log("done") - - if(typeof done === "function") done(results.statistics) - } - }) - }) - - - req.write(body) - req.end() - console.log("minifying...") + fs.writeFileSync(filePath, compressed, "utf8") + return {original: original, compressed: compressed} } + function run() { - minify(input, output) + console.log("minifying...") + return minify(filePath) } - run() - if (options && options.watch) fs.watchFile(input, run) + if (options && options.watch) fs.watchFile(filePath, run) + + return run() } diff --git a/mithril.min.js b/mithril.min.js index 9c23112e..be1e4248 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,48 +1 @@ -(function(){function x(a,d,e,g,q,k){return{tag:a,key:d,attrs:e,children:g,text:q,dom:k,domSize:void 0,state:void 0,events:void 0,instance:void 0}}function Q(a){for(var d in a)if(G.call(a,d))return!1;return!0}function w(a){if(null==a||"string"!==typeof a&&"function"!==typeof a&&"function"!==typeof a.view)throw Error("The selector must be either a string or a component.");var d=arguments[1],e=2;if(null==d)d={};else if("object"!==typeof d||null!=d.tag||Array.isArray(d))d={},e=1;if(arguments.length=== -e+1){var g=arguments[e];Array.isArray(g)||(g=[g])}else for(g=[];ec.indexOf("?")?"?":"&";c+=e+d}return c}function h(c){try{return""!==c?JSON.parse(c):null}catch(y){throw Error("Invalid JSON: "+ -c);}}function m(c){return c.responseText}function n(c,a){if("function"===typeof c)if(Array.isArray(a))for(var d=0;dl.status||304===l.status||Z.test(c.url))d(n(c.type,a));else{var g=Error(l.responseText);g.code=l.status;g.response=a;e(g)}}catch(aa){e(aa)}};g&&null!=c.data?l.send(c.data):l.send()});return!0===c.background?y:F(y)},jsonp:function(c,m){var h=e();c=g(c,m);var y=new d(function(d,e){var g=c.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+l++,m=a.document.createElement("script"); -a[g]=function(e){m.parentNode.removeChild(m);d(n(c.type,e));delete a[g]};m.onerror=function(){m.parentNode.removeChild(m);e(Error("JSONP request failed"));delete a[g]};null==c.data&&(c.data={});c.url=q(c.url,c.data);c.data[c.callbackKey||"callback"]=g;m.src=k(c.url,c.data);a.document.documentElement.appendChild(m)});return!0===c.background?y:h(y)},setCompletionCallback:function(c){C=c}}}(window,p),V=function(a){function d(t,b){if(t.state!==b)throw Error("`vnode.state` must not be modified");}function e(t){var b= -t.state;try{return this.apply(b,arguments)}finally{d(t,b)}}function g(t,b,f,c,a,d,e){for(;f'+ -b.children+"",a=a.firstChild):a.innerHTML=b.children;b.dom=a.firstChild;b.domSize=a.childNodes.length;for(b=E.createDocumentFragment();f=a.firstChild;)b.appendChild(f);C(t,b,c)}function h(t,b,f,c,a,d){if(b!==f&&(null!=b||null!=f))if(null==b||0===b.length)g(t,f,0,f.length,c,a,d);else if(null==f||0===f.length)y(b,0,b.length);else{for(var e=0,u=0,h=null,k=null;u=u&&h>=e;)if(A=b[k],z=f[h],null==A)k--;else if(null==z)h--;else if(A.key===z.key)A!==z&&m(t,A,z,c,a,d),null!=z.dom&&(a=z.dom),k--,h--;else break;for(;k>=u&&h>=e;)if(r=b[u],v=f[e],null==r)u++;else if(null==v)e++;else if(r.key===v.key)u++,e++,r!==v&&m(t,r,v,c,l(b,u,a),d);else break;for(;k>=u&&h>=e;){if(null==r)u++;else if(null==v)e++;else if(null==A)k--;else if(null==z)h--;else if(e===h)break;else{if(r.key!== -z.key||A.key!==v.key)break;B=l(b,u,a);C(t,n(A),B);A!==v&&m(t,A,v,c,B,d);++e<=--h&&C(t,n(r),a);r!==z&&m(t,r,z,c,a,d);null!=z.dom&&(a=z.dom);u++;k--}A=b[k];z=f[h];r=b[u];v=f[e]}for(;k>=u&&h>=e;){if(null==A)k--;else if(null==z)h--;else if(A.key===z.key)A!==z&&m(t,A,z,c,a,d),null!=z.dom&&(a=z.dom),k--,h--;else break;A=b[k];z=f[h]}if(e>h)y(b,u,k+1);else if(u>k)g(t,f,e,h+1,c,a,d);else{v=a;A=h-e+1;r=Array(A);var x=2147483647,w=0;for(B=0;B=e;B--){if(null==p){p=b;A=u;z=k+1;for(var D= -Object.create(null);A=e;B--)v=f[B],-1===r[B-e]?q(t,v,c,d,a):b[u]===B-e?u--:C(t,n(v),a),null!=v.dom&&(a=f[B].dom)}else for(B=h;B>=e;B--)v=f[B],-1===r[B-e]&&q(t,v,c,d,a),null!=v.dom&&(a=f[B].dom)}}else{h=b.lengthh&&y(b,e,b.length);f.length>h&&g(t,f,e,f.length,c,a,d)}}}function m(a,b,f,d,g,l){var t=b.tag; -if(t===f.tag){f.state=b.state;f.events=b.events;var u;var y;null!=f.attrs&&"function"===typeof f.attrs.onbeforeupdate&&(u=e.call(f.attrs.onbeforeupdate,f,b));"string"!==typeof f.tag&&"function"===typeof f.state.onbeforeupdate&&(y=e.call(f.state.onbeforeupdate,f,b));void 0===u&&void 0===y||u||y?u=!1:(f.dom=b.dom,f.domSize=b.domSize,f.instance=b.instance,u=!0);if(!u)if("string"===typeof t)switch(null!=f.attrs&&L(f.attrs,f,d),t){case "#":b.children.toString()!==f.children.toString()&&(b.dom.nodeValue= -f.children);f.dom=b.dom;break;case "<":b.children!==f.children?(n(b),k(a,f,l,g)):(f.dom=b.dom,f.domSize=b.domSize);break;case "[":h(a,b.children,f.children,d,g,l);b=0;d=f.children;f.dom=null;if(null!=d){for(var p=0;p0&&(l.className=i.join(" ")),n[e]={tag:o,attrs:l}}(i),a,e.normalizeChildren(l)):e(i,a.key,a,l)}i.trust=function(t){return null==t&&(t=""),e("<",void 0,void 0,t,void 0,void 0)},i.fragment=function(t,n){return e("[",t.key,t,e.normalizeChildren(n),void 0,void 0)};var l=i;if((a=function(e){if(!(this instanceof a))throw new Error("Promise must be called with `new`");if("function"!=typeof e)throw new TypeError("executor must be a function");var t=this,n=[],r=[],o=f(n,!0),i=f(r,!1),l=t._instance={resolvers:n,rejectors:r},u="function"==typeof setImmediate?setImmediate:setTimeout;function f(e,o){return function a(f){var c;try{if(!o||null==f||"object"!=typeof f&&"function"!=typeof f||"function"!=typeof(c=f.then))u(function(){o||0!==e.length||console.error("Possible unhandled promise rejection:",f);for(var t=0;t0||e(n)}}var r=n(i);try{e(n(o),r)}catch(e){r(e)}}s(e)}).prototype.then=function(e,t){var n,r,o=this._instance;function i(e,t,i,l){t.push(function(t){if("function"!=typeof e)i(t);else try{n(e(t))}catch(e){r&&r(e)}}),"function"==typeof o.retry&&l===o.state&&o.retry()}var l=new a(function(e,t){n=e,r=t});return i(e,o.resolvers,n,!0),i(t,o.rejectors,r,!1),l},a.prototype.catch=function(e){return this.then(null,e)},a.prototype.finally=function(e){return this.then(function(t){return a.resolve(e()).then(function(){return t})},function(t){return a.resolve(e()).then(function(){return a.reject(t)})})},a.resolve=function(e){return e instanceof a?e:new a(function(t){t(e)})},a.reject=function(e){return new a(function(t,n){n(e)})},a.all=function(e){return new a(function(t,n){var r=e.length,o=0,i=[];if(0===e.length)t([]);else for(var l=0;l=200&&i.status<300||304===i.status||f.test(n.url))t(d(n.type,e));else{var o=new Error(i.responseText);o.code=i.status,o.response=e,r(o)}}catch(e){r(e)}},o&&null!=n.data?i.send(n.data):i.send()});return!0===n.background?v:u(v)},jsonp:function(n,u){var f=o();n=i(n,u);var s=new t(function(t,o){var i=n.callbackName||"_mithril_"+Math.round(1e16*Math.random())+"_"+r++,u=e.document.createElement("script");e[i]=function(r){u.parentNode.removeChild(u),t(d(n.type,r)),delete e[i]},u.onerror=function(){u.parentNode.removeChild(u),o(new Error("JSONP request failed")),delete e[i]},null==n.data&&(n.data={}),n.url=l(n.url,n.data),n.data[n.callbackKey||"callback"]=i,u.src=a(n.url,n.data),e.document.documentElement.appendChild(u)});return!0===n.background?s:f(s)},setCompletionCallback:function(e){n=e}}}(window,a),c=function(t){var n,r=t.document,o={svg:"http://www.w3.org/2000/svg",math:"http://www.w3.org/1998/Math/MathML"};function i(e){return e.attrs&&e.attrs.xmlns||o[e.tag]}function l(e,t){if(e.state!==t)throw new Error("`vnode.state` must not be modified")}function a(e){var t=e.state;try{return this.apply(t,arguments)}finally{l(e,t)}}function u(e,t,n,r,o,i,l){for(var a=n;a'+t.children+"",l=l.firstChild):l.innerHTML=t.children,t.dom=l.firstChild,t.domSize=l.childNodes.length;for(var a,u=r.createDocumentFragment();a=l.firstChild;)u.appendChild(a);y(e,u,o)}function d(e,t,n,r,o,i){if(t!==n&&(null!=t||null!=n))if(null==t||0===t.length)u(e,n,0,n.length,r,o,i);else if(null==n||0===n.length)w(t,0,t.length);else{for(var l=0,a=0,s=null,c=null;a=a&&S>=l;)if(x=t[E],k=n[S],null==x)E--;else if(null==k)S--;else{if(x.key!==k.key)break;x!==k&&v(e,x,k,r,o,i),null!=k.dom&&(o=k.dom),E--,S--}for(;E>=a&&S>=l;)if(d=t[a],g=n[l],null==d)a++;else if(null==g)l++;else{if(d.key!==g.key)break;a++,l++,d!==g&&v(e,d,g,r,m(t,a,o),i)}for(;E>=a&&S>=l;){if(null==d)a++;else if(null==g)l++;else if(null==x)E--;else if(null==k)S--;else{if(l===S)break;if(d.key!==k.key||x.key!==g.key)break;C=m(t,a,o),y(e,p(x),C),x!==g&&v(e,x,g,r,C,i),++l<=--S&&y(e,p(d),o),d!==k&&v(e,d,k,r,o,i),null!=k.dom&&(o=k.dom),a++,E--}x=t[E],k=n[S],d=t[a],g=n[l]}for(;E>=a&&S>=l;){if(null==x)E--;else if(null==k)S--;else{if(x.key!==k.key)break;x!==k&&v(e,x,k,r,o,i),null!=k.dom&&(o=k.dom),E--,S--}x=t[E],k=n[S]}if(l>S)w(t,a,E+1);else if(a>E)u(e,n,l,S+1,r,o,i);else{var A,z,j=o,N=S-l+1,T=new Array(N),O=0,P=0,$=2147483647,I=0;for(P=0;P=l;P--)if(null==A&&(A=h(t,a,E+1)),null!=(k=n[P])){var R=A[k.key];null!=R&&($=R<$?R:-1,T[P-l]=R,x=t[R],t[R]=null,x!==k&&v(e,x,k,r,o,i),null!=k.dom&&(o=k.dom),I++)}if(o=j,I!==E-a+1&&w(t,a,E+1),0===I)u(e,n,l,S+1,r,o,i);else if(-1===$)for(O=(z=function(e){var t,n,r=e.slice(),o=[];o.push(0);for(var i=0,l=e.length;i0&&(r[i]=o[t-1]),o[t]=i)}}t=o.length,n=o[t-1];for(;t-- >0;)o[t]=n,n=r[n];return o}(T)).length-1,P=S;P>=l;P--)g=n[P],-1===T[P-l]?f(e,g,r,i,o):z[O]===P-l?O--:y(e,p(g),o),null!=g.dom&&(o=n[P].dom);else for(P=S;P>=l;P--)g=n[P],-1===T[P-l]&&f(e,g,r,i,o),null!=g.dom&&(o=n[P].dom)}}else{var L=t.lengthL&&w(t,l,t.length),n.length>L&&u(e,n,l,n.length,r,o,i)}}}function v(t,n,r,o,l,u){var s=n.tag;if(s===r.tag){if(r.state=n.state,r.events=n.events,function(e,t){var n,r;null!=e.attrs&&"function"==typeof e.attrs.onbeforeupdate&&(n=a.call(e.attrs.onbeforeupdate,e,t));"string"!=typeof e.tag&&"function"==typeof e.state.onbeforeupdate&&(r=a.call(e.state.onbeforeupdate,e,t));if(!(void 0===n&&void 0===r||n||r))return e.dom=t.dom,e.domSize=t.domSize,e.instance=t.instance,!0;return!1}(r,n))return;if("string"==typeof s)switch(null!=r.attrs&&N(r.attrs,r,o),s){case"#":!function(e,t){e.children.toString()!==t.children.toString()&&(e.dom.nodeValue=t.children);t.dom=e.dom}(n,r);break;case"<":!function(e,t,n,r,o){t.children!==n.children?(p(t),c(e,n,r,o)):(n.dom=t.dom,n.domSize=t.domSize)}(t,n,r,u,l);break;case"[":!function(e,t,n,r,o,i){d(e,t.children,n.children,r,o,i);var l=0,a=n.children;if(n.dom=null,null!=a){for(var u=0;u0){for(var o=e.dom;--t;)n.appendChild(o.nextSibling);n.insertBefore(o,n.firstChild)}return n}return e.dom}function m(e,t,n){for(;t-1||null!=e.attrs&&e.attrs.is||"href"!==t&&"list"!==t&&"form"!==t&&"width"!==t&&"height"!==t)&&t in e.dom}function S(e,t,n){if(null==t||null==n||"object"!=typeof t||"object"!=typeof n||n===t)if(t===n&&(e.style.cssText="",t=null),null==n)e.style.cssText="";else if("string"==typeof n)e.style.cssText=n;else for(var r in"string"==typeof t&&(e.style.cssText=""),n)e.style[r]=n[r];else{for(var r in n)n[r]!==t[r]&&(e.style[r]=n[r]);for(var r in t)r in n||(e.style[r]="")}}function A(){}function z(e,t,n){if(null!=e.events){if(e.events[t]===n)return;null==n||"function"!=typeof n&&"object"!=typeof n?(null!=e.events[t]&&e.dom.removeEventListener(t.slice(2),e.events,!1),e.events[t]=void 0):(null==e.events[t]&&e.dom.addEventListener(t.slice(2),e.events,!1),e.events[t]=n)}else null==n||"function"!=typeof n&&"object"!=typeof n||(e.events=new A,e.dom.addEventListener(t.slice(2),e.events,!1),e.events[t]=n)}function j(e,t,n){"function"==typeof e.oninit&&a.call(e.oninit,t),"function"==typeof e.oncreate&&n.push(a.bind(e.oncreate,t))}function N(e,t,n){"function"==typeof e.onupdate&&n.push(a.bind(e.onupdate,t))}return A.prototype=Object.create(null),A.prototype.handleEvent=function(e){var t,r=this["on"+e.type];"function"==typeof r?t=r.call(e.target,e):"function"==typeof r.handleEvent&&r.handleEvent(e),"function"==typeof n&&n.call(e.target,e),!1===t&&(e.preventDefault(),e.stopPropagation())},{render:function(t,n){if(!t)throw new Error("Ensure the DOM element being passed to m.route/m.mount/m.render is not undefined.");var o=[],i=r.activeElement,l=t.namespaceURI;null==t.vnodes&&(t.textContent=""),n=e.normalizeChildren(Array.isArray(n)?n:[n]),d(t,t.vnodes,n,o,null,"http://www.w3.org/1999/xhtml"===l?void 0:l),t.vnodes=n,null!=i&&r.activeElement!==i&&"function"==typeof i.focus&&i.focus();for(var a=0;a-1&&r.splice(t,2)}function l(){if(o)throw new Error("Nested m.redraw.sync() call");o=!0;for(var e=1;e-1&&u.pop();for(var s=0;s-1?r:o>-1?o:e.length;if(r>-1){var l=o>-1?o:e.length,a=p(e.slice(r+1,l));for(var u in a)t[u]=a[u]}if(o>-1){var f=p(e.slice(o+1));for(var u in f)n[u]=f[u]}return e.slice(0,i)}var l={prefix:"#!",getPath:function(){switch(l.prefix.charAt(0)){case"#":return o("hash").slice(l.prefix.length);case"?":return o("search").slice(l.prefix.length)+o("hash");default:return o("pathname").slice(l.prefix.length)+o("search")+o("hash")}},setPath:function(t,r,o){var a={},f={};if(t=i(t,a,f),null!=r){for(var s in r)a[s]=r[s];t=t.replace(/:([^\/]+)/g,function(e,t){return delete a[t],r[t]})}var c=u(a);c&&(t+="?"+c);var d=u(f);if(d&&(t+="#"+d),n){var v=o?o.state:null,h=o?o.title:null;e.onpopstate(),o&&o.replace?e.history.replaceState(v,h,l.prefix+t):e.history.pushState(v,h,l.prefix+t)}else e.location.href=l.prefix+t}};return l.defineRoutes=function(o,a,u){function f(){var t=l.getPath(),n={},r=i(t,n,n),f=e.history.state;if(null!=f)for(var s in f)n[s]=f[s];for(var c in o){var d=new RegExp("^"+c.replace(/:[^\/]+?\.{3}/g,"(.*?)").replace(/:[^\/]+/g,"([^\\/]+)")+"/?$");if(d.test(r))return void r.replace(d,function(){for(var e=c.match(/:[^\/]+/g)||[],r=[].slice.call(arguments,1,-2),i=0;i