Automatic Dependency Resolution For JavaScript Libraries

Just added a feature to my JavaScript libraries so that they can work out what other JavaScript libraries need to be included and then includes them in the correct order. Most of the work is done by the foloowing three functions: MwUse , MwRequire and MwInclude .

Not working under all browsers yet – but shows lots of promise!

For example, in one of my test cases I specify the following script includes

<script language="JavaScript" type="text/javascript" src="mw_lib.js" ></script>
<script language="JavaScript" type="text/javascript" src="mw_lib_page.js" ></script>

mw_lib.js is the core library and depends on no external libraries so it does not trigger the load of any dependencies.

mw_lib_page.js however has the following two dependencies decared with the following statements.

// Ensure we have the namespace we need before we load this page

// Ensure we have the namespaces/objects we need before we start executing

MwUse checks to see if the specified object exist, if not it loads the corresponding JavaScript file (in a future feature it will work out the file name from the object name if the filename is not provided, hence my strict naming standard).

So mw_lib_page.js is declaring that it needs the namespace MetaWrap which can be found in mw_lib.js and that if you want to even load and parse this library, you better have it. But we already have that namespace loaded so nothing happens. if this was another namespace and file pair, such as


Then mw_lib_test.js would be downloaded and included by MwInclude before mw_lib_page.js loaded any further.

MwRequire checks to see if the object exists and loads the corresponding javascript file only at the point of initialising the library, so its a kind of defered MwUse. Any files that calls, MwRequire is declaring that the file can continue to parse but before you execute any of the methods in the file, you need to include the specified library or it Would Be Bad.

To initialise the library we call MwInit() (looking into automating this – stay posted)

At this point all the namespaces/objects that have been specified by MwRequire are loaded and in this case its mw_lib_page_element_addhandler.js, which contains the following lines of code in its header.

// Ensure we have the namespaces we need




So it checks for MetaWrap, which it has, and then MetaWrap.Page, that it already has, but it does not have MetaWrap.Page.Element so it loads mw_lib_page_element.js, which contains the following

// Ensure we have the namespaces we need



And all of these namespaces are loaded – so no more loads are triggered.

It solves some nagging problems for me and counters human error so I’m really liking this new feature.


About James McParlane

CTO Massive Interactive. Ex Computer Whiz Kid - Now Grumpy Old Guru.
This entry was posted in JavaScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s