Skip to content

Commit

Permalink
fix: should apply full ext resolve on module entries
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 28, 2020
1 parent c8ee484 commit 63b0e3c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
64 changes: 42 additions & 22 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface InternalResolver {
requestToFile(publicPath: string): string
fileToRequest(filePath: string): string
alias(id: string): string | undefined
resolveExt(publicPath: string): string | undefined
}

export const supportedExts = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
Expand Down Expand Up @@ -68,7 +69,7 @@ const isFile = (file: string): boolean => {
}
}

export const resolveExt = (id: string) => {
const resolveExt = (id: string): string | undefined => {
const cleanId = cleanUrl(id)
if (!isFile(cleanId)) {
let inferredExt = ''
Expand All @@ -87,41 +88,58 @@ export const resolveExt = (id: string) => {
const resolved = cleanId + inferredExt + query
if (resolved !== id) {
debug(`(extension) ${id} -> ${resolved}`)
return inferredExt
}
return resolved
}
return id
}

export function createResolver(
root: string,
resolvers: Resolver[] = [],
alias: Record<string, string> = {}
): InternalResolver {
return {
requestToFile: (publicPath) => {
let resolved: string | undefined
for (const r of resolvers) {
const filepath = r.requestToFile && r.requestToFile(publicPath, root)
if (filepath) {
resolved = filepath
break
}
}
if (!resolved) {
resolved = defaultRequestToFile(publicPath, root)
function resolveRequest(
publicPath: string
): {
filePath: string
ext: string | undefined
} {
let resolved: string | undefined
for (const r of resolvers) {
const filepath = r.requestToFile && r.requestToFile(publicPath, root)
if (filepath) {
resolved = filepath
break
}
resolved = resolveExt(resolved)
return resolved
}
if (!resolved) {
resolved = defaultRequestToFile(publicPath, root)
}
const ext = resolveExt(resolved)
return {
filePath: ext ? resolved + ext : resolved,
ext
}
}

return {
requestToFile(publicPath) {
return resolveRequest(publicPath).filePath
},

resolveExt(publicPath) {
return resolveRequest(publicPath).ext
},
fileToRequest: (filePath) => {

fileToRequest(filePath) {
for (const r of resolvers) {
const request = r.fileToRequest && r.fileToRequest(filePath, root)
if (request) return request
}
return defaultFileToRequest(filePath, root)
},
alias: (id: string) => {

alias(id) {
let aliased: string | undefined = alias[id]
if (aliased) {
return aliased
Expand Down Expand Up @@ -268,10 +286,12 @@ export function resolveNodeModule(
let entryFilePath: string | null = null
if (entryPoint) {
// #284 some packages specify entry without extension...
if (!path.extname(entryPoint)) {
entryPoint += '.js'
}
entryFilePath = path.join(path.dirname(pkgPath), entryPoint!)
const ext = resolveExt(entryFilePath)
if (ext) {
entryPoint += ext
entryFilePath += ext
}
entryPoint = path.posix.join(id, entryPoint!)
// save the resolved file path now so we don't need to do it again in
// resolveNodeModuleFile()
Expand Down
15 changes: 3 additions & 12 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
resolveRelativeRequest
} from '../utils'
import chalk from 'chalk'
import slash from 'slash'
import { moduleRE } from './serverPluginModuleResolve'

const debug = require('debug')('vite:rewrite')
Expand Down Expand Up @@ -228,8 +227,6 @@ export function rewriteImports(
}

const bareImportRE = /^[^\/\.]/
const indexRE = /\/index\.\w+$/
const indexRemoveRE = /\/index(\.\w+)?$/

export const resolveImport = (
root: string,
Expand All @@ -255,15 +252,9 @@ export const resolveImport = (
}

// 3. resolve extensions.
const file = slash(resolver.requestToFile(pathname))
const resolvedExt = path.extname(file)
if (resolvedExt !== path.extname(pathname)) {
const indexMatch = file.match(indexRE)
if (indexMatch) {
pathname = pathname.replace(indexRemoveRE, '') + indexMatch[0]
} else {
pathname += resolvedExt
}
const ext = resolver.resolveExt(pathname)
if (ext) {
pathname += ext
}

// 4. mark non-src imports
Expand Down

0 comments on commit 63b0e3c

Please sign in to comment.