上一篇 requirejs源码分析: requirejs 方法–1. 主入口  中的return context.require(deps, callback, errback);  调用的是makeRequire方法中的localRequire

 

function localRequire(deps, callback, errback) {
    var id, map, requireMod;

    if (options.enableBuildCallback && callback && isFunction(callback)) {
        callback.__requireJsBuild = true;
    }

    if (typeof deps === 'string') {
        if (isFunction(callback)) {
            //Invalid call
            return onError(makeError('requireargs', 'Invalid require call'), errback);
        }

        //If require|exports|module are requested, get the
        //value for them from the special handlers. Caveat:
        //this only works while module is being defined.
        if (relMap && hasProp(handlers, deps)) {
            return handlers[deps](registry[relMap.id]);
        }

        //Synchronous access to one module. If require.get is
        //available (as in the Node adapter), prefer that.
        if (req.get) {
            return req.get(context, deps, relMap, localRequire);
        }

        //Normalize module name, if it contains . or ..
        map = makeModuleMap(deps, relMap, false, true);
        id = map.id;

        if (!hasProp(defined, id)) {
            return onError(makeError('notloaded', 'Module name "' +
                        id +
                        '" has not been loaded yet for context: ' +
                        contextName +
                        (relMap ? '' : '. Use require([])')));
        }
        return defined[id];
    }

    //Grab defines waiting in the global queue.
    intakeDefines();

   //Mark all the dependencies as needing to be loaded.
    context.nextTick(function () {
        //Some defines could have been added since the
        //require call, collect them.
        intakeDefines();

        //实例化Module对象                   
        requireMod = getModule(makeModuleMap(null, relMap));

        //Store if map config should be applied to this require
        //call for dependencies.
        requireMod.skipMap = options.skipMap;

        //初始化 Module 对象
        requireMod.init(deps, callback, errback, {
            enabled: true
        });

        checkLoaded();
    });

    return localRequire;
}

这里主要是方法:

context.nextTick(function () {
        //Some defines could have been added since the
        //require call, collect them.
        intakeDefines();

        //实例化Module对象                   
        requireMod = getModule(makeModuleMap(null, relMap));

        //Store if map config should be applied to this require
        //call for dependencies.
        requireMod.skipMap = options.skipMap;

        //初始化 Module 对象
        requireMod.init(deps, callback, errback, {
            enabled: true
        });

        checkLoaded();
    });

这里使用context.nextTick是延迟4ms再执行(异步)。 这里不能很理解作者的目的。 是为了不卡主线程?

该方法主要流程:
1. 实例化Module。

requireMod = getModule(makeModuleMap(null, relMap));

        Module也是上下文newContext里的对象。这个是核心模块。包含以下主要的属性。

this.events = getOwn(undefEvents, map.id) || {};
this.map = map;
this.shim = getOwn(config.shim, map.id);
this.depExports = [];
this.depMaps = [];
this.depMatched = [];
this.pluginMaps = {};
this.depCount = 0;

该对象是封装模块,存在模块信息(shim,依赖等。)。 还包含对模块的加载,依赖加载,通知依赖等。

requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);的更多相关文章

  1. requirejs源码分析: requirejs 方法–1. 主入口

    该方法是 主要的入口点 也是最常用的方法. req = requirejs = function (deps, callback, errback, optional) { //Find the ri ...

  2. requirejs源码分析: define 方法

    define = function (name, deps, callback) {     var node, context; //Allow for anonymous modules     ...

  3. requirejs源码分析: 路径

    1. 没有设置baseUrl(一般我们都会设置baseurl)        在没有设置baseUrl时, 默认  baseurl: "./"        当指定data-mai ...

  4. requirejs源码分析: config中shim

    shim处理的源码: //Merge shim                 if (cfg.shim) {                     eachProp(cfg.shim, funct ...

  5. requirejs源码分析,使用注意要点

    本文将深度剖析require.js代码,为了是大家更高效.正确的去使用它,本文不会介绍require的基本使用! 概要 先来一个流程图来概要一下大概流程 在require中,根据AMD(Asynchr ...

  6. mybatis源码分析(方法调用过程)

    十一月月底,宿舍楼失火啦,搞得20多天没有网,目测直到放假也不会来了... 正题 嗯~,其实阅读源码不是为了应付面试,更重要的让你知道,大师是怎样去写代码的,同样是用Java,为啥Clinton Be ...

  7. requirejs源码分析

  8. WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)

    WorkerMan中work.php中 resetStd 方法中代码如下 public static function resetStd() { if (!static::$daemonize || ...

  9. 【requireJS源码学习01】了解整个requireJS的结构

    前言 现在工作中基本离不开requireJS这种模块管理工具了,之前一直在用,但是对其原理不甚熟悉,整两天我们来试着学习其源码,而后在探寻其背后的AMD思想吧 于是今天的目标是熟悉requireJS整 ...

随机推荐

  1. libevent源码学习_event_test

    对应的sample文件中提供了event_test.c,里面就是关于事件的简单示例,具体如下: /* * Compile with: * cc -I/usr/local/include -o even ...

  2. Building Maintainable Software-java篇之Separate Concerns in Modules

    Building Maintainable Software-java篇之Separate Concerns in Modules   In a system that is both complex ...

  3. HibernateTools实现pojo类 数据库schma mapping映射的相互转换 二

    接着上一篇博客:HibernateTools实现pojo类 数据库schma mapping映射的相互转换 思路二:由数据库表,生成Mapping映射文件和POJO类. 尽管能够实现,但个人觉着先设计 ...

  4. Hbuilder MUI 注册短信验证60秒后重新发送

    <div class="mui-input-row"> <label class="iconfont_log_reg icon-youjian" ...

  5. github+hexo+node.js搭建个人博客基本过程及遇到的问题

    一,所需工具 1,github账号+Gitclient+配置SSH key 2,安装node.js. 3,安装Hexo. 当中,github pages是我们用来部署我们本地的博客到github上的. ...

  6. diamond源码阅读-diamond-client

    读取数据 DiamondManager manager = new DefaultDiamondManager("DEFAULT_GROUP", "zml", ...

  7. NetCore 中 EFcore的DbFirst和CodeFirst混合 使用注意

    NetCore 最近很火热.笔者想把自己以前的旧项目迁移到NetCore平台. 先用EFcore的DBFirst根据数据库创建实体类,然后加入数据库版本控制功能也就是EFcore的CodeFirst部 ...

  8. IDLE崩溃:IDLE's subprocess didn't make connection. Either IDLE can't start a...

    今天在测试Python脚本的时候,突然间发现,脚本不能启动了,还弹出了“IDLE's subprocess didn't make connection. Either IDLE can't star ...

  9. vue起手式

    主要步骤 安装node 安装npm 安装vue-cli(vue命令行工具) 初始化一个vue项目 进行开发 # 安装node # 安装npm # 安装cnpm,在中国大陆防止被墙 # 安装git # ...

  10. Linux 的字符串截取很有用。有八种方法。

    假设有变量 var=http://www.aaa.com/123.htm 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} 其中 var 是变量名,# 号是运算符,*// ...