2015-06-28 13:09:57 +00:00
|
|
|
/**
|
|
|
|
* Allow code that lives on the server to resolve paths like
|
|
|
|
* require("plugins/c9.language/language"),
|
|
|
|
* just like on the client.
|
|
|
|
*/
|
|
|
|
var modules = require("module");
|
|
|
|
var oldResolve = modules._resolveFilename;
|
2015-08-19 14:07:32 +00:00
|
|
|
var path = require("path")
|
|
|
|
var root = path.join(__dirname, "/../..");
|
|
|
|
var extraPaths = [ root ];
|
2015-06-29 15:38:28 +00:00
|
|
|
modules._resolveFilename = function(request, parent) {
|
|
|
|
if (!parent) parent = { paths: [], filename: "" };
|
2015-06-28 13:09:57 +00:00
|
|
|
// Ensure client extensions can be loaded
|
|
|
|
request = request.replace(/^lib\//, "node_modules/");
|
2015-08-19 14:07:32 +00:00
|
|
|
// ensure we never use node_modules outside of root dir
|
2015-09-03 06:29:12 +00:00
|
|
|
if (parent.paths[0] && parent.paths[0].indexOf(root) == 0) {
|
|
|
|
parent.paths = parent.paths.filter(function(p) {
|
|
|
|
return p.indexOf(root) == 0;
|
|
|
|
});
|
|
|
|
}
|
2015-06-28 13:09:57 +00:00
|
|
|
// Add the extra paths
|
|
|
|
extraPaths.forEach(function(p) {
|
2015-06-29 15:38:28 +00:00
|
|
|
if (parent.paths.indexOf(p) === -1)
|
|
|
|
parent.paths.push(p);
|
2015-06-28 13:09:57 +00:00
|
|
|
});
|
2015-06-29 15:38:28 +00:00
|
|
|
return oldResolve(request, parent);
|
2015-06-28 13:09:57 +00:00
|
|
|
};
|