@@ -3,7 +3,7 @@ import os from 'node:os'
3
3
import path from 'node:path'
4
4
import { exec } from 'node:child_process'
5
5
import { createHash } from 'node:crypto'
6
- import { URL , URLSearchParams } from 'node:url'
6
+ import { URL , URLSearchParams , fileURLToPath } from 'node:url'
7
7
import { builtinModules , createRequire } from 'node:module'
8
8
import { promises as dns } from 'node:dns'
9
9
import { performance } from 'node:perf_hooks'
@@ -34,6 +34,7 @@ import {
34
34
import type { DepOptimizationConfig } from './optimizer'
35
35
import type { ResolvedConfig } from './config'
36
36
import type { ResolvedServerUrls , ViteDevServer } from './server'
37
+ import { resolvePackageData } from './packages'
37
38
import type { CommonServerOptions } from '.'
38
39
39
40
/**
@@ -966,21 +967,25 @@ export function getHash(text: Buffer | string): string {
966
967
return createHash ( 'sha256' ) . update ( text ) . digest ( 'hex' ) . substring ( 0 , 8 )
967
968
}
968
969
970
+ const _dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
971
+
969
972
export const requireResolveFromRootWithFallback = (
970
973
root : string ,
971
974
id : string ,
972
975
) : string => {
973
- const paths = _require . resolve . paths ?.( id ) || [ ]
974
- // Search in the root directory first, and fallback to the default require paths.
975
- paths . unshift ( root )
976
-
977
- // Use `resolve` package to check existence first, so if the package is not found,
976
+ // check existence first, so if the package is not found,
978
977
// it won't be cached by nodejs, since there isn't a way to invalidate them:
979
978
// https://github.com/nodejs/node/issues/44663
980
- resolve . sync ( id , { basedir : root , paths } )
979
+ const found = resolvePackageData ( id , root ) || resolvePackageData ( id , _dirname )
980
+ if ( ! found ) {
981
+ const error = new Error ( `${ JSON . stringify ( id ) } not found.` )
982
+ ; ( error as any ) . code = 'MODULE_NOT_FOUND'
983
+ throw error
984
+ }
981
985
982
- // Use `require.resolve` again as the `resolve` package doesn't support the `exports` field
983
- return _require . resolve ( id , { paths } )
986
+ // actually resolve
987
+ // Search in the root directory first, and fallback to the default require paths.
988
+ return _require . resolve ( id , { paths : [ root , _dirname ] } )
984
989
}
985
990
986
991
export function emptyCssComments ( raw : string ) : string {
0 commit comments