Created
July 27, 2015 10:19
-
-
Save mholtzhausen/5206754564f8c0c226ad to your computer and use it in GitHub Desktop.
Aliasing parameters for require locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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