Skip to content
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

module: skip directories known not to exist #9196

Merged
merged 3 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,17 @@ However, in practice, there are much better ways to do this, such as
loading modules via some other Node.js program, or compiling them to
JavaScript ahead of time.

Since the Module system is locked, this feature will probably never go
Since the module system is locked, this feature will probably never go
away. However, it may have subtle bugs and complexities that are best
left untouched.

Note that the number of file system operations that the module system
has to perform in order to resolve a `require(...)` statement to a
filename scales linearly with the number of registered extensions.

In other words, adding extensions slows down the module loader and
should be discouraged.

### require.resolve()
<!-- YAML
added: v0.3.0
Expand Down
6 changes: 3 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ Module._findPath = function(request, paths, isMain) {
var basePath = path.resolve(curPath, request);
var filename;

const rc = stat(basePath);
if (!trailingSlash) {
const rc = stat(basePath);
if (rc === 0) { // File.
if (preserveSymlinks && !isMain) {
filename = path.resolve(basePath);
Expand All @@ -193,13 +193,13 @@ Module._findPath = function(request, paths, isMain) {
}
}

if (!filename) {
if (!filename && rc === 1) { // Directory.
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain);
}

if (!filename) {
if (!filename && rc === 1) { // Directory.
// try it with each of the extensions at "index"
if (exts === undefined)
exts = Object.keys(Module._extensions);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/packages/index/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.ok = 'ok';
1 change: 1 addition & 0 deletions test/fixtures/packages/index/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ assert.equal(threeFolder, threeIndex);
assert.notEqual(threeFolder, three);

console.error('test package.json require() loading');
assert.equal(require('../fixtures/packages/index').ok, 'ok',
Copy link
Member

@jasnell jasnell Oct 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strictEqual?
I know equal is consistent with the rest of the test but updating the test may be worthwhile

Copy link
Member Author

@bnoordhuis bnoordhuis Oct 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the style of the surrounding code here.

EDIT: Never mind, didn't see you edited your comment. Still, consistency is important, right?

Copy link
Member Author

@bnoordhuis bnoordhuis Oct 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the test en bloc in a separate pull request.

EDIT: #9263

'Failed loading package');
assert.equal(require('../fixtures/packages/main').ok, 'ok',
'Failed loading package');
assert.equal(require('../fixtures/packages/main-index').ok, 'ok',
Expand Down Expand Up @@ -208,6 +210,7 @@ assert.deepStrictEqual(children, {
},
'fixtures/nested-index/three.js': {},
'fixtures/nested-index/three/index.js': {},
'fixtures/packages/index/index.js': {},
'fixtures/packages/main/package-main-module.js': {},
'fixtures/packages/main-index/package-main-module/index.js': {},
'fixtures/cycles/root.js': {
Expand Down