aui-loader
Version:
用于webpack的aui组件解析
7 lines • 5.93 kB
JavaScript
/**
* aui-loader aui组件加载器
* Version: 0.3.15.1592559585021
* Author: nandy007
* License MIT @ https://github.com/nandy007/aui-loader
*/
const str="window.__AGILE_UI_NAME__ = 'agile-ui';\ndefine(['agile-ui'], function(aui) {\n\t\n\tvar _loader = {\n\t\tpatchPath: function(p){\n\t\t\tif(p.split('').pop()!=='/') return p + '/';\n\t\t\treturn p;\n\t\t},\n\t\tgetParent: function(p){\n\t\t\tif(p.indexOf('/')<0){\n\t\t\t\treturn './';\n\t\t\t}\n\t\t\tvar ps = p.split('/');\n\t\t\tps.pop();\n\t\t\tp = ps.join('/') + '/';\n\t\t\treturn p;\n\t\t},\n\t\trequest: function(url) {\n\t\t\tvar xmlhttp;\n\t\t\tif (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari\n\t\t\t\txmlhttp = new XMLHttpRequest();\n\t\t\t} else {// code for IE6, IE5\n\t\t\t\txmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\n\t\t\t}\n\t\t\txmlhttp.open(\"GET\", url, false);\n\t\t\txmlhttp.send();\n\t\t\treturn xmlhttp.status===200 ? xmlhttp.responseText : '' ;\n\t\t},\n\t\tload : function(name, parentRequire, onload, config) {\n\t\t\tvar _args = arguments;\n\t\t\tvar auiPath = parentRequire.toUrl(name);\n\t\t\tvar auiPaths = auiPath.split('?');\n\t\t\tif (auiPaths[0].split('.').pop() !== 'aui') {\n\t\t\t\tauiPaths[0] = auiPaths[0] + '.aui';\n\t\t\t\tauiPath = auiPaths.join('?');\n\t\t\t}\n\t\t\tvar $aui = document.createElement('div');\n\t\t\t$aui.innerHTML = _loader.request(auiPath);\n\t\t\tvar auiInfo = {};\n\t\t\tvar $auiChildren = $aui.children;\n\t\t\tfor (var i = 0,\n\t\t\t len = $auiChildren.length; i < len; i++) {\n\t\t\t\tvar $target = $auiChildren[i],\n\t\t\t\t tag = ($target.tagName || '').toLowerCase();\n\t\t\t\tif (tag === 'style') {\n\t\t\t\t\tauiInfo[tag] = {\n\t\t\t\t\t\ttype : $target.getAttribute('type'),\n\t\t\t\t\t\tcontent : $target.innerHTML\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tauiInfo[tag] = $target.innerHTML;\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\tvar templateStr = auiInfo.ui,\n\t\t\t moduleStr = auiInfo.script,\n\t\t\t $style = auiInfo.style || {};\n\n\t\t\tif (!moduleStr) {\n\t\t\t\treturn onload.error(new Error('模块[' + name + ']不符合auicomponent规范'));\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tvar func = new Function('module', 'exports', 'require', 'define', moduleStr),\n\t\t\t\t module = {\n\t\t\t\t\texports : {}\n\t\t\t\t};\n\n\t\t\t\tfunc(module, module.exports, require, function() {\n\n\t\t\t\t\tvar args = Array.prototype.slice.call(arguments, 0),\n\t\t\t\t\t cb = args.pop(),\n\t\t\t\t\t\tdeps = args.pop() || [];\n\t\t\t\t\tfunction getCreate() {\n\t\t\t\t\t\tvar anestor = cb.apply(cb, arguments);\n\t\t\t\t\t\tcreateComponent.call(_args, anestor, templateStr, $style, onload);\n\t\t\t\t\t}\n\t\t\t\t\tif(deps.length===0){\n\t\t\t\t\t\tgetCreate();\n\t\t\t\t\t}else{\n\t\t\t\t\t\trequire(deps, getCreate);\n\t\t\t\t\t}\n\n\t\t\t\t});\n\t\t\t\tvar anestor = module.exports;\n\t\t\t\tif (anestor && typeof anestor === 'function') {\n\t\t\t\t\tcreateComponent.call(_args, anestor, templateStr, $style, onload);\n\t\t\t\t}\n\t\t\t} catch(e) {\n\t\t\t\tconsole.log(name, moduleStr, e);\n\t\t\t\tonload.error(new Error('模块[' + name + ']加载失败'));\n\t\t\t}\n\n\t\t},\n\t\taddStyleHandler : function(k, func) {\n\t\t\tstyleHandlers[k] = func;\n\t\t}\n\t};\n\n\tvar base = document.getElementsByTagName('base');\n\tbase = base && base[0] && base[0] && base[0].href;\n\tvar pagePath = (base || window.location.href.split('#')[0].split('?')[0]).split('/');\n\tpagePath[pagePath.length - 1] = '';\n\tpagePath = _loader.patchPath(pagePath.join('/'));\n\t\n\tvar styleHandlers = {\n\t\t'text' : function(o, cb) {\n\t\t\tcb(o);\n\t\t},\n\t\t'less' : function(o, cb) {\n\t\t\tvar _this = this;\n\t\t\trequire(['less', 'lessc', 'normalize'], function(less, lessc, normalize) {\n\t\t\t\tvar fileUrl = _this[0], baseUrl = _this[3].baseUrl, parentPath = _loader.getParent(normalize.absoluteURI(fileUrl, baseUrl));\t\n\t\t\t\tvar curPath = normalize.absoluteURI(fileUrl+'.less', baseUrl);\n\t\t\t\tvar parser = new lessc.Parser({\n\t\t\t\t\tfilename: curPath\n\t\t\t\t});\n\t\t\t\tparser.parse(o, function(err, tree) {\n\t\t\t\t\tif (err)\n\t\t\t\t\t\treturn cb('');\n\t\t\t\t\tvar css = normalize(tree.toCSS(), _loader.getParent(normalize.absoluteURI(fileUrl, baseUrl)), pagePath);\n\t\t\t\t\tcb(css);\n\t\t\t\t}, window.less);\n\t\t\t});\n\t\t},\n\t\t'sass' : function(o, cb){\n\t\t\t//https://github.com/medialize/sass.js\n\t\t\tvar _this = this;\n\t\t\trequire(['sass', 'normalize'], function(Sass, normalize) {\n\t\t\t\tvar fileUrl = _this[0], baseUrl = _this[3].baseUrl, parentPath = _loader.getParent(normalize.absoluteURI(fileUrl, baseUrl));\n\t\t\t\tSass.importer(function(request, done) {\n\t\t\t\t\tvar importPath = normalize.absoluteURI(normalize.absoluteURI('in.scss', parentPath), pagePath);\n\t\t\t\t\tvar importContent = _loader.request(importPath);\n\t\t\t\t\tdone({path: importPath, content: importContent});\n\t\t\t\t});\n\t\t\t\tSass.compile(o, function(result) {\n\t\t\t\t\tif(result.status===0 && result.text){\n\t\t\t\t\t\tvar css = normalize(result.text, parentPath, pagePath);\n\t\t\t\t\t\tcb(css);\n\t\t\t\t\t}else{\n\t\t\t\t\t\tconsole.log(result);\n\t\t\t\t\t\tcb('');\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t});\n\t\t}\n\t};\n\n\tvar createComponent = function(anestor, templateStr, $style, cb) {\n\t\tvar Component = anestor;\n\t\tvar AuiComponent = aui.AuiComponent;\n\t\t//Component.style = $style.content;\n\t\tComponent.template = templateStr;\n\t\t//onload(new AuiComponent(Component));\n\n\t\tvar styleHandler = styleHandlers[$style.type] || styleHandlers['text'];\n\n\t\tstyleHandler.call(this, $style.content || '', function(content) {\n\t\t\tif(content) Component.style = content;\n\t\t\tif(!Component.tag) Component.tag = Component.name || '';\n\t\t\tAuiComponent.create(Component);\n\t\t\tcb(Component);\n\t\t});\n\t};\n\n\treturn window.auiloader = _loader;\n});";window.define&&window.define.amd&&new Function(str)();