It turns out that one way to do this is to call Y.add() proactively on the module name. This works well for modules that are not themselves defined with Y.add() (e. g. jQuery plugins). However, there is a potential race condition if you do this with modules that ARE defined with Y.add(). For example, consider the following sequence:
- script for module A is run (calls Y.add('A', ...))
- YUI goes off to load the requirements for module A before running the callback
- Y.add('A') is called again with an empty callback (to notify YUI that the script file for A is on the page)
- YUI since the async load hasn't yet returned, YUI takes the empty callback as the definition of A
The fix is to use the following script:
var preloadedModules = ['a', ... ]; // list of module scripts already on the page
, noop = function (Y) { }, version = '@VERSION@', i, len, moduleName;
for (var i = 0, len = preloadedModules.length; i < len; ++i) {
moduleName = preloadedModules[i];
if (!YUI.Env.mods[moduleName]) { // avoids overwriting YUI module definitions
YUI.add(moduleName, noop, version);
}
}