@@ -2,7 +2,6 @@ import path from 'node:path'
2
2
import { pathToFileURL } from 'node:url'
3
3
import type { ViteDevServer } from '../server'
4
4
import {
5
- bareImportRE ,
6
5
dynamicImport ,
7
6
isBuiltin ,
8
7
unwrapId ,
@@ -11,7 +10,6 @@ import {
11
10
import { transformRequest } from '../server/transformRequest'
12
11
import type { InternalResolveOptions } from '../plugins/resolve'
13
12
import { tryNodeResolve } from '../plugins/resolve'
14
- import { hookNodeResolve } from '../plugins/ssrRequireHook'
15
13
import {
16
14
ssrDynamicImportKey ,
17
15
ssrExportAllKey ,
@@ -114,21 +112,16 @@ async function instantiateModule(
114
112
root
115
113
} = server . config
116
114
117
- // The `extensions` and `mainFields` options are used to ensure that
118
- // CommonJS modules are preferred. We want to avoid ESM->ESM imports
119
- // whenever possible, because `hookNodeResolve` can't intercept them.
120
115
const resolveOptions : InternalResolveOptions = {
121
116
mainFields : [ 'main' ] ,
122
117
browserField : true ,
123
118
conditions : [ ] ,
124
119
extensions : [ '.js' , '.cjs' , '.json' ] ,
125
120
dedupe,
126
121
preserveSymlinks,
127
- isBuild : true ,
122
+ isBuild : false ,
128
123
isProduction,
129
- isRequire : true ,
130
- root,
131
- isHookNodeResolve : true
124
+ root
132
125
}
133
126
134
127
// Since dynamic imports can happen in parallel, we need to
@@ -227,96 +220,44 @@ async function instantiateModule(
227
220
return Object . freeze ( ssrModule )
228
221
}
229
222
230
- // `nodeImport` may run in parallel on multiple `ssrLoadModule` calls.
231
- // We keep track of the current importing count so that the first import
232
- // would `hookNodeResolve`, and the last import would `unhookNodeResolve`.
233
- let importingCount = 0
234
- let unhookNodeResolve : ReturnType < typeof hookNodeResolve > | undefined
235
-
236
223
// In node@12+ we can use dynamic import to load CJS and ESM
237
224
async function nodeImport (
238
225
id : string ,
239
226
importer : string ,
240
227
resolveOptions : InternalResolveOptions
241
228
) {
242
- // Node's module resolution is hi-jacked so Vite can ensure the
243
- // configured `resolve.dedupe` and `mode` options are respected.
244
- const viteResolve = (
245
- id : string ,
246
- importer : string ,
247
- options = resolveOptions
248
- ) => {
249
- const resolved = tryNodeResolve ( id , importer , options , false )
250
- if ( ! resolved ) {
251
- const err : any = new Error (
252
- `Cannot find module '${ id } ' imported from '${ importer } '`
253
- )
254
- err . code = 'ERR_MODULE_NOT_FOUND'
255
- throw err
256
- }
257
- return resolved . id
258
- }
259
-
260
- if ( importingCount === 0 ) {
261
- // When an ESM module imports an ESM dependency, this hook is *not* used.
262
- unhookNodeResolve = hookNodeResolve (
263
- ( nodeResolve ) => ( id , parent , isMain , options ) => {
264
- // Use the Vite resolver only for bare imports while skipping
265
- // any absolute paths, built-in modules and binary modules.
266
- if (
267
- ! bareImportRE . test ( id ) ||
268
- path . isAbsolute ( id ) ||
269
- isBuiltin ( id ) ||
270
- id . endsWith ( '.node' )
271
- ) {
272
- return nodeResolve ( id , parent , isMain , options )
273
- }
274
- if ( parent ) {
275
- let resolved = viteResolve ( id , parent . id )
276
- if ( resolved ) {
277
- // hookNodeResolve must use platform-specific path.normalize
278
- // to be compatible with dynamicImport (#6080)
279
- resolved = path . normalize ( resolved )
280
- }
281
- return resolved
282
- }
283
- // Importing a CJS module from an ESM module. In this case, the import
284
- // specifier is already an absolute path, so this is a no-op.
285
- // Options like `resolve.dedupe` and `mode` are not respected.
286
- return id
287
- }
288
- )
289
- }
290
-
291
229
let url : string
292
230
if ( id . startsWith ( 'node:' ) || isBuiltin ( id ) ) {
293
231
url = id
294
232
} else {
295
- url = viteResolve (
233
+ const resolved = tryNodeResolve (
296
234
id ,
297
235
importer ,
298
236
// Non-external modules can import ESM-only modules, but only outside
299
237
// of test runs, because we use Node `require` in Jest to avoid segfault.
300
238
// @ts -expect-error
301
239
typeof jest === 'undefined'
302
240
? { ...resolveOptions , tryEsmOnly : true }
303
- : resolveOptions
241
+ : resolveOptions ,
242
+ false
304
243
)
244
+ if ( ! resolved ) {
245
+ const err : any = new Error (
246
+ `Cannot find module '${ id } ' imported from '${ importer } '`
247
+ )
248
+ err . code = 'ERR_MODULE_NOT_FOUND'
249
+ throw err
250
+ }
251
+ url = resolved . id
305
252
if ( usingDynamicImport ) {
306
253
url = pathToFileURL ( url ) . toString ( )
307
254
}
308
255
}
309
256
310
257
try {
311
- importingCount ++
312
258
const mod = await dynamicImport ( url )
313
259
return proxyESM ( mod )
314
- } finally {
315
- importingCount --
316
- if ( importingCount === 0 ) {
317
- unhookNodeResolve ?.( )
318
- }
319
- }
260
+ } catch { }
320
261
}
321
262
322
263
// rollup-style default import interop for cjs
0 commit comments