-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
require.resolve with options.paths + require.resolve.paths #6471
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3a7d79
refactor(resolve): extract resolution function that does not throw if…
jeysal ec79974
feat(resolve): add paths override to ResolveModuleConfig
jeysal 3257c9a
feat(runtime): support require.resolve with options.paths
jeysal e669f89
test(resolve): resolveModule with custom paths
jeysal be2948a
test(module-name-mapper): update line number in snapshot
jeysal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const runJest = require('../runJest'); | ||
|
||
test('require.resolve.paths', () => { | ||
const {status} = runJest('resolve-get-paths'); | ||
expect(status).toBe(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const {resolve} = require('path'); | ||
|
||
const runJest = require('../runJest'); | ||
const {writeFiles, cleanup} = require('../Utils'); | ||
|
||
const workdirNodeModules = resolve( | ||
__dirname, | ||
'..', | ||
'resolve-with-paths', | ||
'node_modules', | ||
); | ||
|
||
beforeAll(() => { | ||
writeFiles(resolve(workdirNodeModules, 'mod'), { | ||
'index.js': 'module.exports = 42;', | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanup(workdirNodeModules); | ||
}); | ||
|
||
test('require.resolve with paths', () => { | ||
const {status} = runJest('resolve-with-paths'); | ||
expect(status).toBe(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
import {resolve} from 'path'; | ||
|
||
test('returns the resolve path for a relative path', () => { | ||
expect(require.resolve.paths('./mod.js')).toEqual([resolve(__dirname)]); | ||
}); | ||
|
||
test('returns the resolve paths for a node_module', () => { | ||
expect(require.resolve.paths('mod').slice(0, 2)).toEqual([ | ||
resolve(__dirname, 'node_modules'), | ||
resolve(__dirname, '..', 'node_modules'), | ||
]); | ||
}); | ||
|
||
test('returns null for a native node module', () => { | ||
expect(require.resolve.paths('fs')).toBeNull(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
e2e/resolve-with-paths/__tests__/resolve-with-paths.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
import {resolve} from 'path'; | ||
|
||
test('finds a module relative to one of the given paths', () => { | ||
expect(require.resolve('./mod.js', {paths: ['../dir']})).toEqual( | ||
resolve(__dirname, '..', 'dir', 'mod.js') | ||
); | ||
}); | ||
|
||
test('finds a module without a leading "./" relative to one of the given paths', () => { | ||
expect(require.resolve('mod.js', {paths: ['../dir']})).toEqual( | ||
resolve(__dirname, '..', 'dir', 'mod.js') | ||
); | ||
}); | ||
|
||
test('finds a node_module above one of the given paths', () => { | ||
expect(require.resolve('mod', {paths: ['../dir']})).toEqual( | ||
resolve(__dirname, '..', 'node_modules', 'mod', 'index.js') | ||
); | ||
}); | ||
|
||
test('finds a native node module when paths are given', () => { | ||
expect(require.resolve('fs', {paths: ['../dir']})).toEqual('fs'); | ||
}); | ||
|
||
test('throws an error if the module cannot be found from given paths', () => { | ||
expect(() => require.resolve('./mod.js', {paths: ['..']})).toThrowError( | ||
"Cannot resolve module './mod.js' from paths ['..'] from " | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
module.exports = 'mod'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
.startsWith(0)? Whatever is faster on latest V8
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.
Totally forgot that function exists, would be cleaner for sure.
Not sure if performance is that critical here (how often would a user call
require.resolve.paths
?) but I can try to find/run a benchmark later.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.
Okay so if you run it a lot,
[0] === '.'
is many times faster than.startsWith('.')
.I think because of this and because the
startsWith
only really simplifies the code a lot when called with a multi character string, we can leave it this way.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 did V8 define string access via bracket notation? If it works on Node 6, then we're fine with it.
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.
CI definitely passed on Node 6 last time.
I also think I've seen this being done somewhere else in the codebase already, let me check.
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.
Here from a few years ago, so this should not cause any Node version issues.