RequireJS:模块ID与模块名称 [英] RequireJS: module ID vs module name

查看:289
本文介绍了RequireJS:模块ID与模块名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RequireJS中有点菜鸟;我最近阅读了 API文档,并遇到了以下两个术语:module IDmodule name .它们可以互换使用吗?还是它们在某种程度上是不同的概念?

I'm kinda noob in RequireJS; I recently read the API documentation, and came across these two terms: module ID and module name. Are they used interchangeably? Or are they somehow different concepts?

节选:

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

RequireJS在默认情况下还假定所有依赖项都是脚本,因此,它不会在模块ID 上看到尾随的".js"后缀.将模块ID 转换为路径时,RequireJS将自动添加它.

RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs. RequireJS will automatically add it when translating the module ID to a path.

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

用于模块名称的路径不应包含扩展名,因为路径映射可能用于目录.将模块名称映射到路径时,路径映射代码将自动添加.js扩展名.

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.

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

加载程序按模块名称而不是内部路径存储模块.因此,对于相对名称引用,将相对于引用的模块名称进行解析,然后将模块名称 ID 转换为路径(如果需要加载)

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. Suppose the following configuration:

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

您的第一句引用内容是有关调用require(['foo'], ...之类的内容时发生的情况.上面的配置中没有paths指定foo转换为的内容.因此,RequireJS将根据模块ID(foo)创建路径.最终它将尝试加载文件/lib/foo.js.

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(['bar'], ...,则RequireJS在尝试加载ID时会将其转换为/lib/a/b/c.js.它添加扩展本身.同样的引用也倾斜地暗示您要执行require(['bar/baz'], ...的情况.使用上面的配置,RequireJS会将模块ID分为两部分:barbaz,会发现bar具有paths配置,因此将构建路径/lib/a/b/c,然后添加及其扩展名,以便它尝试加载文件/lib/a/b/c/baz.js.因此,如果您具有相关模块的层次结构,则只需将该层次结构的根放在paths中,而不必为该层次结构中的每个模块指定路径.

Your second quote talks about what happens when there is a paths for your module. 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.

第三引号指出,模块ID是解释相对模块ID时使用的模块.假设flip/flop已加载并且其依赖项中包含... RequireJS将flip/flop..组合为flip,然后RequireJS将此模块ID转换为路径:d/e/f.js,因为flippaths中具有映射.有时人们认为RequireJS会相对于需要它的模块的 path 解释...引号阐明不是事实. (如果是这种情况,那么RequireJS将尝试加载dir/dir.js.)

The third quote points out that the module id is what is used when interpreting relative module ids. Let's say flip/flop has been loaded and it has .. in its dependencies. 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. Sometimes people think that RequireJS will interpret .. relative to the path of the module that needs it. The quote clarifies that it is not the case. (If it were the case, then RequireJS would try to load dir/dir.js.)

这篇关于RequireJS:模块ID与模块名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆