RequireJS - 模块ID与模块名称 [英] RequireJS - module ID vs module name
问题描述
我在RequireJS中有点像菜鸟;我最近阅读了 API文档,并遇到了这两个术语:模块ID
和模块名称
。它们可以互换使用吗?或者它们是不同的概念?
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'
}
});
您的第一个报价说明当您拨打需要时会发生什么([ 'foo'],...
。上面的配置中没有路径
指定 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
.
你的第二句报价谈到 <$ c $时会发生什么c>路径为你的模块。如果你 require(['bar'],...
那么RequireJS会将id转换为 /lib/a/b/c.js
当它试图加载它时。它会添加扩展本身。同样的引用也倾向于暗示你要做<$ c的情况$ c> require(['bar / baz'],... 。使用上面的配置,RequireJS会将模块ID分成两部分: bar
和 baz
,会发现 bar
有路径
配置等等uild路径 / lib / a / b / c
然后将 baz
添加到它和扩展名中,这样就可以了尝试加载文件 /lib/a/b/c/baz.js
。因此,如果您具有相关模块的层次结构,则可以将该层次结构的根目录放在路径
中,而不是为层次结构中的每个模块指定路径。
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时使用的。假设翻转/翻转
已加载,其依赖项中包含 ..
。 RequireJS将翻转/翻牌
与 ..
组合,后者解析为翻转
,然后RequireJS会将此模块ID转换为路径: d / e / f.js
因为 flip
在路径中有一个映射
。有时人们认为RequireJS会解释相对于需要它的模块的路径的 ..
。该报价澄清事实并非如此。 (如果是这种情况,那么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屋!