Skip to content

Commit

Permalink
fix: prevent cache on optional package resolve (v3) (#10812) (#10845)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 9, 2022
1 parent d9f6dc0 commit 3ba45b9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
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 3ba45b9

Please sign in to comment.