Skip to content

Instantly share code, notes, and snippets.

@jdavisclark
Created February 20, 2014 15:13
Show Gist options
  • Select an option

  • Save jdavisclark/9115898 to your computer and use it in GitHub Desktop.

Select an option

Save jdavisclark/9115898 to your computer and use it in GitHub Desktop.
project relative require
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