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. python学习之函数返回值

    python中函数返回值的方式有2种: 1.return语句 说明:return语句执行完后,函数后续的代码将不会被执行 2.yield语句 说明:yield语句返回的是一个迭代器对象,可以通过nex ...

  2. 李洪强iOS开发之OC语言前期准备

    OC语言前期准备 一.OC简介 Oc语言在c语言的基础上,增加了一层最小的面向对象语法,完全兼容C语言,在OC代码中,可以混用c,甚至是c++代码. 可以使用OC开发mac osx平台和ios平台的应 ...

  3. SQL数据库查询练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  4. Proving NP-completeness

    Proving NP-completeness by generalization. For each of the problems below, prove that it is NP-compl ...

  5. Hibernate无主键配置文件编写

    1.       环境:jdk1.4+hibernate2.0+weblogic8 一般情况下,我们建的表都会有主键,然后根据hibernate的配置文件编写条件 有一个主键key,剩下的是Prope ...

  6. 设置内容 - text()、html() 以及 val()

    我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回表单字段的 ...

  7. JavaScript 对大小写敏感。

    JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getElementbyID 是不同的. 同样,变 ...

  8. 最新win7系统安全稳定版

    最新win7系统32位安全稳定版 V2016年2月,具有更安全.更稳定.更人性化等特点.集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加上萝卜独有人性化的设计.是电脑城.个人.公 ...

  9. android EditText 限定中文个数与英文个数的解决方式

    EditText 限定中文8个英文16个的解决方法. 在EditText上控件提供的属性中有限定最大最小长度的方法. 可是,对于输入时,限定中文8个英文16个时,怎么办?相当于一个中文的长度是两个英文 ...

  10. Ant Design使用问题记录

    公司的测试管理平台前端使用的是Ant Design of React框架,后台使用的是python,数据库用的是mysql.没有参与前期的开发,听说是工作了10年积累下来的一个暂且可用的管理平台,开发 ...