-
-
Notifications
You must be signed in to change notification settings - Fork 191
Implements packageIterator #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,14 @@ var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts) { | |
| return x; | ||
| }; | ||
|
|
||
| var getPackageCandidates = function getPackageCandidates(x, start, opts) { | ||
| var dirs = nodeModulesPaths(start, opts, x); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this function is the same in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that, but then it's a separate file that has to be supported for the rest of v1. |
||
| for (var i = 0; i < dirs.length; i++) { | ||
| dirs[i] = path.join(dirs[i], x); | ||
| } | ||
| return dirs; | ||
| }; | ||
|
|
||
| module.exports = function (x, options) { | ||
| if (typeof x !== 'string') { | ||
| throw new TypeError('Path must be a string.'); | ||
|
|
@@ -47,6 +55,7 @@ module.exports = function (x, options) { | |
| var isFile = opts.isFile || defaultIsFile; | ||
| var isDirectory = opts.isDirectory || defaultIsDir; | ||
| var readFileSync = opts.readFileSync || fs.readFileSync; | ||
| var packageIterator = opts.packageIterator; | ||
|
|
||
| var extensions = opts.extensions || ['.js']; | ||
| var basedir = opts.basedir || path.dirname(caller()); | ||
|
|
@@ -162,13 +171,15 @@ module.exports = function (x, options) { | |
| } | ||
|
|
||
| function loadNodeModulesSync(x, start) { | ||
| var dirs = nodeModulesPaths(start, opts, x); | ||
| var thunk = function () { return getPackageCandidates(x, start, opts); }; | ||
| var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk(); | ||
|
|
||
| for (var i = 0; i < dirs.length; i++) { | ||
| var dir = dirs[i]; | ||
| if (isDirectory(dir)) { | ||
ljharb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var m = loadAsFileSync(path.join(dir, '/', x)); | ||
| if (isDirectory(path.dirname(dir))) { | ||
| var m = loadAsFileSync(dir); | ||
| if (m) return m; | ||
| var n = loadAsDirectorySync(path.join(dir, '/', x)); | ||
| var n = loadAsDirectorySync(dir); | ||
| if (n) return n; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| { | ||
| "name": "baz", | ||
| "main": "quux.js" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when is the dirname not going to be a directory? the intention here, i believe, is to see if
dirpoints to a file or not. It seems like hoisting thepath.joinprevents that from being determined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
dirnameisn't a directory when it doesn't exist. From what I gather from #190 and #116, this code was only meant to speed up the resolution by checking whether the n_m folder existed before doing the file stat calls.Checking whether the parent of the subpath (ie
/n_m/fooforfoo/hello) exists has the same behaviours: iffoo/hellocan be resolved, then/n_m/foowill necessarily be a directory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words:
foo/n_m/foo/n_m/n_mfoo/bar/n_m/foo/bar/n_m/n_m/foo@foo/bar/n_m/@foo/bar/n_m/n_m/@fooThe effect will be the same before and after: the two stat calls that the code would have made before #190 (taking the first line as an example, one to check whether
/n_m/foo.jsexists and another to check whether/n_m/foo/index.jsexists) won't be necessary becauseresolveis able to find out that the directory supposed to contain them doesn't even exist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Do you think that this perf improvement could also be split out into a separate PR (or even just a separate commit prior to your addition)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR it's not a perf improvement, it's necessary to make my PR work with the changes brought by #190 (because with my PR we don't have access to
n_mpaths anymore inloadNodeModules- instead we only have/n_m/<subpath>). I guess I could revert it and re-apply it, but I'm not sure it would be much clearer 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm mainly concerned about an unintentional breakage, and my hope would be to release this particular change as a patch prior to releasing this PR as a minor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #190 has only been released in the 2.0.0-next.0 branch, so for the purpose of the patch release the
path.dirnamething can be removed entirely. Do you want me to open a separate PR against the1.xbranch?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#192 was the PR into the 1.x branch.