1. 没有设置baseUrl(一般我们都会设置baseurl)

       在没有设置baseUrl时, 默认  baseurl: “./”

       当指定data-main时。  <script src="require.js" data-main="js/main.js"></script>. 将从data-main提取目录路径作为 baseurl,这里就是js/.  所有这里需要注意这里不能分成两个<script(一个加载require.js,一个加载main.js)来加载。这样baseurl将会是./加载依赖会可能会变找不到文件了。。。

源码以下:

       dataMain = script.getAttribute('data-main');
           if (dataMain) {
               //Preserve dataMain in case it is a path (i.e. contains '?')
               mainScript = dataMain;

               //Set final baseUrl if there is not already an explicit one.
              if (!cfg.baseUrl) {
                   //Pull off the directory of data-main for use as the
                   //baseUrl.
                   src = mainScript.split('/');
                   mainScript = src.pop();
                   subPath = src.length ? src.join('/')  + '/' : './';

                   cfg.baseUrl = subPath;
               }

               //Strip off any trailing .js since mainScript is now
               //like a module name.
               mainScript = mainScript.replace(jsSuffixRegExp, '');

                //If mainScript is still a path, fall back to dataMain
               if (req.jsExtRegExp.test(mainScript)) {
                   mainScript = dataMain;
               }

               //Put the data-main script in the files to load.
               cfg.deps = cfg.deps ? cfg.deps.concat(mainScript) : [mainScript];

               return true;
           }

 

2. paths设置

源码:

//A module that needs to be converted to a path.
                   paths = config.paths;

                    syms = moduleName.split('/');
                    //For each module name segment, see if there is a path
                    //registered for it. Start with most specific name
                    //and work up from it.
                    for (i = syms.length; i > 0; i -= 1) {
                        parentModule = syms.slice(0, i).join('/');

                       parentPath = getOwn(paths, parentModule);
                        if (parentPath) {
                            //If an array, it means there are a few choices,
                            //Choose the one that is desired
                            if (isArray(parentPath)) {
                                parentPath = parentPath[0];
                            }
                            syms.splice(0, i, parentPath);
                            break;
                        }
                    }

                    //url 添加baseurl, .js 等
                    //Join the path parts together, then figure out if baseUrl is needed.
                    url = syms.join('/');
                    url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js'));
                    url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;

将依赖字符串作为键, 从config的paths查找对应的键值。   从这里可以看出paths中的键是可以是数组,但也只能取数组第一个元素。 如下这个就是根据lib去paths查找lib,这里lib实际的路径为 js/common/lib.js

requirejs.config({
  baseUrl: 'js',
  paths: {
    lib: 'common/lib'
  }
});

 

require(['lib'], function(){
    // do sth
});

 

 

    path设置为数组
          
会一个一个加载, 直到成功为至.   这里的失败是服务端报错,不能是服务端自定义404页面那是不行. 

           当报错时时,会将路径从path中删除, undef(重新初始化一些属性) ,然后重新require当前的ID.

源码:

onScriptError: function (evt) {
                var data = getScriptData(evt);
                if (!hasPathFallback(data.id)) {
                    return onError(makeError('scripterror', 'Script error for: ' + data.id, evt, [data.id]));
                }
            }

 

function hasPathFallback(id) {
    var pathConfig = getOwn(config.paths, id);
    if (pathConfig && isArray(pathConfig) && pathConfig.length > 1) {
        //Pop off the first array value, since it failed, and
        //retry
        pathConfig.shift();
        context.require.undef(id);
        context.require([id]);
        return true;
    }
}

 

 

3. 插件加载

      插件名!资源名方法.   示例如下:

require(['text!txt.txt'], function(){
    // do sth
});

 

  源码解析:  暂无

requirejs源码分析: 路径的更多相关文章

  1. requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);

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

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

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

  3. requirejs源码分析: config中shim

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

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

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

  5. requirejs源码分析: define 方法

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

  6. requirejs源码分析

  7. Spring Developer Tools 源码分析:二、类路径监控

    在 Spring Developer Tools 源码分析一中介绍了 devtools 提供的文件监控实现,在第二部分中,我们将会使用第一部分提供的目录监控功能,实现对开发环境中 classpath ...

  8. Require.js 源码分析

    本文将简单介绍下个人对require.js的源码分析,简单分析实现原理 一.require加载资源的流程 require中,根据AMD(Asynchronous Module Definition)的 ...

  9. zookeeper源码分析之五服务端(集群leader)处理请求流程

    leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...

随机推荐

  1. SecureCRT中的vim出现1H特殊字符

    问题原因:由于采用了UTF_8编码方式导致的. 解决方法:把字符编码改为:GB18030即可.如图:

  2. eclipse 设置智能感知功能

    今天有点时间,研究了一下MyEclispse的智能感知的功能.刚开始使用它时总是感觉如此不爽→智能感知功能太弱!与Visual Studio2008简直不是一个档次的!不过后来经过查看网上的资料发现它 ...

  3. abp 列表和明细页面的规则说明

    0>列表区域mainviewbody 1>列表头mainviewheader 名称 2>列表条件mainviewcodtionbody 条件区 3>列表工具栏mainviewt ...

  4. 百度UEditor -- ZeroClipboard is not defined

    项目中一直在使用UEditor,风平浪静. 近期该项目引入了Requirejs,结果发现在有富文本编辑器的页面都会在控制台报出如下异常: Uncaught ReferenceError: ZeroCl ...

  5. PHP 几种 序列化/反序列化的方法

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  6. Mysql 5.7 liunx 忘记密码的补救方法

    linux下mysql的root密码忘记解决方 1.修改MySQL的登录设置 # vim /etc/my.cnf 在[mysqld]的段中加上一句: skip-grant-tables 例如: [my ...

  7. linux无密登录

    ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub bigdata@cor2

  8. pip依赖安装与记录

    pip freeze requirements.txt是一个常常被许多Flask应用用于列出它所依赖的包的文本文件.它是通过pip freeze > requirements.txt生成的. 使 ...

  9. Wireshark使用注意事项

    一直在使用老板的Wireshark,因为4G网络的逐步开通,越来越须要新版Wireshark来解析一些数据包. 在更换了新Wireshark的1.11.3后发现原来能够解析Gb口数据的NSIP不见了 ...

  10. ChemDraw能够生成化学性质报告吗

    ChemDraw破解版是一种不安全的软件版本,危害用户信息安全,一些不法分子正是因利用用户寻求免费软件的心理以ChemDraw破解版.ChemDraw注册机为噱头传播不安全信息.如果使用者已经成功安装 ...