RequireJS-jQuery区分大小写吗?

RequireJS-jQuery区分大小写吗?,requirejs,Requirejs,我试图开始使用RequireJS,但遇到了一个恼人的问题 require.config({ baseUrl: 'app_content/scripts', paths: { // the left side is the module ID, // the right side is the path to // the jQuery file, relative to baseUrl. // Also, the p

我试图开始使用RequireJS,但遇到了一个恼人的问题

require.config({
    baseUrl: 'app_content/scripts',
    paths: {
        // the left side is the module ID,
        // the right side is the path to
        // the jQuery file, relative to baseUrl.
        // Also, the path should NOT include
        // the '.js' file extension. This example
        // is using jQuery 1.9.0 located at
        // js/lib/jquery-1.9.0.js, relative to
        // the HTML page.
        'jQuery': 'lib/jQuery/jQuery-2.0.3'
    }
});
使用大写字母
Q
,这不起作用,但如果我将其更改为
jquery
,它会起作用。而且,这在我所需要的领域也是如此

<script type="text/javascript">
    require(["jQuery"], function ($) {
        console.log($);
    });
</script>

require([“jQuery”],function($){
console.log($);
});
这将返回未定义的
undefined
,但如果我将所有内容都更改为right up
jquery
,它将正常工作


这是预期的行为吗?我能做些什么吗?

是的,jQuery将自己定义为“jQuery”,全部小写。这很正常

如果打开jQuery的源代码,您将发现:

// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.
if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function () { return jQuery; } );
}
因此,在RequireJS调用中,您必须将其称为
“jquery”
。这里的问题是jQuery使用的
define
是我们在创建模块时通常不使用的。当我们运行RequireJS优化器时,它会为我们添加这些名称

无论如何,当使用“命名的
define
”时,模块名称被设置为
define
的名称,而不是文件名(否则,当我们不使用命名的
define
时)

可以将
“jquery”
重命名为
“jquery”
,如下所示:

require.config({
  baseUrl: "./js",
  paths: {
      "jquery": "jquery-1.10.2"
  }
});

define("jQuery", ["jquery"], function ($) {
   return $;
});

require(["jQuery"], function ($) {
   console.log($);
   console.log($("body")[0]);
});

我使用的是
define
版本,它将名称作为第一个参数。完整示例。

虽然这是真的,但它并没有涵盖给定的RequireJS配置更改使其正常工作的内容。换句话说:是什么阻止了使用备用模块ID(“jQuery”)?我已经删除了我的否决票,但我仍然很好奇为什么没有添加别名,即使使用了命名的define。我确实找到了,但它回答了一个正交问题。
路径
不是给已经命名的模块赋予新名称的地方。我已经用一种方法更新了我的答案。也可以使用带有空模块的垫片来获得相同的结果,但这需要磁盘上有一个空文件(以及一个额外的网络请求)。这很公平。我仍然希望找到“抑制”替代模块ID的行为记录在哪里,不过:(