grunt-component-build
Version:
Build and watch Components
344 lines (320 loc) • 8.33 kB
JavaScript
/**
* Require the module at `name`.
*
* @param {String} name
* @return {Object} exports
* @api public
*/
function require(name) {
var module = require.modules[name];
if (!module) throw new Error('failed to require "' + name + '"');
if (!('exports' in module) && typeof module.definition === 'function') {
module.client = module.component = true;
module.definition.call(this, module.exports = {}, module);
delete module.definition;
}
return module.exports;
}
/**
* Meta info, accessible in the global scope unless you use AMD option.
*/
require.loader = 'component';
/**
* Internal helper object, contains a sorting function for semantiv versioning
*/
require.helper = {};
require.helper.semVerSort = function(a, b) {
var aArray = a.version.split('.');
var bArray = b.version.split('.');
for (var i=0; i<aArray.length; ++i) {
var aInt = parseInt(aArray[i], 10);
var bInt = parseInt(bArray[i], 10);
if (aInt === bInt) {
var aLex = aArray[i].substr((""+aInt).length);
var bLex = bArray[i].substr((""+bInt).length);
if (aLex === '' && bLex !== '') return 1;
if (aLex !== '' && bLex === '') return -1;
if (aLex !== '' && bLex !== '') return aLex > bLex ? 1 : -1;
continue;
} else if (aInt > bInt) {
return 1;
} else {
return -1;
}
}
return 0;
}
/**
* Find and require a module which name starts with the provided name.
* If multiple modules exists, the highest semver is used.
* This function can only be used for remote dependencies.
* @param {String} name - module name: `user~repo`
* @param {Boolean} returnPath - returns the canonical require path if true,
* otherwise it returns the epxorted module
*/
require.latest = function (name, returnPath) {
function showError(name) {
throw new Error('failed to find latest module of "' + name + '"');
}
// only remotes with semvers, ignore local files conataining a '/'
var versionRegexp = /(.*)~(.*)@v?(\d+\.\d+\.\d+[^\/]*)$/;
var remoteRegexp = /(.*)~(.*)/;
if (!remoteRegexp.test(name)) showError(name);
var moduleNames = Object.keys(require.modules);
var semVerCandidates = [];
var otherCandidates = []; // for instance: name of the git branch
for (var i=0; i<moduleNames.length; i++) {
var moduleName = moduleNames[i];
if (new RegExp(name + '@').test(moduleName)) {
var version = moduleName.substr(name.length+1);
var semVerMatch = versionRegexp.exec(moduleName);
if (semVerMatch != null) {
semVerCandidates.push({version: version, name: moduleName});
} else {
otherCandidates.push({version: version, name: moduleName});
}
}
}
if (semVerCandidates.concat(otherCandidates).length === 0) {
showError(name);
}
if (semVerCandidates.length > 0) {
var module = semVerCandidates.sort(require.helper.semVerSort).pop().name;
if (returnPath === true) {
return module;
}
return require(module);
}
// if the build contains more than one branch of the same module
// you should not use this funciton
var module = otherCandidates.pop().name;
if (returnPath === true) {
return module;
}
return require(module);
}
/**
* Registered modules.
*/
require.modules = {};
/**
* Register module at `name` with callback `definition`.
*
* @param {String} name
* @param {Function} definition
* @api private
*/
require.register = function (name, definition) {
require.modules[name] = {
definition: definition
};
};
/**
* Define a module's exports immediately with `exports`.
*
* @param {String} name
* @param {Generic} exports
* @api private
*/
require.define = function (name, exports) {
require.modules[name] = {
exports: exports
};
};
require.register("component~emitter@1.1.2", Function("exports, module",
"\n\
/**\n\
* Expose `Emitter`.\n\
*/\n\
\n\
module.exports = Emitter;\n\
\n\
/**\n\
* Initialize a new `Emitter`.\n\
*\n\
* @api public\n\
*/\n\
\n\
function Emitter(obj) {\n\
if (obj) return mixin(obj);\n\
};\n\
\n\
/**\n\
* Mixin the emitter properties.\n\
*\n\
* @param {Object} obj\n\
* @return {Object}\n\
* @api private\n\
*/\n\
\n\
function mixin(obj) {\n\
for (var key in Emitter.prototype) {\n\
obj[key] = Emitter.prototype[key];\n\
}\n\
return obj;\n\
}\n\
\n\
/**\n\
* Listen on the given `event` with `fn`.\n\
*\n\
* @param {String} event\n\
* @param {Function} fn\n\
* @return {Emitter}\n\
* @api public\n\
*/\n\
\n\
Emitter.prototype.on =\n\
Emitter.prototype.addEventListener = function(event, fn){\n\
this._callbacks = this._callbacks || {};\n\
(this._callbacks[event] = this._callbacks[event] || [])\n\
.push(fn);\n\
return this;\n\
};\n\
\n\
/**\n\
* Adds an `event` listener that will be invoked a single\n\
* time then automatically removed.\n\
*\n\
* @param {String} event\n\
* @param {Function} fn\n\
* @return {Emitter}\n\
* @api public\n\
*/\n\
\n\
Emitter.prototype.once = function(event, fn){\n\
var self = this;\n\
this._callbacks = this._callbacks || {};\n\
\n\
function on() {\n\
self.off(event, on);\n\
fn.apply(this, arguments);\n\
}\n\
\n\
on.fn = fn;\n\
this.on(event, on);\n\
return this;\n\
};\n\
\n\
/**\n\
* Remove the given callback for `event` or all\n\
* registered callbacks.\n\
*\n\
* @param {String} event\n\
* @param {Function} fn\n\
* @return {Emitter}\n\
* @api public\n\
*/\n\
\n\
Emitter.prototype.off =\n\
Emitter.prototype.removeListener =\n\
Emitter.prototype.removeAllListeners =\n\
Emitter.prototype.removeEventListener = function(event, fn){\n\
this._callbacks = this._callbacks || {};\n\
\n\
// all\n\
if (0 == arguments.length) {\n\
this._callbacks = {};\n\
return this;\n\
}\n\
\n\
// specific event\n\
var callbacks = this._callbacks[event];\n\
if (!callbacks) return this;\n\
\n\
// remove all handlers\n\
if (1 == arguments.length) {\n\
delete this._callbacks[event];\n\
return this;\n\
}\n\
\n\
// remove specific handler\n\
var cb;\n\
for (var i = 0; i < callbacks.length; i++) {\n\
cb = callbacks[i];\n\
if (cb === fn || cb.fn === fn) {\n\
callbacks.splice(i, 1);\n\
break;\n\
}\n\
}\n\
return this;\n\
};\n\
\n\
/**\n\
* Emit `event` with the given args.\n\
*\n\
* @param {String} event\n\
* @param {Mixed} ...\n\
* @return {Emitter}\n\
*/\n\
\n\
Emitter.prototype.emit = function(event){\n\
this._callbacks = this._callbacks || {};\n\
var args = [].slice.call(arguments, 1)\n\
, callbacks = this._callbacks[event];\n\
\n\
if (callbacks) {\n\
callbacks = callbacks.slice(0);\n\
for (var i = 0, len = callbacks.length; i < len; ++i) {\n\
callbacks[i].apply(this, args);\n\
}\n\
}\n\
\n\
return this;\n\
};\n\
\n\
/**\n\
* Return array of callbacks for `event`.\n\
*\n\
* @param {String} event\n\
* @return {Array}\n\
* @api public\n\
*/\n\
\n\
Emitter.prototype.listeners = function(event){\n\
this._callbacks = this._callbacks || {};\n\
return this._callbacks[event] || [];\n\
};\n\
\n\
/**\n\
* Check if this emitter has `event` handlers.\n\
*\n\
* @param {String} event\n\
* @return {Boolean}\n\
* @api public\n\
*/\n\
\n\
Emitter.prototype.hasListeners = function(event){\n\
return !! this.listeners(event).length;\n\
};\n\
\n\
//# sourceURL=components/component/emitter/1.1.2/index.js"
));
require.modules["component-emitter"] = require.modules["component~emitter@1.1.2"];
require.modules["component~emitter"] = require.modules["component~emitter@1.1.2"];
require.modules["emitter"] = require.modules["component~emitter@1.1.2"];
require.register("./dep", Function("exports, module",
"module.exports = 'Local dep';\n\
//# sourceURL=dep/index.js"
));
require.modules["dep"] = require.modules["./dep"];
require.register("./src", Function("exports, module",
"var Emitter = require('component~emitter@1.1.2');\n\
var localDep = require('local-dep');\n\
\n\
module.exports = Foo;\n\
\n\
function Foo() {\n\
console.log(localDep);\n\
}\n\
\n\
Emitter(Foo.prototype);\n\
\n\
var string = \"{{ foo }}\";\n\
\n\
Foo.prototype.bar = function() {\n\
this.emit('bar');\n\
};\n\
//# sourceURL=src/index.js"
));
require.modules["src"] = require.modules["./src"];
require("./src");