当前位置 : 主页 > 手机开发 > cordova >

cordova 源码分析记录

来源:互联网 收集:自由互联 发布时间:2021-06-10
1.模块定义 (function () { var modules = {}; // Stack of moduleIds currently being built. var requireStack = []; // Map of module ID - index into requireStack of modules currently being built. var inProgressModules = {}; var SEPARATOR = ‘

1.模块定义

(function () {
    var modules = {};
    // Stack of moduleIds currently being built.
    var requireStack = [];
    // Map of module ID -> index into requireStack of modules currently being built.
    var inProgressModules = {};
    var SEPARATOR = ‘.‘;

    function build (module) {
        var factory = module.factory;
        var localRequire = function (id) {
            var resultantId = id;
            // Its a relative path, so lop off the last portion and add the id (minus "./")
            if (id.charAt(0) === ‘.‘) {
                resultantId = module.id.slice(0, module.id.lastIndexOf(SEPARATOR)) + SEPARATOR + id.slice(2);
            }
            return require(resultantId);
        };
        module.exports = {};
        delete module.factory;
        factory(localRequire, module.exports, module);
        return module.exports;
    }

    require = function (id) {
        if (!modules[id]) {
            throw ‘module ‘ + id + ‘ not found‘;
        } else if (id in inProgressModules) {
            var cycle = requireStack.slice(inProgressModules[id]).join(‘->‘) + ‘->‘ + id;
            throw ‘Cycle in require graph: ‘ + cycle;
        }
        if (modules[id].factory) {
            try {
                inProgressModules[id] = requireStack.length;
                requireStack.push(id);
                return build(modules[id]);
            } finally {
                delete inProgressModules[id];
                requireStack.pop();
            }
        }
        return modules[id].exports;
    };

    define = function (id, factory) {
        if (modules[id]) {
            throw ‘module ‘ + id + ‘ already defined‘;
        }

        modules[id] = {
            id: id,
            factory: factory
        };
    };

    define.remove = function (id) {
        delete modules[id];
    };

    define.moduleMap = modules;
})();
网友评论