Skip to content

Commit

Permalink
Make resolver more reliable
Browse files Browse the repository at this point in the history
Due to my env issue, I implemented it in a wrong way, this fixes it. More detail: nodejs/node#1488
  • Loading branch information
chrisyip committed Apr 21, 2015
1 parent 98c93b1 commit 5188cf7
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 99 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var bar = require('foo/bar.js')
### Attentions

- Require hook will pollute `Module._load()`. You must be knowing this, and there's a `requere/deregister` to help you disable require hook.
- `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.
- `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.

# Contributors

Expand Down
28 changes: 17 additions & 11 deletions lib/resolver.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
var Module = require('module')
var path = require('path')
var fs = require('fs')

function exists (target) {
var found = false

found = fs.existsSync(target)

if (!found && path.extname(target) === '') {
found = fs.existsSync(target + '.js')
function exists (target, extensions) {
if (fs.existsSync(target)) {
return target
}

return found
if (path.extname(target) === '') {
for (var i = 0; i < extensions.length; i++) {
var resolvedPath = target + extensions[i]
if (fs.existsSync(resolvedPath)) {
return resolvedPath
}
}
}
}

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

var resolvedPath
var i = 0
var extensions = Object.keys(Module._extensions)

for (i = 0; i < parent.paths.length; i++) {
resolvedPath = path.resolve(parent.paths[i], request)
if (fs.existsSync(resolvedPath)) {
resolvedPath = exists(path.resolve(parent.paths[i], request), extensions)
if (resolvedPath) {
return resolvedPath
}
}

for (i = 0; i < parent.paths.length; i++) {
resolvedPath = path.resolve(parent.paths[i].slice(0, parent.paths[i].lastIndexOf('node_modules')), request)
if (exists(resolvedPath)) {
resolvedPath = exists(resolvedPath, extensions)
if (resolvedPath) {
return resolvedPath
}
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "./node_modules/eslint/bin/eslint.js .",
"test": "node node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha --check-leaks test/*.js",
"test": "node node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha --check-leaks test/standalone/index.js test/register/index.js",
"test-travis": "npm run lint && npm test"
},
"repository": {
Expand All @@ -19,7 +19,6 @@
},
"homepage": "https://github.com/chrisyip/requere",
"devDependencies": {
"babel": "^5.0.12",
"chai": "^2.2.0",
"coffee-script": "^1.9.1",
"eslint": "^0.19.0",
Expand Down
79 changes: 0 additions & 79 deletions test/index.js

This file was deleted.

31 changes: 31 additions & 0 deletions test/register/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var expect = require('chai').expect
var path = require('path')

require('../../register.js')

describe('requere/register', function () {
it('should require module correctly', function () {
var _path = require('path')
expect(path).to.equal(_path)
})

it('should resolve module start from module root', function () {
var res = require('test/textual/nested/nested')

expect(res).to.be.equal('foo.js in nested folders')
})

it('should work with transipler', function () {
require('coffee-script/register')

var b = require('test/textual/b.coffee')
expect(b()).to.equal('Module B')
})

it('should throw exception if module cannot found', function () {
var func = function () {
require('module_not_found')
}
expect(func).to.throw('Cannot find module \'module_not_found\'')
})
})
35 changes: 35 additions & 0 deletions test/standalone/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var expect = require('chai').expect
var requere = require('../../index.js')

describe('requere()', function () {
it('should be a function', function () {
expect(requere).to.be.a('function')
})

it('should require module correctly', function () {
var path = requere('path')
expect(path).to.equal(require('path'))
expect(require('../textual/a.js')).to.equal(requere('test/textual/a.js'))
})

it('should resolve module start from module root', function () {
var res1 = requere('test/textual/nested/nested')
var res2 = require('../../test/textual/nested/nested.js')

expect(res1).to.be.equal(res2)
})

it('should work with transipler', function () {
require('coffee-script/register')

var b = requere('test/textual/b.coffee')
expect(b()).to.equal('Module B')
})

it('should throw exception if module cannot found', function () {
var func = function () {
requere('module_not_found')
}
expect(func).to.throw('Cannot find module \'module_not_found\'')
})
})
5 changes: 0 additions & 5 deletions test/textual/es6.js

This file was deleted.

3 changes: 2 additions & 1 deletion test/textual/nested/nested.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module.exports = require('test/textual/nested/foo.js')
var requere = require('../../../index.js')
module.exports = requere('test/textual/nested/foo.js')

0 comments on commit 5188cf7

Please sign in to comment.