Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created July 25, 2012 03:03
Show Gist options
  • Save juandopazo/3174141 to your computer and use it in GitHub Desktop.
Save juandopazo/3174141 to your computer and use it in GitHub Desktop.
YUI AMD loader
var YUI_config = {};
(function (YUI, global) {
var modules = {};
var justLoaded = [];
var AMD_prefix = '~AMD~';
function nameFromPath(path) {
var parts = path.split(/\//g).pop().split(/\./g),
ext = parts.pop();
return (ext == 'css' || ext == 'js') ? parts.pop() : ext;
}
function registerModule(name, deps, factory) {
var path = name;
name = nameFromPath(name);
var requires = deps.filter(function (dep) {
return dep != 'require' && dep != 'exports';
});
YUI.add(name, function (Y) {
var isFunction = typeof factory == 'function',
returns = isFunction,
result;
function require(module) {
return Y[AMD_prefix + nameFromPath(module)] || Y;
}
var args = deps.map(function (dep) {
if (dep == 'require') {
return require;
} else if (dep == 'exports') {
returns = false;
return Y;
} else {
return require(dep);
}
});
if (isFunction) {
result = factory.apply(this, args);
}
if (returns) {
Y[AMD_prefix + name] = result;
}
}, '', { requires: requires });
}
global.define = function define(name, deps, factory) {
if (arguments.length === 1) {
factory = name;
deps = [];
name = '';
} else if (arguments.length === 2) {
factory = deps;
if (typeof name == 'string') {
deps = [];
} else {
deps = name;
name = '';
}
}
if (name === '') {
justLoaded.push({
deps: deps,
fn: factory
});
} else {
registerModule(name, deps, factory);
}
};
global.require = function require(config, modules, callback) {
if (arguments.length == 2) {
callback = modules;
modules = config;
config = {};
} else if (arguments.length == 1) {
if (typeof config == 'function') {
callback = config;
modules = [];
} else {
modules = config;
callback = function () {};
}
config = {};
}
var request = modules.filter(function (mod) {
return mod != 'require';
});
YUI(config).use(request, function (Y) {
function require(name) {
return Y[AMD_prefix + nameFromPath(name)] || Y;
}
var args = modules.filter(function (mod) {
return mod.substr(mod.length - 3) != 'css';
}).map(function (name) {
return name == 'require' ? require : require(name);
});
callback.apply(this, args);
});
};
YUI.add = (function (__add) {
return function add(name) {
var args = Array.prototype.slice.call(arguments, 1);
if (modules[name]) {
name = modules[name];
}
return __add.apply(this, [name].concat(args));
}
}(YUI.add));
YUI_config.onProgress = function (o) {
var def = justLoaded[justLoaded.length - 1];
if (def) {
registerModule(o.name, def.deps, def.fn);
}
};
YUI_config.groups = {
amd: {
patterns: {
css: {
test: function (name) {
return name.substr(name.length - 4) == '.css';
},
configFn: function (mod) {
mod.type = 'css';
mod.fullpath = mod.name;
}
},
js: {
test: function (name) {
return name.charAt(0) == '.';
},
configFn: function (mod) {
var fullpath = mod.name,
parts = fullpath.split(/\//g).pop().split(/\./g),
ext = parts.pop(),
name = nameFromPath(fullpath);
if (ext != 'js') {
fullpath += '.js';
}
modules[name] = mod.name;
mod.fullpath = fullpath;
}
}
}
}
};
}(YUI, window));
@bharatkumar-r
Copy link

Could you please give the samples to use this script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment