123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /**
- * @file: mod.js
- * @author fis
- * ver: 1.0.11
- * update: 2015/05/14
- * https://github.com/fex-team/mod
- */
- var require;
- /* eslint-disable no-unused-vars */
- var define;
- (function (global) {
- // 避免重复加载而导致已定义模块丢失
- if (require) {
- return;
- }
- var head = document.getElementsByTagName('head')[0];
- var loadingMap = {};
- var factoryMap = {};
- var modulesMap = {};
- var scriptsMap = {};
- var resMap = {};
- var pkgMap = {};
- var createScript = function (url, onerror) {
- if (url in scriptsMap) {
- return;
- }
- scriptsMap[url] = true;
- var script = document.createElement('script');
- if (onerror) {
- var tid = setTimeout(onerror, require.timeout);
- script.onerror = function () {
- clearTimeout(tid);
- onerror();
- };
- var onload = function () {
- clearTimeout(tid);
- };
- if ('onload' in script) {
- script.onload = onload;
- }
- else {
- script.onreadystatechange = function () {
- if (this.readyState === 'loaded' || this.readyState === 'complete') {
- onload();
- }
- };
- }
- }
- script.type = 'text/javascript';
- script.src = url;
- head.appendChild(script);
- return script;
- };
- var loadScript = function (id, callback, onerror) {
- var queue = loadingMap[id] || (loadingMap[id] = []);
- queue.push(callback);
- //
- // resource map query
- //
- var res = resMap[id] || resMap[id + '.js'] || {};
- var pkg = res.pkg;
- var url;
- if (pkg) {
- url = pkgMap[pkg].url;
- }
- else {
- url = res.url || id;
- }
- createScript(url, onerror && function () {
- onerror(id);
- });
- };
- define = function (id, factory) {
- id = id.replace(/\.js$/i, '');
- factoryMap[id] = factory;
- var queue = loadingMap[id];
- if (queue) {
- for (var i = 0, n = queue.length; i < n; i++) {
- queue[i]();
- }
- delete loadingMap[id];
- }
- };
- require = function (id) {
- // compatible with require([dep, dep2...]) syntax.
- if (id && id.splice) {
- return require.async.apply(this, arguments);
- }
- id = require.alias(id);
- var mod = modulesMap[id];
- if (mod) {
- return mod.exports;
- }
- //
- // init module
- //
- var factory = factoryMap[id];
- if (!factory) {
- throw '[ModJS] Cannot find module `' + id + '`';
- }
- mod = modulesMap[id] = {
- exports: {}
- };
- //
- // factory: function OR value
- //
- var ret = (typeof factory === 'function') ? factory.apply(mod, [require, mod.exports, mod]) : factory;
- if (ret) {
- mod.exports = ret;
- }
- if (mod.exports && !mod.exports['default']) {
- mod.exports['default'] = mod.exports;
- }
- return mod.exports;
- };
- require.async = function (names, onload, onerror) {
- if (typeof names === 'string') {
- names = [names];
- }
- var needMap = {};
- var needNum = 0;
- function findNeed(depArr) {
- var child;
- for (var i = 0, n = depArr.length; i < n; i++) {
- //
- // skip loading or loaded
- //
- var dep = require.alias(depArr[i]);
- if (dep in factoryMap) {
- // check whether loaded resource's deps is loaded or not
- child = resMap[dep] || resMap[dep + '.js'];
- if (child && 'deps' in child) {
- findNeed(child.deps);
- }
- continue;
- }
- if (dep in needMap) {
- continue;
- }
- needMap[dep] = true;
- needNum++;
- loadScript(dep, updateNeed, onerror);
- child = resMap[dep] || resMap[dep + '.js'];
- if (child && 'deps' in child) {
- findNeed(child.deps);
- }
- }
- }
- function updateNeed() {
- if (0 === needNum--) {
- var args = [];
- for (var i = 0, n = names.length; i < n; i++) {
- args[i] = require(names[i]);
- }
- onload && onload.apply(global, args);
- }
- }
- findNeed(names);
- updateNeed();
- };
- require.resourceMap = function (obj) {
- var k;
- var col;
- // merge `res` & `pkg` fields
- col = obj.res;
- for (k in col) {
- if (col.hasOwnProperty(k)) {
- resMap[k] = col[k];
- }
- }
- col = obj.pkg;
- for (k in col) {
- if (col.hasOwnProperty(k)) {
- pkgMap[k] = col[k];
- }
- }
- };
- require.loadJs = function (url) {
- createScript(url);
- };
- require.loadCss = function (cfg) {
- if (cfg.content) {
- var sty = document.createElement('style');
- sty.type = 'text/css';
- if (sty.styleSheet) { // IE
- sty.styleSheet.cssText = cfg.content;
- }
- else {
- sty.innerHTML = cfg.content;
- }
- head.appendChild(sty);
- }
- else if (cfg.url) {
- var link = document.createElement('link');
- link.href = cfg.url;
- link.rel = 'stylesheet';
- link.type = 'text/css';
- head.appendChild(link);
- }
- };
- require.alias = function (id) {
- return id.replace(/\.js$/i, '');
- };
- require.timeout = 5000;
- })(this);
|