Skip to content

Commit

Permalink
Warn once through util.deprecate, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Apr 11, 2015
1 parent 712e75c commit 9a2e885
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function tryExtensions(p, exts) {
}


const noopDeprecateRequireDot = util.deprecate(function() {},
`warning: require('.') did resolve outside the package directory. ` +
`This functionality will be deprecated soon.`);


Module._findPath = function(request, paths) {
var exts = Object.keys(Module._extensions);

Expand Down Expand Up @@ -169,9 +174,8 @@ Module._findPath = function(request, paths) {
}

if (filename) {
if (request === '.' && i > 0) {
console.error(`(node) warning: require('.') resolved to ${filename}`);
}
// Warn once if '.' resolved outside the module dir
if (request === '.' && i > 0) noopDeprecateRequireDot();
Module._pathCache[cacheKey] = filename;
return filename;
}
Expand Down Expand Up @@ -215,11 +219,14 @@ Module._resolveLookupPaths = function(request, parent) {
paths = parent.paths.concat(paths);
}

// For '.', put the module's dir at the front of the resolve paths
// TODO(silverwind): Treat '.' exactly the same as './'
// Maintain backwards compat with certain broken uses of require('.')
// by putting the module's directory in front of the lookup paths.
if (request === '.') {
paths.splice(0, 0, parent && parent.filename ?
path.dirname(parent.filename) : path.resolve(request));
if (parent && parent.filename) {
paths.splice(0, 0, path.dirname(parent.filename));
} else {
paths.splice(0, 0, path.resolve(request));
}
}

return [request, paths];
Expand Down

0 comments on commit 9a2e885

Please sign in to comment.