@@ -170,35 +170,12 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
170
170
scan : resolveOpts ?. scan ?? resolveOptions . scan ,
171
171
}
172
172
173
- const resolveSubpathImports = ( id : string , importer ?: string ) => {
174
- if ( ! importer || ! id . startsWith ( subpathImportsPrefix ) ) return
175
- const basedir = path . dirname ( importer )
176
- const pkgData = findNearestPackageData ( basedir , options . packageCache )
177
- if ( ! pkgData ) return
178
-
179
- let importsPath = resolveExportsOrImports (
180
- pkgData . data ,
181
- id ,
182
- options ,
183
- targetWeb ,
184
- 'imports' ,
185
- )
186
-
187
- if ( importsPath ?. [ 0 ] === '.' ) {
188
- importsPath = path . relative (
189
- basedir ,
190
- path . join ( pkgData . dir , importsPath ) ,
191
- )
192
-
193
- if ( importsPath [ 0 ] !== '.' ) {
194
- importsPath = `./${ importsPath } `
195
- }
196
- }
197
-
198
- return importsPath
199
- }
200
-
201
- const resolvedImports = resolveSubpathImports ( id , importer )
173
+ const resolvedImports = resolveSubpathImports (
174
+ id ,
175
+ importer ,
176
+ options ,
177
+ targetWeb ,
178
+ )
202
179
if ( resolvedImports ) {
203
180
id = resolvedImports
204
181
}
@@ -230,42 +207,14 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
230
207
return optimizedPath
231
208
}
232
209
233
- const ensureVersionQuery = ( resolved : string ) : string => {
234
- if (
235
- ! options . isBuild &&
236
- ! options . scan &&
237
- depsOptimizer &&
238
- ! (
239
- resolved === normalizedClientEntry ||
240
- resolved === normalizedEnvEntry
241
- )
242
- ) {
243
- // Ensure that direct imports of node_modules have the same version query
244
- // as if they would have been imported through a bare import
245
- // Use the original id to do the check as the resolved id may be the real
246
- // file path after symlinks resolution
247
- const isNodeModule =
248
- nodeModulesInPathRE . test ( normalizePath ( id ) ) ||
249
- nodeModulesInPathRE . test ( normalizePath ( resolved ) )
250
-
251
- if ( isNodeModule && ! resolved . match ( DEP_VERSION_RE ) ) {
252
- const versionHash = depsOptimizer . metadata . browserHash
253
- if ( versionHash && isOptimizable ( resolved , depsOptimizer . options ) ) {
254
- resolved = injectQuery ( resolved , `v=${ versionHash } ` )
255
- }
256
- }
257
- }
258
- return resolved
259
- }
260
-
261
210
// explicit fs paths that starts with /@fs /*
262
211
if ( asSrc && id . startsWith ( FS_PREFIX ) ) {
263
212
const fsPath = fsPathFromId ( id )
264
213
res = tryFsResolve ( fsPath , options )
265
214
isDebug && debug ( `[@fs] ${ colors . cyan ( id ) } -> ${ colors . dim ( res ) } ` )
266
215
// always return here even if res doesn't exist since /@fs/ is explicit
267
216
// if the file doesn't exist it should be a 404
268
- return ensureVersionQuery ( res || fsPath )
217
+ return ensureVersionQuery ( res || fsPath , id , options , depsOptimizer )
269
218
}
270
219
271
220
// URL
@@ -274,7 +223,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
274
223
const fsPath = path . resolve ( root , id . slice ( 1 ) )
275
224
if ( ( res = tryFsResolve ( fsPath , options ) ) ) {
276
225
isDebug && debug ( `[url] ${ colors . cyan ( id ) } -> ${ colors . dim ( res ) } ` )
277
- return ensureVersionQuery ( res )
226
+ return ensureVersionQuery ( res , id , options , depsOptimizer )
278
227
}
279
228
}
280
229
@@ -314,7 +263,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
314
263
}
315
264
316
265
if ( ( res = tryFsResolve ( fsPath , options ) ) ) {
317
- res = ensureVersionQuery ( res )
266
+ res = ensureVersionQuery ( res , id , options , depsOptimizer )
318
267
isDebug &&
319
268
debug ( `[relative] ${ colors . cyan ( id ) } -> ${ colors . dim ( res ) } ` )
320
269
const pkg = importer != null && idToPkgMap . get ( importer )
@@ -336,7 +285,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
336
285
if ( ( res = tryFsResolve ( fsPath , options ) ) ) {
337
286
isDebug &&
338
287
debug ( `[drive-relative] ${ colors . cyan ( id ) } -> ${ colors . dim ( res ) } ` )
339
- return ensureVersionQuery ( res )
288
+ return ensureVersionQuery ( res , id , options , depsOptimizer )
340
289
}
341
290
}
342
291
@@ -346,7 +295,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
346
295
( res = tryFsResolve ( id , options ) )
347
296
) {
348
297
isDebug && debug ( `[fs] ${ colors . cyan ( id ) } -> ${ colors . dim ( res ) } ` )
349
- return ensureVersionQuery ( res )
298
+ return ensureVersionQuery ( res , id , options , depsOptimizer )
350
299
}
351
300
352
301
// external
@@ -467,6 +416,66 @@ export default new Proxy({}, {
467
416
}
468
417
}
469
418
419
+ function resolveSubpathImports (
420
+ id : string ,
421
+ importer : string | undefined ,
422
+ options : InternalResolveOptions ,
423
+ targetWeb : boolean ,
424
+ ) {
425
+ if ( ! importer || ! id . startsWith ( subpathImportsPrefix ) ) return
426
+ const basedir = path . dirname ( importer )
427
+ const pkgData = findNearestPackageData ( basedir , options . packageCache )
428
+ if ( ! pkgData ) return
429
+
430
+ let importsPath = resolveExportsOrImports (
431
+ pkgData . data ,
432
+ id ,
433
+ options ,
434
+ targetWeb ,
435
+ 'imports' ,
436
+ )
437
+
438
+ if ( importsPath ?. [ 0 ] === '.' ) {
439
+ importsPath = path . relative ( basedir , path . join ( pkgData . dir , importsPath ) )
440
+
441
+ if ( importsPath [ 0 ] !== '.' ) {
442
+ importsPath = `./${ importsPath } `
443
+ }
444
+ }
445
+
446
+ return importsPath
447
+ }
448
+
449
+ function ensureVersionQuery (
450
+ resolved : string ,
451
+ id : string ,
452
+ options : InternalResolveOptions ,
453
+ depsOptimizer ?: DepsOptimizer ,
454
+ ) : string {
455
+ if (
456
+ ! options . isBuild &&
457
+ ! options . scan &&
458
+ depsOptimizer &&
459
+ ! ( resolved === normalizedClientEntry || resolved === normalizedEnvEntry )
460
+ ) {
461
+ // Ensure that direct imports of node_modules have the same version query
462
+ // as if they would have been imported through a bare import
463
+ // Use the original id to do the check as the resolved id may be the real
464
+ // file path after symlinks resolution
465
+ const isNodeModule =
466
+ nodeModulesInPathRE . test ( normalizePath ( id ) ) ||
467
+ nodeModulesInPathRE . test ( normalizePath ( resolved ) )
468
+
469
+ if ( isNodeModule && ! resolved . match ( DEP_VERSION_RE ) ) {
470
+ const versionHash = depsOptimizer . metadata . browserHash
471
+ if ( versionHash && isOptimizable ( resolved , depsOptimizer . options ) ) {
472
+ resolved = injectQuery ( resolved , `v=${ versionHash } ` )
473
+ }
474
+ }
475
+ }
476
+ return resolved
477
+ }
478
+
470
479
function splitFileAndPostfix ( path : string ) {
471
480
let file = path
472
481
let postfix = ''
0 commit comments