简体   繁体   English

RequireJS:模块ID与模块名称

[英]RequireJS: module ID vs module name

I'm kinda noob in RequireJS; 我在RequireJS中有点菜鸟; I recently read the API documentation , and came across these two terms: module ID and module name . 我最近阅读了API文档 ,并遇到了两个术语: module IDmodule name Are they used interchangeably? 它们可以互换使用吗? Or are they somehow different concepts? 还是它们有不同的概念?

Excerpts: 摘录:

http://requirejs.org/docs/api.html#jsfiles http://requirejs.org/docs/api.html#jsfiles

RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module ID s. 默认情况下,RequireJS还假定所有依赖项都是脚本,因此,它不会在模块ID上看到尾随的“ .js”后缀。 RequireJS will automatically add it when translating the module ID to a path. 在将模块ID转换为路径时,RequireJS将自动添加它。

http://requirejs.org/docs/api.html#config-paths http://requirejs.org/docs/api.html#config-paths

The path that is used for a module name should not include an extension, since the path mapping could be for a directory. 用于模块名称的路径不应包含扩展名,因为路径映射可能用于目录。 The path mapping code will automatically add the .js extension when mapping the module name to a path. 当将模块名称映射到路径时,路径映射代码将自动添加.js扩展

http://requirejs.org/docs/api.html#modulenotes http://requirejs.org/docs/api.html#modulenotes

The loader stores modules by their name and not by their path internally. 加载程序按模块名称而不是内部路径存储模块。 So for relative name references, those are resolved relative to the module name making the reference, then that module name , or ID , is converted to a path if needs to be loaded. 因此,对于相对名称引用,相对于引用的模块名称进行解析,然后如果需要加载,则将该模块名称ID转换为路径。

Module name and module id are the same thing, and they are different form the module path. 模块名称和模块ID是同一件事,并且在模块路径上是不同的。 Suppose the following configuration: 假设以下配置:

require.config({
    baseUrl: '/lib/',
    paths  : {
        bar        : 'a/b/c',
        flip       : 'd/e/f',
        'flip/flop': 'dir/dir/something'
    }
});

Your first quote talks about what happens when you call something like require(['foo'], ... . There is no paths in the configuration above that specifies what foo translates to. So RequireJS will create a path from the module id, which is foo . Ultimately it will try to load the file /lib/foo.js . 约当你调用像会发生什么你的第一次报价谈判require(['foo'], ... ,没有paths ,在上面的配置中指定了foo转化为。所以RequireJS将从模块ID创建路径,这是foo ,最终它将尝试加载文件/lib/foo.js

Your second quote talks about what happens when there is a paths for your module. 您的第二个引用内容关于模块存在paths的情况。 If you require(['bar'], ... then RequireJS will transform the id to /lib/a/b/c.js when it tries to load it. It adds the extension itself. This same quote also obliquely alludes to the case where you'd do require(['bar/baz'], ... . With the configuration above, RequireJS would split the module id in two: bar , and baz , would find that bar has a paths configuration and so would build the path /lib/a/b/c and then would add baz to it and the extension so it would try to load the file /lib/a/b/c/baz.js . So if you have hierarchy of related modules you can just put the root of that hierarchy in your paths rather than specify a path for each and every module in the hierarchy. 如果您require(['bar'], ...则RequireJS会在尝试加载ID时将其ID转换为/lib/a/b/c.js 。它会自己添加扩展名。在您require(['bar/baz'], ...的情况下,使用上述配置,RequireJS会将模块ID分为两部分: barbaz ,会发现bar具有paths配置,因此会构建路径/lib/a/b/c ,然后在其和扩展名中添加baz ,以便它将尝试加载文件/lib/a/b/c/baz.js 。您可以仅将模块层次结构的根放在paths而不必为层次结构中的每个模块指定路径。

The third quote points out that the module id is what is used when interpreting relative module ids. 第三引号指出,模块ID是解释相对模块ID时使用的模块。 Let's say flip/flop has been loaded and it has .. in its dependencies. 假设flip/flop已加载,并且其依赖项中有.. RequireJS will combine flip/flop with .. which resolves to flip , and then RequireJS will convert this module id to a path: d/e/f.js because flip has a mapping in the paths . RequireJS将把flip/flop..结合起来,解析为flip ,然后RequireJS会将这个模块ID转换为路径: d/e/f.js因为flippaths具有映射。 Sometimes people think that RequireJS will interpret .. relative to the path of the module that needs it. 有时人们认为RequireJS将解释..相对于需要它的模块的路径 The quote clarifies that it is not the case. 引号阐明不是事实。 (If it were the case, then RequireJS would try to load dir/dir.js .) (如果是这种情况,那么RequireJS将尝试加载dir/dir.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM