Skip to content

Commit

Permalink
fix: incorrectly resolving knownJsSrcRE files from root (fixes #4161)…
Browse files Browse the repository at this point in the history
… (#8808)
  • Loading branch information
sapphi-red committed Jun 28, 2022
1 parent 895a7d6 commit e1e426e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -24,6 +24,7 @@ import {
isDataUrl,
isExternalUrl,
isFileReadable,
isNonDriveRelativeAbsolutePath,
isObject,
isPossibleTsOutput,
isTsRequest,
Expand Down Expand Up @@ -172,8 +173,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
// relative
if (
id.startsWith('.') ||
(preferRelative && /^\w/.test(id)) ||
importer?.endsWith('.html')
((preferRelative || importer?.endsWith('.html')) && /^\w/.test(id))
) {
const basedir = importer ? path.dirname(importer) : process.cwd()
const fsPath = path.resolve(basedir, id)
Expand Down Expand Up @@ -239,7 +239,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

// absolute fs paths
if (path.isAbsolute(id) && (res = tryFsResolve(id, options))) {
if (
isNonDriveRelativeAbsolutePath(id) &&
(res = tryFsResolve(id, options))
) {
isDebug && debug(`[fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
}
Expand Down
11 changes: 11 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -1063,3 +1063,14 @@ export function stripBomTag(content: string): string {
}

export const isTS = (filename: string): boolean => /\.[cm]?ts$/.test(filename)

const windowsDrivePathPrefixRE = /^[A-Za-z]:[/\\]/

/**
* path.isAbsolute also returns true for drive relative paths on windows (e.g. /something)
* this function returns false for them but true for absolute paths (e.g. C:/something)
*/
export const isNonDriveRelativeAbsolutePath = (p: string): boolean => {
if (!isWindows) return p.startsWith('/')
return windowsDrivePathPrefixRE.test(p)
}

0 comments on commit e1e426e

Please sign in to comment.