Created
February 20, 2014 15:13
-
-
Save jdavisclark/9115898 to your computer and use it in GitHub Desktop.
project relative require
This file contains hidden or 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
| module.exports = function(projectRoot) { | |
| var nodeRequire = require; | |
| var path = nodeRequire("path"); | |
| // normalize the project root path | |
| projectRoot = path.normalize(projectRoot); | |
| if(projectRoot[projectRoot.length -1] !== "/") { | |
| projectRoot += "/"; | |
| } | |
| return function(rPath) { | |
| if(rPath[0] === "~") { | |
| rPath = rPath.replace(/^~\/?/, ""); | |
| return nodeRequire(path.join(projectRoot, rPath)); | |
| } else { | |
| return nodeRequire(rPath); | |
| } | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment