Skip to content

Commit

Permalink
fix: normalize path case on case insensitive file systems; remove / b…
Browse files Browse the repository at this point in the history
…efore drive letter from allow dirs on windows
  • Loading branch information
dominikg committed Feb 8, 2022
1 parent cebc044 commit 919cea0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -33,7 +33,7 @@ import { timeMiddleware } from './middlewares/time'
import type { ModuleNode } from './moduleGraph'
import { ModuleGraph } from './moduleGraph'
import type { Connect } from 'types/connect'
import { ensureLeadingSlash, normalizePath } from '../utils'
import { normalizePath } from '../utils'
import { errorMiddleware, prepareError } from './middlewares/error'
import type { HmrOptions } from './hmr'
import { handleHMRUpdate, handleFileAddUnlink } from './hmr'
Expand Down Expand Up @@ -693,7 +693,7 @@ function createServerCloseFn(server: http.Server | null) {
}

function resolvedAllowDir(root: string, dir: string): string {
return ensureLeadingSlash(normalizePath(path.resolve(root, dir)))
return normalizePath(path.resolve(root, dir))
}

export function resolveServerOptions(
Expand Down
30 changes: 28 additions & 2 deletions packages/vite/src/node/utils.ts
Expand Up @@ -9,7 +9,8 @@ import {
DEFAULT_EXTENSIONS,
VALID_ID_PREFIX,
CLIENT_PUBLIC_PATH,
ENV_PUBLIC_PATH
ENV_PUBLIC_PATH,
CLIENT_ENTRY
} from './constants'
import resolve from 'resolve'
import { builtinModules } from 'module'
Expand Down Expand Up @@ -139,11 +140,36 @@ export function createDebugger(
}
}

function testCaseInsensitiveFS() {
if (!CLIENT_ENTRY.endsWith('client.mjs')) {
throw new Error(
`cannot test case insensitive FS, CLIENT_ENTRY const doesn't contain client.mjs`
)
}
if (!fs.existsSync(CLIENT_ENTRY)) {
throw new Error(
'cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file: ' +
CLIENT_ENTRY
)
}
return fs.existsSync(CLIENT_ENTRY.replace('client.mjs', 'cLiEnT.mjs'))
}

export const isCaseInsensitiveFS = testCaseInsensitiveFS()

export const isWindows = os.platform() === 'win32'

const VOLUME_RE = /^[A-Z]:/i

export function normalizePath(id: string): string {
return path.posix.normalize(isWindows ? slash(id) : id)
let normalized = path.normalize(id)
if (isCaseInsensitiveFS) {
normalized = normalized.toLowerCase()
}
if (isWindows) {
normalized = slash(normalized)
}
return normalized
}

export function fsPathFromId(id: string): string {
Expand Down

0 comments on commit 919cea0

Please sign in to comment.