Skip to content

Commit

Permalink
Merge pull request #173 from rocjs/fix/improve-resolve-filename
Browse files Browse the repository at this point in the history
Changed the Node resolving to be more reliable, fixing some issues
  • Loading branch information
dlmr authored May 10, 2017
2 parents 6eb69a4 + fda507a commit 18c618f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/RocObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ __`request`__
A string, the path that was requested.

__`requestContext`__
A string, the location of the request. The path to the file that did the request.
A string, the location of the request. The path to the directory from which the request was performed.

#### `requires`
Dependencies that are required and will be verified to exist by Roc.
Expand Down
4 changes: 2 additions & 2 deletions src/require/createResolveRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default function createResolveRequest(exports, directory, dependencyConte
};

const requestHelper = (request, context, fallback, identifier) => {
// If we got an empty request we want to let Node handle it
if (!request) {
// If we got an empty request or context we want to let Node handle it
if (!request || !context) {
return { completedRequest: request };
}

Expand Down
17 changes: 15 additions & 2 deletions src/require/patchResolveFilename.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Module = require('module');
const path = require('path');

const log = require('debug')('roc:core:require');

Expand All @@ -8,8 +9,20 @@ export default function patchResolveFilename(resolveRequest) {
log('Initializing');

Module._resolveFilename = function rocResolveFilename(request, parent) {
// Get the context for the request, the directory that the request is coming from.
// We are using "dirname" here to remove "node_modules" from the path that will be
// present given how Nodes algorithm works, with the first path being the directory
// from which the request was performed.
// Example: [/dir/that/did/the/request/node_modules, /dir/that/did/the/node_modules, ...]
const context = parent && parent.paths
? path.dirname(parent.paths[0])
: undefined;

try {
return originalResolveFilename.apply(this, [resolveRequest(request, parent.id), parent]);
return originalResolveFilename.apply(this, [
resolveRequest(request, context),
parent,
]);
} catch (_error) {
/* We try again with fallback enabled.
* This emulates kinda how NODE_PATH works in that we try again with another scope.
Expand All @@ -19,7 +32,7 @@ export default function patchResolveFilename(resolveRequest) {
* extension is providing.
*/
return originalResolveFilename.apply(this, [
resolveRequest(request, parent.id, { fallback: true }),
resolveRequest(request, context, { fallback: true }),
parent,
]);
}
Expand Down

0 comments on commit 18c618f

Please sign in to comment.