AMD(特别是 RequireJs)如何处理跨多个模块的依赖关系
我有一个调用 require() 的主初始化脚本,其中一个依赖项是实用程序框架,但我通过 require() 指定的其他一些模块本身也已将此框架定义为依赖项。
例如(init.js):
require(['module-a', 'module-b', 'module-c'], function(a, b, c){
// where module-c is the framework
});
然后在“module-a”中我有:
define(['module-c'], function(c){
// utilize module-c framework
});
那么AMD/RequireJs如何处理这种情况,它是否加载相同的框架两次?
任何帮助表示赞赏。
亲切的问候, 标记
I have my main initialization script which calls require() and one of the dependencies is a utilities framework, but some of the other modules that I'm specifying via require() also themselves have defined this framework as a dependency.
For example (init.js):
require(['module-a', 'module-b', 'module-c'], function(a, b, c){
// where module-c is the framework
});
And then in 'module-a' I have:
define(['module-c'], function(c){
// utilize module-c framework
});
So how does AMD/RequireJs handle this scenario, does it load the same framework twice?
Any help appreciated.
Kind regards,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它只会加载一次,上述两个模块都会为“module-c”获得相同的模块值。
It will only be loaded once, both of the above modules will get the same module value for 'module-c'.
万一它对其他人有用 - 这是我遇到的一种情况,模块被加载两次:
对于以下项目结构:
其中 RequireJs
baseurl
是~/prj/js/app
fileA.js
引用外部 (ext) 依赖项publisher.js
为:但是
fileB.js
引用具有不同的相同依赖项路径:简而言之,对于两者文件中,尽管依赖项位于同一位置,但依赖项路径不同。在这种情况下,publisher.js 被加载两次。
使用 Firebug 的
Net
选项卡查看其加载两次:使用
paths
配置外部文件夹路径可以轻松解决此问题(如 require_js docs):设置
paths
后,依赖项仅使用fileA.js
和fileB.js
加载一次,两者都使用相同的依赖路径如下:和
Incase its useful to others - Here's a situation I came across where a module was loaded twice:
For the following project structure:
where the RequireJs
baseurl
is~/prj/js/app
fileA.js
refers to the external (ext) dependancypublisher.js
as:But
fileB.js
refers to the same dependancy with a different path:In short, for both files, the dependency paths are different although the dependancy is in the same location. In this case, publisher.js gets loaded twice.
Use Firebug's
Net
tab to see it loading twice:This is easily fixed using
paths
to configure the external folder path (as explained in the require_js docs):After setting
paths
, the dependency is loaded just once withfileA.js
andfileB.js
both using the same dependency path as follows:and