-
Notifications
You must be signed in to change notification settings - Fork 17
/
lithe.min.js
7 lines (7 loc) · 7.81 KB
/
lithe.min.js
1
2
3
4
5
6
7
/**
lithe
@author []
@fileoverview a javascript common loader
@vserion 0.3.5
**/
!function(a,b){var c=!(typeof window===b||!a.navigator||!a.document);if(c){var d=a.document,e=Array.prototype,f=Object,g=f.prototype.toString,h=/(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/gm,i=/[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,j=function(a){return"[object String]"===g.call(a)},k=function(a){return"[object Function]"===g.call(a)},l=function(a){return"[object Array]"===g.call(a)},m=e.forEach?function(a,b){a.forEach(b)}:function(a,b){for(var c=0;c<a.length;c++)b(a[c],c,a)},n=e.filter?function(a,b){return a.filter(b)}:function(a,b){var c=[];return m(a,function(a,d,e){b(a,d,e)&&c.push(a)}),c},o=e.map?function(a,b){return a.map(b)}:function(a,b){var c=[];return m(a,function(a,d,e){c.push(b(a,d,e))}),c},p=f.keys?f.keys:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b},q=function(a){function b(a){c.push(a)}var c=[];for(var d in a)a.hasOwnProperty(d)&&(l(a[d])?m(a[d],b):b(a[d]));return c},r=(e.indexOf?function(a,b){return a.indexOf(b)}:function(a,b){for(var c=0;c<a.length;c++)if(a[c]===b)return c;return-1},function(a,b){return b=b||d,b?b.getElementsByTagName(a):b}),s=function(){},t=function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},u=function(a){var b={};return m(a,function(a){b[a]=1}),p(b)},v=function(a,b){return a.getAttribute(b)},w=function(a){var b=[];return a.replace(h,"").replace(i,function(a,c){b.push(c)}),u(b)},x=function(a,c){var d=a(c.require,c.exports,c);d!==b&&(c.exports=d)},y=function(){this.map={}};t(y.prototype,{trigger:function(a,b){var c=this.map[a];c&&m(c,function(a){a.apply(this,b)})},on:function(a,b){this.map[a]?this.map[a].push(b):this.map[a]=[b]}});var z=new y,A=function(a){for(var b=a.slice(a.lastIndexOf("?")+1).split("&"),c=0;c<b.length;c++){var d=b[c].split("="),e=d[0],f=d[1];if("timestamp"===e)return f}return null},B=function(a){return a.indexOf("://")>0||0===a.indexOf("//")},C=function(a){var b=a.match(/[^?]*(?=\/.*$)/);return(b?b[0]:".")+"/"},D=function(a){return a.slice(a.lastIndexOf("/")+1).replace(/\?.*$/,"")},E=function(a){return!D(a)},F=function(a){var b=/([^:\/])\/\/+/g;if(b.lastIndex=0,b.test(a)&&(a=a.replace(b,"$1/")),-1===a.indexOf("."))return a;for(var c,d=a.split("/"),e=[],f=0;f<d.length;f++)if(c=d[f],".."===c){if(0===e.length)throw new Error("The path is invalid: "+a);e.pop()}else"."!==c&&e.push(c);return e.join("/")},G=function(a,b){a=F(a);var c=a.charAt(a.length-1);return"/"===c?a:("#"===c?a=a.slice(0,-1):-1!==a.indexOf("?")||/\.(?:js|css)$/.test(a)||(a+=".js"),a.indexOf(":80/")>0&&(a=a.replace(":80/","/")),b&&(a=a.replace(/\?.+$/,"")),a)},H=function(a){var b,c,d,e,f,g={};for(b=0;b<jb.length&&!g[a];b++){e=jb[b];for(f in e)if(c=e[f],d=new RegExp("^"+f+"/"),d.test(a)&&!g[a]){a=a.replace(d,c),g[a]=!0;break}}return a},I=function(a){var b=gb.alias;if(b){var c=b[a];return c?c:H(a)}return a},J=function(a,b){if(b=xb&&xb.publicpath&&lb(a).isPublicDeps?xb.publicpath:C(b||xb.basepath),B(a))return a;ab&&(a=I(a));var c="";return 0===a.indexOf("./")||0===a.indexOf("../")?(0===a.indexOf("./")&&(a=a.substring(2)),c=b+a):c="/"===a.charAt(0)&&"/"!==a.charAt(1)?b+a.substring(1):b+"/"+a,G(c)},K=d.head||r("head")[0]||d.documentElement,L=r("script"),M=L[L.length-1],N=M.src||v(M,"src"),O=v(M,"data-path")||N,P=v(M,"data-config"),Q=v(M,"charset")||"utf8",R="true"===v(M,"data-debug"),S="true"===v(M,"data-config-dynamic"),T=A(M.src),U=null,V=v(M,"data-main"),W={},X={},Y={},Z=[],$=".js";O=O===N?C(N):J(O,C(N));var _=function(a,b){z.trigger("fetch",[a,b])};z.on("fetch",function(a,b){if(!/\.css$/.test(a)){if(Y[a])return b(),void 0;if(W[a])return X[a].push(b),void 0;W[a]=!0,X[a]=[b],cb(a,function(){Y[a]=!0,delete W[a];var b=X[a];b&&(delete X[a],m(b,function(a){a()}))},Q)}});var ab,bb={},cb=function(a,c,d){function e(){l(a)&&a.length?cb(a,c,d):k(c)&&c()}var f=l(a)?a.shift():a;if(bb[f])return e(),void 0;if(!f)throw new Error("getscript url is "+f);var g=db("script",d);g.onload=g.onerror=g.onreadystatechange=function(){/loaded|complete|undefined/.test(g.readyState)&&(g.onload=g.onerror=g.onreadystatechange=null,g.parentNode&&!R&&g.parentNode.removeChild(g),g=b,bb[f]=!0,e())},g.async="async";var h;h=S?U?U:(new Date).valueOf():U?U:T,f=h?f+"?timestamp="+h:f,g.src=f,eb(g)},db=function(a,b){var c=d.createElement(a);return c.charset=b?b:Q,c},eb=function(a){var b=r("base",K)[0];b?K.insertBefore(a,b):K.appendChild(a)},fb=[],gb={},hb={created:0,save:1,ready:2,compiling:3,compiled:4},ib=[],jb=[],kb=function(a,b,c){return-1!==a.indexOf(b+c)?{isPublicDeps:!0,dep:b}:{isPublicDeps:!1,dep:null}},lb=function(a){if(xb.config.publicdeps){var b,c=Object.keys(xb.config.publicdeps);a=xb.config.alias&&xb.config.alias[a]?xb.config.alias[a]:a;var d=o(c,function(a){return a.lastIndexOf($)<0?a+$:a});return/\.(?:js)$/.test(a)&&(b=kb(d,a,"")),/\.(?:js)$/.test(a)||(b=kb(d,a,$)),b}return{isPublicDeps:!1,dep:null}},mb=function(a){var b=a.id,c=n(a.dependencies,function(a){if(xb&&xb.config&&xb.config.publicdeps){var c=lb(a);if(c.isPublicDeps)return nb(c.dep),void 0}ib.push(b);var d=ob(xb.cache[J(a)]);return d&&ib.push(b),ib.pop(),!d}),d=qb(c);return d},nb=function(a){Z.push(a),Z=u(Z)},ob=function(a){if(!a||a.status!==hb.save)return!1;ib.push(a.id);var b=a.dependencies;if(b.length){if(pb(b,ib))return!0;for(var c=0;c<b.length;c++)if(ob(xb.cache[J(b[c])]))return!0}return ib.pop(),!1},pb=function(a,b){var c=a.concat(b);return c.length>u(c).length},qb=function(a){return j(a)&&(a=[a]),o(a,function(a){return J(a)})},rb=function(a,b){a=qb(a),z.trigger("start",[a]);var c=n(a,function(a){return a&&(!xb.cache[a]||xb.cache[a].status<hb.ready)}),d=c.length;if(0===d)return b(),void 0;var e=d,f=function(a){(a||{}).status<hb.ready&&(a.status=hb.ready),--e,0===e&&b()};m(c,function(a){function b(a){if(z.trigger("fetchsuccess",[c,a]),c.status>=hb.save){var b=mb(c);b.length?rb(b,function(){f(c)}):f(c)}else a?f(c):f()}var c=xb.get(a);c.status<hb.save?_(a,b):b()})},sb=function(){m(fb,function(a){var b=xb.get(a.id);b._save(a)}),fb=[]},tb=function(a,b){rb(a,function(){a=qb(a),ub(function(){var c=o(a,function(a){return a?xb.get(a)._compile():null});k(b)&&b.apply(null,c),z.trigger("end")})})},ub=function(a){if(xb.publicpath&&Z.length){var b=[],c=[];m(Z,function(a){q(xb.config.publicdeps[a]).forEach(function(a){-1!==p(xb.config.publicdeps).indexOf(a)&&Z.push(a)})}),m(u(Z),function(a){a.lastIndexOf($)<0?(b.push(a),b.push(a+$)):b.push(a)}),m(b,function(a){xb.config.publicdeps[a]&&c.push(p(xb.config.publicdeps[a])[0])});var d=u(c).join(","),e=xb.publicpath+"??"+d;m(Z,function(a){xb.get(qb(a)[0])}),cb(e,a)}else a()},vb=function(a){gb=a,jb=[];var b,c,d,e=gb.alias;if(e)for(b in e)c=e[b],E(c)&&(d={},d[b]=c,jb.push(d));ab=!0,gb.basepath&&(xb.basepath=gb.basepath),gb.publicpath&&(xb.publicpath=gb.publicpath),xb.config=gb,U=gb.timestamp},wb=function(a){this.id=a,this.status=0,this.dependencies=[],this.exports=null,this.parent=[],this.factory=s};t(wb.prototype,{_compile:function(){function a(a){a=G(J(a),!0);var c=xb.cache[a];return c?c.status===hb.compiling?c.exports:(c.parent=b,c._compile()):null}var b=this;if(b.status===hb.compiled)return b.exports;if(b.status<hb.save)return null;b.status=hb.compiling,a.cache=xb.cache,b.require=a,b.exports={};var c=b.factory;return k(c)&&x(c,b),b.status=hb.compiled,xb.events.trigger("compiled",[b]),b.exports},_save:function(a){this.status<hb.save&&(this.id=a.id,this.name=a.name,this.dependencies=a.deps,this.factory=a.factory,this.status=hb.save)}});var xb=t({basepath:O,events:z,cache:{},get:function(a){return a=G(a,!0),xb.cache[a]?xb.cache[a]:(xb.cache[a]=new wb(a),xb.cache[a])},define:function(a,b){var c=w(b.toString()),d={id:J(a),name:a,deps:c,factory:b};fb.push(d),sb()},use:function(a,b){!P||ab?tb(a,b):tb(P,function(c){vb(c),tb(a,b)})},load:cb,setConfig:vb});P&&(P=qb(P)),a.lithe=xb,a.define=xb.define,V&&setTimeout(function(){a.lithe.use(V)})}else exports.tool=require("./lib/lithe-tool.js"),exports.hfs=require("./lib/lithe-hfs.js")}(this);