How does AMD (specifically RequireJs) handle dependancies across multiple modules

梦想与她 提交于 2019-12-04 03:39:54

It will only be loaded once, both of the above modules will get the same module value for 'module-c'.

Incase its useful to others - Here's a situation I came across where a module was loaded twice:

For the following project structure:

~/prj/js/app/fileA.js
~/prj/js/app/util/fileB.js
~/prj/js/ext/publisher.js

where the RequireJs baseurl is ~/prj/js/app

fileA.js refers to the external (ext) dependancy publisher.js as:

//fileA:
define(['../ext/publisher'], function(){});

But fileB.js refers to the same dependancy with a different path:

//fileB:
define(['../../ext/publisher'], function(){});

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):

requirejs.config({
    paths: {ext: '../ext'}
});

After setting paths, the dependency is loaded just once with fileA.js and fileB.js both using the same dependency path as follows:

//fileA:
define(['ext/publisher'], function(){});

and

//fileB:
define(['ext/publisher'], function(){});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!