Skip to content

Instantly share code, notes, and snippets.

@mholtzhausen
Created July 27, 2015 10:19
Show Gist options
  • Save mholtzhausen/5206754564f8c0c226ad to your computer and use it in GitHub Desktop.
Save mholtzhausen/5206754564f8c0c226ad to your computer and use it in GitHub Desktop.
Aliasing parameters for require locations
require = function (filename) {
console.log('The parameter passed to require : ' + filename);
};
require._alias = function (name, path) {
var _rStartsWithSlash = /^\//;
var _rEndsWithSlash = /\/$/;
require[name] = (function (path) {
return function (filename) {
var fullFileName = (path + (_rEndsWithSlash.test(path) ? '' : '/')) + (_rStartsWithSlash.test(filename) ? filename.substr(1) : filename);
require(fullFileName);
};
})
(path);
};
require._alias('local', './local/lib/'); //relative to the file calling .require
require._alias('local2', '/this/is/from/a/different/root'); //absolute path to resources
require.local('somefile');
require.local2('somefile2');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment