Skip to content

Commit 18c618f

Browse files
authored
Merge pull request #173 from rocjs/fix/improve-resolve-filename
Changed the Node resolving to be more reliable, fixing some issues
2 parents 6eb69a4 + fda507a commit 18c618f

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

docs/RocObject.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ __`request`__
149149
A string, the path that was requested.
150150

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

154154
#### `requires`
155155
Dependencies that are required and will be verified to exist by Roc.

src/require/createResolveRequest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export default function createResolveRequest(exports, directory, dependencyConte
3030
};
3131

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

src/require/patchResolveFilename.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Module = require('module');
2+
const path = require('path');
23

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

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

1011
Module._resolveFilename = function rocResolveFilename(request, parent) {
12+
// Get the context for the request, the directory that the request is coming from.
13+
// We are using "dirname" here to remove "node_modules" from the path that will be
14+
// present given how Nodes algorithm works, with the first path being the directory
15+
// from which the request was performed.
16+
// Example: [/dir/that/did/the/request/node_modules, /dir/that/did/the/node_modules, ...]
17+
const context = parent && parent.paths
18+
? path.dirname(parent.paths[0])
19+
: undefined;
20+
1121
try {
12-
return originalResolveFilename.apply(this, [resolveRequest(request, parent.id), parent]);
22+
return originalResolveFilename.apply(this, [
23+
resolveRequest(request, context),
24+
parent,
25+
]);
1326
} catch (_error) {
1427
/* We try again with fallback enabled.
1528
* This emulates kinda how NODE_PATH works in that we try again with another scope.
@@ -19,7 +32,7 @@ export default function patchResolveFilename(resolveRequest) {
1932
* extension is providing.
2033
*/
2134
return originalResolveFilename.apply(this, [
22-
resolveRequest(request, parent.id, { fallback: true }),
35+
resolveRequest(request, context, { fallback: true }),
2336
parent,
2437
]);
2538
}

0 commit comments

Comments
 (0)