Skip to content

Commit 5188cf7

Browse files
committed
Make resolver more reliable
Due to my env issue, I implemented it in a wrong way, this fixes it. More detail: nodejs/node#1488
1 parent 98c93b1 commit 5188cf7

File tree

8 files changed

+87
-99
lines changed

8 files changed

+87
-99
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var bar = require('foo/bar.js')
5959
### Attentions
6060

6161
- Require hook will pollute `Module._load()`. You must be knowing this, and there's a `requere/deregister` to help you disable require hook.
62-
- `requere`'s require hook may conflict with other package's require hook, e.g. [`CoffeeScript`](http://coffeescript.org/), [`Babel`](https://babeljs.io/). Though my test looks fine when using `requere`, `CoffeeScript` and `Babel` together.
62+
- `requere`'s require hook may conflict with other package's require hook, e.g. [`CoffeeScript`](http://coffeescript.org/), [`Babel`](https://babeljs.io/). If you use `requere()` instead register hook, everything goes fine.
6363

6464
# Contributors
6565

lib/resolver.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
var Module = require('module')
12
var path = require('path')
23
var fs = require('fs')
34

4-
function exists (target) {
5-
var found = false
6-
7-
found = fs.existsSync(target)
8-
9-
if (!found && path.extname(target) === '') {
10-
found = fs.existsSync(target + '.js')
5+
function exists (target, extensions) {
6+
if (fs.existsSync(target)) {
7+
return target
118
}
129

13-
return found
10+
if (path.extname(target) === '') {
11+
for (var i = 0; i < extensions.length; i++) {
12+
var resolvedPath = target + extensions[i]
13+
if (fs.existsSync(resolvedPath)) {
14+
return resolvedPath
15+
}
16+
}
17+
}
1418
}
1519

1620
module.exports = function resolver (request, parent) {
@@ -20,17 +24,19 @@ module.exports = function resolver (request, parent) {
2024

2125
var resolvedPath
2226
var i = 0
27+
var extensions = Object.keys(Module._extensions)
2328

2429
for (i = 0; i < parent.paths.length; i++) {
25-
resolvedPath = path.resolve(parent.paths[i], request)
26-
if (fs.existsSync(resolvedPath)) {
30+
resolvedPath = exists(path.resolve(parent.paths[i], request), extensions)
31+
if (resolvedPath) {
2732
return resolvedPath
2833
}
2934
}
3035

3136
for (i = 0; i < parent.paths.length; i++) {
3237
resolvedPath = path.resolve(parent.paths[i].slice(0, parent.paths[i].lastIndexOf('node_modules')), request)
33-
if (exists(resolvedPath)) {
38+
resolvedPath = exists(resolvedPath, extensions)
39+
if (resolvedPath) {
3440
return resolvedPath
3541
}
3642
}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "./node_modules/eslint/bin/eslint.js .",
8-
"test": "node node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha --check-leaks test/*.js",
8+
"test": "node node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha --check-leaks test/standalone/index.js test/register/index.js",
99
"test-travis": "npm run lint && npm test"
1010
},
1111
"repository": {
@@ -19,7 +19,6 @@
1919
},
2020
"homepage": "https://github.com/chrisyip/requere",
2121
"devDependencies": {
22-
"babel": "^5.0.12",
2322
"chai": "^2.2.0",
2423
"coffee-script": "^1.9.1",
2524
"eslint": "^0.19.0",

test/index.js

-79
This file was deleted.

test/register/index.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var expect = require('chai').expect
2+
var path = require('path')
3+
4+
require('../../register.js')
5+
6+
describe('requere/register', function () {
7+
it('should require module correctly', function () {
8+
var _path = require('path')
9+
expect(path).to.equal(_path)
10+
})
11+
12+
it('should resolve module start from module root', function () {
13+
var res = require('test/textual/nested/nested')
14+
15+
expect(res).to.be.equal('foo.js in nested folders')
16+
})
17+
18+
it('should work with transipler', function () {
19+
require('coffee-script/register')
20+
21+
var b = require('test/textual/b.coffee')
22+
expect(b()).to.equal('Module B')
23+
})
24+
25+
it('should throw exception if module cannot found', function () {
26+
var func = function () {
27+
require('module_not_found')
28+
}
29+
expect(func).to.throw('Cannot find module \'module_not_found\'')
30+
})
31+
})

test/standalone/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var expect = require('chai').expect
2+
var requere = require('../../index.js')
3+
4+
describe('requere()', function () {
5+
it('should be a function', function () {
6+
expect(requere).to.be.a('function')
7+
})
8+
9+
it('should require module correctly', function () {
10+
var path = requere('path')
11+
expect(path).to.equal(require('path'))
12+
expect(require('../textual/a.js')).to.equal(requere('test/textual/a.js'))
13+
})
14+
15+
it('should resolve module start from module root', function () {
16+
var res1 = requere('test/textual/nested/nested')
17+
var res2 = require('../../test/textual/nested/nested.js')
18+
19+
expect(res1).to.be.equal(res2)
20+
})
21+
22+
it('should work with transipler', function () {
23+
require('coffee-script/register')
24+
25+
var b = requere('test/textual/b.coffee')
26+
expect(b()).to.equal('Module B')
27+
})
28+
29+
it('should throw exception if module cannot found', function () {
30+
var func = function () {
31+
requere('module_not_found')
32+
}
33+
expect(func).to.throw('Cannot find module \'module_not_found\'')
34+
})
35+
})

test/textual/es6.js

-5
This file was deleted.

test/textual/nested/nested.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
module.exports = require('test/textual/nested/foo.js')
1+
var requere = require('../../../index.js')
2+
module.exports = requere('test/textual/nested/foo.js')

0 commit comments

Comments
 (0)