Skip to content

Commit

Permalink
fix: prevent cache on optional package resolve (#10812)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 7, 2022
1 parent 7a18b2e commit c599a2e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/vite/src/node/utils.ts
Expand Up @@ -951,12 +951,17 @@ export const requireResolveFromRootWithFallback = (
root: string,
id: string
): string => {
const paths = _require.resolve.paths?.(id) || []
// Search in the root directory first, and fallback to the default require paths.
const fallbackPaths = _require.resolve.paths?.(id) || []
const path = _require.resolve(id, {
paths: [root, ...fallbackPaths]
})
return path
paths.unshift(root)

// Use `resolve` package to check existence first, so if the package is not found,
// it won't be cached by nodejs, since there isn't a way to invalidate them:
// https://github.com/nodejs/node/issues/44663
resolve.sync(id, { basedir: root, paths })

// Use `require.resolve` again as the `resolve` package doesn't support the `exports` field
return _require.resolve(id, { paths })
}

// Based on node-graceful-fs
Expand Down

0 comments on commit c599a2e

Please sign in to comment.