Skip to content

Commit

Permalink
refactor: extract func
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Mar 27, 2021
1 parent e52eb18 commit ba5f9fc
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,21 +662,16 @@ function mapWithBrowserField(

for (const key in map) {
const normalizedKey = path.posix.normalize(key)
const isJsExt = normalizedKey.endsWith('.js')
const isIndexJs = normalizedKey.endsWith('/index.js')

const substitutions: string[] = [normalizedKey]

if (isJsExt) {
substitutions.push(normalizedKey.slice(0, -'.js'.length))
}

if (isIndexJs) {
substitutions.push(normalizedKey.slice(0, -'/index.js'.length))
}

if (substitutions.includes(normalizedPath)) {
if (
normalizedPath === normalizedKey ||
equalWithoutSuffix(normalizedPath, normalizedKey, '.js') ||
equalWithoutSuffix(normalizedPath, normalizedKey, '/index.js')
) {
return map[key]
}
}
}

function equalWithoutSuffix(path: string, key: string, suffix: string) {
return key.endsWith(suffix) && key.slice(0, -suffix.length) === path
}

0 comments on commit ba5f9fc

Please sign in to comment.