!function(){ var path = document.getElementsByTagName("script"); path = path[path.length-1].src.split("?path=")[1]; var requireOption = window.requireOption || { path : path, }; var windowIsLoad = false; var key = setInterval(function(){ if( document.body || windowIsLoad ){ windowIsLoad = true; clearInterval(key); this.onlad = null; key = null; requireFunctionRun(); } },500) var requireFileType = ['js','css','html','json']; //進行處理的文件後綴 var defaultRequireType = 0; //默認下標第一個 var requireFileFunction = { '.js':function(responseText,name){ if( responseText.indexOf("/*lwz-define-js*/") > -1 || responseText.indexOf('define') < 0 ){ eval( "Lwz.define([],'文件不符合AMD格式,做爲js獨立運行中')"); responseText = "/*" + defineName + "*/\n" + responseText; eval( responseText); }else{ responseText = responseText.replace("define",'Lwz.define') responseText = "/*" + defineName + "*/\n" + responseText; eval( responseText ); } responseText = null; }, ".css" : function(responseText,name){ var css = document.createElement("style"); css.innerHTML = responseText; css.setAttribute('name',name); document.head.appendChild(css); responseText = null; eval( "Lwz.define([],'css文件不具有返回值功能')"); }, '.html' : function(responseText,name){ eval( "Lwz.define([],'"+responseText.replace(/\'/g,'\\\'').replace(/\r|\n/g, "")+"')"); responseText = null; }, '.json' : function(responseText,name){ eval( "Lwz.define([],function(){ return "+responseText.replace(/\'/g,'\\\'').replace(/\r|\n/g, "")+";})"); responseText = null; }, } //保存全部的已經加載完成的require方法 等待頁面網頁加載完成執行 var requireFunction = []; var requireFunctionRun = function(){ for(;requireFunction.length > 0;){ var f = requireFunction.shift(); f[0].apply(window,f[1]); } } var defineName = ''; var responseText = ''; var zz = new RegExp("\.("+ requireFileType.join('|') +")$"); var isScript = new RegExp("\^(http:|https:).*?$"); var _require = function(name,callback){ callback || (callback = function(){ return arguments.length == 1 ? arguments[0] : Array.prototype.slice.apply(arguments); }); var path = requireOption.path, argu = [], key = false, it, length = name.length; name.forEach(function(e,i,ar){ it = zz.exec(e); if( it == null ){ ar[i] += "." + requireFileType[defaultRequireType]; } }) if( name.length > 0 ){ for( var i = 0 ; i < name.length ; i++ ){ if( typeof defineData.get(name[i]) === 'undefined' ){ if( !defineData.time[name[i]] ) defineData.time[name[i]] = {}; defineData.time[name[i]].start = new Date(); if( isScript.test(name) ){ var script = document.createElement("script"); script.src = name; script.onload = function(){ eval( "Lwz.define([],'script文件不具有返回值功能會直接運行')"); }; script.onerror = function(e){ eval( "Lwz.define([],'加載失敗')"); console.log(name+"文件加載失敗") console.log("失敗緣由:"+e.currentTarget.responseText); }; }else{ _require.ajax({ type: 'get', url : requireOption.path + name[i], success: function(name,e){ // try{ defineName = name.toString(); responseText = e.currentTarget.responseText; if( responseText ){ it = zz.exec(defineName); if( it == null ) console.log( defineName + "文件的後綴名沒法識別"); it = requireFileFunction[it[0]]; if( it == null ) console.log( defineName + "文件的處理邏輯沒法識別"); it.call(this,responseText,defineName); } else eval( "Lwz.define([],null)"); // }catch(e){ // console.log(name+"文件執行錯誤") // console.log("錯誤緣由:"+e.stack) // responseText = null; // } }.bind(this,name[i]), error: function(name,e){ defineName = name.toString() //.split('/').pop() eval( "Lwz.define([],'加載失敗')"); console.log(name+"文件加載失敗") console.log("失敗緣由:"+e.currentTarget.responseText); }.bind(this,name[i]), }); } defineData.setLoading(name[i]); } defineData.get(name[i],function(data,n){ argu[name.indexOf(n)] = data; if( --length == 0 ){ windowIsLoad ? callback.apply(window,argu) : requireFunction.push([callback,argu]) } }) } } else{ callback.apply(this,argu) } ; } var getXML = function(){ if( XMLHttpRequest ) return new XMLHttpRequest(); else return new ActiveXObject("Microsoft.XMLHTTP"); } _require.XMLHttp = getXML(); _require.option = requireOption; _require.ajax = function(option = {}){ var url = option.url; if( !url ) return; var mothed = option.type || "get"; var data = ""; for( var i in option.data ) data += i + "=" + option.data[i] + "&"; var success = option.success; var error = option.error; var complete = option.complete; var ajax = getXML(); //判斷是不是post請求 if( mothed === "post" ){ //若是是則添加頭部文件 ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded"); }else{ url += "?" + data; data = undefined; } //創建請求鏈接 ajax.open(mothed,url+"?"+Math.random()); //發送請求 ajax.send(data); //請求回調 ajax.onreadystatechange = function(e){ if( this.readyState == 4 ){ if( this.readyState == 4 && this.status == 200 ) success.apply(this,arguments); else if( this.readyState == 4 && this.status.toString()[0] != "2" ) error.apply(this,arguments); complete && complete.apply(this,arguments); } } } var define = function(){ var argLen = arguments.length, name = arguments[0], depend = arguments[1], object1 = arguments[2], notArgu = [], argu = []; try{ //判斷是否給模塊命名 if( typeof name !== 'string' ){ object1 = depend; depend = name; name = defineName; } //若是存在依賴模塊的話 if( typeof depend === 'object' && typeof depend.length == 'number' && depend.constructor === Array) { depend.forEach(function(e,i,ar){ it = zz.exec(e); if( it == null ){ ar[i] += "." + requireFileType[defaultRequireType]; } }) //先判斷依賴模塊是否已經被加載了 for( var i = 0; i < depend.length ; i ++ ){ if(typeof defineData.get(depend[i]) === 'undefined' || defineData.get(depend[i]) === null ){ //若是存在沒有被加載的模塊 notArgu.push(depend[i]); }else{ argu[i] || ( argu[i] = defineData.get(depend[i]) ); } } if( notArgu.length > 0 ){ //進入加載模塊 //並在加載成功後從新運行函數 _require(notArgu,define.bind(this,name,depend||[],object1)); return ; } }else { object1 = depend; depend = undefined; } }catch(e){ console.error(name+"文件加載錯誤;") console.error(e); } //不存在未被加載的依賴模塊 if( typeof object1 === 'function' ){ defineData.set(name,object1.apply(this,argu)); }else{ defineData.set(name,object1); } } var defineData = { time : {}, data : {}, get : function( name , callback ){ it = zz.exec(name); it == null ? name += "." + requireFileType[defaultRequireType] : null; var data = this.data[name]; if( !callback ) return data; if( data ){ callback.call(this,data,name); return data; }else{ this.waitLoad[name] || (this.waitLoad[name] = []); this.waitLoad[name].push(callback); } }, set : function( name , data ){ this.data[name] = data; console.log(name+'註冊成功。') this.time[name].end = new Date(); this.time[name].time = this.time[name].start - this.time[name].end; var waitFunction = this.waitLoad[name]; for( ;; ){ if( waitFunction.length < 1 ) break; waitFunction.shift()(data,name); } }, setLoading : function(name){ this.data[name] = null; }, waitLoad : {} }; window.Lwz = { data : defineData, require : _require, define : define }; }()