@@ -11,6 +11,7 @@ import {
1111 type TransformHook ,
1212 compareModuleNames ,
1313 isEnabled ,
14+ isModuleName ,
1415 isNodeProtocolImport ,
1516 toRegExp ,
1617 withoutNodeProtocol ,
@@ -197,6 +198,32 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
197198 ...( ( isEnabled ( optionsResolved . globals . process , 'dev' ) ) ? [ require . resolve ( 'vite-plugin-node-polyfills/shims/process' ) ] : [ ] ) ,
198199 ]
199200
201+ const resolvePlugin : Plugin = {
202+ name : 'vite-plugin-node-polyfills:resolve' ,
203+ enforce : 'pre' ,
204+ resolveId : {
205+ // @ts -expect-error -- hook filters are only supported in Vite 6.3.0+
206+ filter : { id : / \/ $ / } ,
207+ async handler ( source , importer , opts ) {
208+ const stripped = source . slice ( 0 , - 1 )
209+
210+ if ( ! isModuleName ( stripped ) ) {
211+ return
212+ }
213+
214+ if ( ! optionsResolved . protocolImports && isNodeProtocolImport ( stripped ) ) {
215+ return
216+ }
217+
218+ if ( isExcluded ( stripped ) ) {
219+ return
220+ }
221+
222+ return this . resolve ( stripped , importer , { skipSelf : true , ...opts } )
223+ } ,
224+ } ,
225+ }
226+
200227 const globalShimsBanner = [
201228 ...( ( isEnabled ( optionsResolved . globals . Buffer , 'dev' ) ) ? globalShimBanners . buffer : [ ] ) ,
202229 ...( ( isEnabled ( optionsResolved . globals . global , 'dev' ) ) ? globalShimBanners . global : [ ] ) ,
@@ -223,9 +250,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
223250 const plugin : Plugin = {
224251 name : 'vite-plugin-node-polyfills' ,
225252 config ( config , env ) {
253+ const isBuild = env . command === 'build'
226254 const isDev = env . command === 'serve'
227255 // @ts -expect-error - this.meta.rolldownVersion only exists with rolldown-vite 7+
228256 const isRolldownVite = ! ! this ?. meta ?. rolldownVersion
257+ const isNativeInjectAvailable = isBuild && isRolldownVite
229258
230259 // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
231260 const defines = {
@@ -241,7 +270,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
241270 ...( isEnabled ( optionsResolved . globals . process , 'build' ) ? { process : 'vite-plugin-node-polyfills/shims/process' } : { } ) ,
242271 }
243272
244- const isNativeInjectAvailable = env . command === 'build' && isRolldownVite
245273 rawInjectPlugin = ( Object . keys ( shimsToInject ) . length > 0 && ! isNativeInjectAvailable ) ? inject ( shimsToInject ) as Plugin : false
246274 if ( rawInjectPlugin === false ) {
247275 delete injectPlugin . transform
@@ -286,6 +314,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
286314 define : defines ,
287315 } ,
288316 plugins : [
317+ resolvePlugin ,
289318 {
290319 name : 'vite-plugin-node-polyfills:optimizer' ,
291320 banner : isDev ? globalShimsBanner : undefined ,
@@ -335,6 +364,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
335364 } ,
336365 }
337366 return [
367+ resolvePlugin ,
338368 injectPlugin ,
339369 plugin ,
340370 ]
0 commit comments