Skip to content

Commit

Permalink
fix: resolve bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 22, 2021
1 parent 63186e5 commit e5946df
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/index.mjs
Expand Up @@ -29,12 +29,13 @@ const DEFAULT_CONDITIONS_SET = new Set(['node', 'import'])
const BUILُTIN_MODULES = new Set(builtinModules)
const DEFAULT_FROM = pathToFileURL(process.cwd())
const DEFAULT_EXTENSIONS = ['.mjs', '.cjs', '.js', '.json']
const NOT_FOUND_ERRORS = new Set(['ERR_MODULE_NOT_FOUND', 'ERR_UNSUPPORTED_DIR_IMPORT', 'MODULE_NOT_FOUND'])

function _tryModuleResolve (id, from, conditions, throwNonNotFound) {
function _tryModuleResolve (id, from, conditions) {
try {
return moduleResolve(id, from, conditions)
} catch (err) {
if (throwNonNotFound && !['MODULE_NOT_FOUND', 'ERR_UNSUPPORTED_DIR_IMPORT'].includes(err.code)) {
if (!NOT_FOUND_ERRORS.has(err.code)) {
throw err
}
return null
Expand All @@ -59,18 +60,16 @@ function _resolve (id, opts = {}) {
const from = opts.from ? normalizeid(opts.from) : DEFAULT_FROM

// Try simple resolve
// Might throw error if error is other than MODULE_NOT_FOUND
let resolved = _tryModuleResolve(id, from, conditionsSet, true)
let resolved = _tryModuleResolve(id, from, conditionsSet)

// Try other extensions if not found
if (!resolved) {
for (const prefix of ['', '/index']) {
for (const ext of DEFAULT_EXTENSIONS || opts.extensions) {
for (const ext of opts.extensions || DEFAULT_EXTENSIONS) {
resolved = _tryModuleResolve(id + prefix + ext, from, conditionsSet)
if (resolved) {
break
}
if (resolved) { break }
}
if (resolved) { break }
}
}

Expand Down

0 comments on commit e5946df

Please sign in to comment.