| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | /** * cmd 内部定义 */(function(global) {    var _modules = {},        loaded = {};    global.inc = {        base: '',        config: function(options) {            this.base = options.base || '';        },        use: function(id) {            return require(id);        },        remove: function(node) {            node.parentNode.removeChild(node);        }    };    global.use = global.seajs.use;    global.define = function(id, deps, f) {        var argLen = arguments.length,            module = null;        var scriptNode;        switch (argLen) {            case 1:                scriptNode = document.getElementsByTagName('script');                f = id;                id = scriptNode[scriptNode.length - 1].getAttribute('data-id');                break;            case 2:                if (typeof id === 'string') {                    f = deps;                } else {                    scriptNode = document.getElementsByTagName('script');                    f = deps;                    id = scriptNode[scriptNode.length - 1].getAttribute('data-id');                }                break;        }        module = _modules[id] = {            exports: {},            value: null,            factory: null        };        loadDeps(f);        if (typeof f === 'function') {            module.factory = f;        } else {            module.value = f;        }    };    function require(id) {        var exports = {},            module = _modules[id];        if (module.value) {            return module.value;        }        exports = module.factory(require, module.exports, module);        if (exports) {            module.exports = exports;        }        module.value = module.exports;        module.exports = null;        module.factory = null;        return module.value;    }    function loadDeps(factory) {        var deps = null,            pathname = location.pathname,            uri = location.protocol + '//' + location.host;        pathname = pathname.split('/');        if (pathname[pathname.length - 1] !== '') {            pathname[pathname.length - 1] = '';        }        uri += pathname.join('/');        if (typeof factory === 'function') {            deps = loadDepsByFunction(factory);        } else {            // 未处理object的情况            return;        }        for (var i = 0, len = deps.length; i < len; i++) {            var key = deps[i];            if (loaded[key]) {                continue;            }            loaded[key] = true;            document.write('<script src="' + uri + inc.base + '/' + key + '.js" '  +                'onload="inc.remove(this)"' + ' data-id="' + key + '"></script>');        }    }    function loadDepsByFunction(factory) {        var content = factory.toString(),            match = null,            deps = [],            pattern = /require\s*\(\s*([^)]+?)\s*\)/g;        while ((match = pattern.exec(content))) {            deps.push(match[1].replace(/'|"/g, ''));        }        return deps;    }})(this);
 |