@@ -92,24 +92,25 @@ const _require = createRequire(import.meta.url)
9292
9393const isSsrExternalCache = new WeakMap <
9494 ResolvedConfig ,
95- ( id : string ) => boolean | undefined
95+ ( id : string , importer ?: string ) => boolean | undefined
9696> ( )
9797
9898export function shouldExternalizeForSSR (
9999 id : string ,
100+ importer : string | undefined ,
100101 config : ResolvedConfig ,
101102) : boolean | undefined {
102103 let isSsrExternal = isSsrExternalCache . get ( config )
103104 if ( ! isSsrExternal ) {
104105 isSsrExternal = createIsSsrExternal ( config )
105106 isSsrExternalCache . set ( config , isSsrExternal )
106107 }
107- return isSsrExternal ( id )
108+ return isSsrExternal ( id , importer )
108109}
109110
110111export function createIsConfiguredAsSsrExternal (
111112 config : ResolvedConfig ,
112- ) : ( id : string ) => boolean {
113+ ) : ( id : string , importer ?: string ) => boolean {
113114 const { ssr, root } = config
114115 const noExternal = ssr ?. noExternal
115116 const noExternalFilter =
@@ -126,6 +127,7 @@ export function createIsConfiguredAsSsrExternal(
126127
127128 const isExternalizable = (
128129 id : string ,
130+ importer ?: string ,
129131 configuredAsExternal ?: boolean ,
130132 ) : boolean => {
131133 if ( ! bareImportRE . test ( id ) || id . includes ( '\0' ) ) {
@@ -134,7 +136,9 @@ export function createIsConfiguredAsSsrExternal(
134136 try {
135137 return ! ! tryNodeResolve (
136138 id ,
137- undefined ,
139+ // Skip passing importer in build to avoid externalizing non-hoisted dependencies
140+ // unresolveable from root (which would be unresolvable from output bundles also)
141+ config . command === 'build' ? undefined : importer ,
138142 resolveOptions ,
139143 ssr ?. target === 'webworker' ,
140144 undefined ,
@@ -157,7 +161,7 @@ export function createIsConfiguredAsSsrExternal(
157161
158162 // Returns true if it is configured as external, false if it is filtered
159163 // by noExternal and undefined if it isn't affected by the explicit config
160- return ( id : string ) => {
164+ return ( id : string , importer ?: string ) => {
161165 const { ssr } = config
162166 if ( ssr ) {
163167 if (
@@ -169,14 +173,14 @@ export function createIsConfiguredAsSsrExternal(
169173 }
170174 const pkgName = getNpmPackageName ( id )
171175 if ( ! pkgName ) {
172- return isExternalizable ( id )
176+ return isExternalizable ( id , importer )
173177 }
174178 if (
175179 // A package name in ssr.external externalizes every
176180 // externalizable package entry
177181 ssr . external ?. includes ( pkgName )
178182 ) {
179- return isExternalizable ( id , true )
183+ return isExternalizable ( id , importer , true )
180184 }
181185 if ( typeof noExternal === 'boolean' ) {
182186 return ! noExternal
@@ -185,24 +189,24 @@ export function createIsConfiguredAsSsrExternal(
185189 return false
186190 }
187191 }
188- return isExternalizable ( id )
192+ return isExternalizable ( id , importer )
189193 }
190194}
191195
192196function createIsSsrExternal (
193197 config : ResolvedConfig ,
194- ) : ( id : string ) => boolean | undefined {
198+ ) : ( id : string , importer ?: string ) => boolean | undefined {
195199 const processedIds = new Map < string , boolean | undefined > ( )
196200
197201 const isConfiguredAsExternal = createIsConfiguredAsSsrExternal ( config )
198202
199- return ( id : string ) => {
203+ return ( id : string , importer ?: string ) => {
200204 if ( processedIds . has ( id ) ) {
201205 return processedIds . get ( id )
202206 }
203207 let external = false
204208 if ( id [ 0 ] !== '.' && ! path . isAbsolute ( id ) ) {
205- external = isBuiltin ( id ) || isConfiguredAsExternal ( id )
209+ external = isBuiltin ( id ) || isConfiguredAsExternal ( id , importer )
206210 }
207211 processedIds . set ( id , external )
208212 return external
0 commit comments