Skip to content

Commit

Permalink
refactor: tryStatSync as util (#12575)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 25, 2023
1 parent 8055536 commit 92601db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
9 changes: 4 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -23,6 +23,7 @@ import {
removeDir,
removeLeadingSlash,
renameDir,
tryStatSync,
writeFile,
} from '../utils'
import { transformWithEsbuild } from '../plugins/esbuild'
Expand Down Expand Up @@ -1212,11 +1213,9 @@ export function getDepHash(config: ResolvedConfig, ssr: boolean): string {
if (checkPatches) {
// Default of https://github.com/ds300/patch-package
const fullPath = path.join(path.dirname(lockfilePath), 'patches')
if (fs.existsSync(fullPath)) {
const stats = fs.statSync(fullPath)
if (stats.isDirectory()) {
content += stats.mtimeMs.toString()
}
const stat = tryStatSync(fullPath)
if (stat?.isDirectory()) {
content += stat.mtimeMs.toString()
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -38,6 +38,7 @@ import {
resolveFrom,
safeRealpathSync,
slash,
tryStatSync,
} from '../utils'
import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer'
import type { DepsOptimizer } from '../optimizer'
Expand Down Expand Up @@ -648,14 +649,6 @@ function tryResolveRealFileWithExtensions(
}
}

function tryStatSync(file: string): fs.Stats | undefined {
try {
return fs.statSync(file, { throwIfNoEntry: false })
} catch {
// Ignore errors
}
}

export type InternalResolveOptionsWithOverrideConditions =
InternalResolveOptions & {
/**
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -392,6 +392,13 @@ export function isDefined<T>(value: T | undefined | null): value is T {
return value != null
}

export function tryStatSync(file: string): fs.Stats | undefined {
try {
return fs.statSync(file, { throwIfNoEntry: false })
} catch {
// Ignore errors
}
}
interface LookupFileOptions {
pathOnly?: boolean
rootDir?: string
Expand All @@ -405,7 +412,7 @@ export function lookupFile(
): string | undefined {
for (const format of formats) {
const fullPath = path.join(dir, format)
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
if (tryStatSync(fullPath)?.isFile()) {
const result = options?.pathOnly
? fullPath
: fs.readFileSync(fullPath, 'utf-8')
Expand Down

0 comments on commit 92601db

Please sign in to comment.