Skip to content

Commit

Permalink
Fix cannot resolve parent with using with other packages
Browse files Browse the repository at this point in the history
e.g., mods.forEach(mod => requere(`path/to/${mod}`))
  • Loading branch information
chrisyip committed Apr 18, 2016
1 parent a8a96d7 commit 8c6f332
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const resolver = require('./lib/resolver.js')
const extensions = require('./lib/extensions')

const getCWD = require('./lib/get-cwd')
const getParent = require('./lib/get-parent')

function resolvedByRequire (request) {
try {
Expand All @@ -28,7 +28,7 @@ function requere (request, onlySupportedExtname) {
return mod
}

return resolver(request, getCWD(__filename, new Error, request[0] === '.'), onlySupportedExtname)
return resolver(request, getParent(__filename, new Error, request[0] === '.'), onlySupportedExtname)
}

requere.register = (ext, loader) => {
Expand Down
19 changes: 15 additions & 4 deletions lib/get-cwd.js → lib/get-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

const path = require('path')

function getFilename (input) {
const firstSepIndex = input.indexOf(path.sep)
const lastSpaceIndex = input.lastIndexOf(' ', firstSepIndex)

return input.slice(lastSpaceIndex)
}

module.exports = (parentFilename, e, relative) => {
const stack = e.stack.split('\n')

Expand All @@ -10,21 +17,25 @@ module.exports = (parentFilename, e, relative) => {
let parent

for (let dir of stack.slice(index + 1)) {
let match = dir.match(/\((.+):\d+:\d+\)?$/)
let match = getFilename(dir).match(/([^(]+):\d+:\d+\)?$/)

if (match) {
match = match[1].trim()
}

if (!match || !path.isAbsolute(match[1])) {
if (!match || !path.isAbsolute(match)) {
continue
}

parent = match[1]
parent = match
break
}

if (relative) {
return path.dirname(parent)
}

const lastIndex = parent && parent.split('/').lastIndexOf('node_modules')
const lastIndex = parent && parent.split(path.sep).lastIndexOf('node_modules')

if (lastIndex > -1) {
return parent.slice(0, lastIndex + 1)
Expand Down

0 comments on commit 8c6f332

Please sign in to comment.