@@ -3,6 +3,7 @@ import { resolveModulePath } from 'exsolve'
33import { isAbsolute , normalize , resolve } from 'pathe'
44import type { Environment , Plugin } from 'vite'
55import { directoryToURL , resolveAlias } from '@nuxt/kit'
6+ import escapeRE from 'escape-string-regexp'
67import type { Nuxt } from '@nuxt/schema'
78
89import { pkgDir } from '../../dirs'
@@ -33,47 +34,58 @@ export function ResolveDeepImportsPlugin (nuxt: Nuxt): Plugin {
3334return {
3435name :'nuxt:resolve-bare-imports' ,
3536enforce :'post' ,
36- async resolveId ( id , importer ) {
37- if ( ! importer || isAbsolute ( id ) || ( ! isAbsolute ( importer ) && ! VIRTUAL_RE . test ( importer ) ) || exclude . some ( e => id . startsWith ( e ) ) ) {
38- return
39- }
37+ resolveId :{
38+ filter :{
39+ id :{
40+ exclude :[
41+ // absolute path
42+ / ^ [ / \\ ] (? ! [ / \\ ] ) | ^ [ / \\ ] { 2 } (? ! \. ) | ^ [ A - Z ] : [ / \\ ] / i,
43+ ...exclude . map ( e => new RegExp ( '^' + escapeRE ( e ) ) ) ,
44+ ] ,
45+ } ,
46+ } ,
47+ async handler ( id , importer ) {
48+ if ( ! importer || ( ! isAbsolute ( importer ) && ! VIRTUAL_RE . test ( importer ) ) ) {
49+ return
50+ }
4051
41- const normalisedId = resolveAlias ( normalize ( id ) , nuxt . options . alias )
42- const isNuxtTemplate = importer . startsWith ( 'virtual:nuxt' )
43- const normalisedImporter = ( isNuxtTemplate ?decodeURIComponent ( importer ) :importer ) . replace ( VIRTUAL_RE , '' )
52+ const normalisedId = resolveAlias ( normalize ( id ) , nuxt . options . alias )
53+ const isNuxtTemplate = importer . startsWith ( 'virtual:nuxt' )
54+ const normalisedImporter = ( isNuxtTemplate ?decodeURIComponent ( importer ) :importer ) . replace ( VIRTUAL_RE , '' )
4455
45- if ( nuxt . options . experimental . templateImportResolution !== false && isNuxtTemplate ) {
46- const template = nuxt . options . build . templates . find ( t => resolve ( nuxt . options . buildDir , t . filename ! ) === normalisedImporter )
47- if ( template ?. _path ) {
48- const res = await this . resolve ?.( normalisedId , template . _path , { skipSelf :true } )
49- if ( res !== undefined && res !== null ) {
50- return res
56+ if ( nuxt . options . experimental . templateImportResolution !== false && isNuxtTemplate ) {
57+ const template = nuxt . options . build . templates . find ( t => resolve ( nuxt . options . buildDir , t . filename ! ) === normalisedImporter )
58+ if ( template ?. _path ) {
59+ const res = await this . resolve ?.( normalisedId , template . _path , { skipSelf :true } )
60+ if ( res !== undefined && res !== null ) {
61+ return res
62+ }
5163}
5264}
53- }
5465
55- const dir = parseNodeModulePath ( normalisedImporter ) . dir || pkgDir
66+ const dir = parseNodeModulePath ( normalisedImporter ) . dir || pkgDir
5667
57- const res = await this . resolve ?.( normalisedId , dir , { skipSelf :true } )
58- if ( res !== undefined && res !== null ) {
59- return res
60- }
68+ const res = await this . resolve ?.( normalisedId , dir , { skipSelf :true } )
69+ if ( res !== undefined && res !== null ) {
70+ return res
71+ }
6172
62- const environmentConditions = conditions [ this . environment . name ] ||= resolveConditions ( this . environment )
73+ const environmentConditions = conditions [ this . environment . name ] ||= resolveConditions ( this . environment )
6374
64- const path = resolveModulePath ( id , {
65- from :[ dir , ...nuxt . options . modulesDir ] . map ( d => directoryToURL ( d ) ) ,
66- suffixes :[ '' , 'index' ] ,
67- conditions :environmentConditions ,
68- try :true ,
69- } )
75+ const path = resolveModulePath ( id , {
76+ from :[ dir , ...nuxt . options . modulesDir ] . map ( d => directoryToURL ( d ) ) ,
77+ suffixes :[ '' , 'index' ] ,
78+ conditions :environmentConditions ,
79+ try :true ,
80+ } )
7081
71- if ( ! path ) {
72- logger . debug ( 'Could not resolve id' , id , importer )
73- return null
74- }
82+ if ( ! path ) {
83+ logger . debug ( 'Could not resolve id' , id , importer )
84+ return null
85+ }
7586
76- return normalize ( path )
87+ return normalize ( path )
88+ } ,
7789} ,
7890}
7991}