@@ -4,6 +4,8 @@ import stdLibBrowser from 'node-stdlib-browser'
44import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
55import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
66import type { Plugin } from 'vite'
7+ import { getModulesToPolyfill } from './modules'
8+ import { buildTrailingSlashNormalizer } from './plugins'
79import {
810 type BooleanOrBuildTarget ,
911 type ModuleName ,
@@ -136,6 +138,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
136138 } ,
137139 }
138140
141+ const modulesToPolyfill = getModulesToPolyfill ( {
142+ modulesToExclude : optionsResolved . exclude ,
143+ modulesToInclude : optionsResolved . include ,
144+ } )
145+
146+ const trailingSlashNormalizer = buildTrailingSlashNormalizer ( {
147+ modules : modulesToPolyfill ,
148+ protocolImports : optionsResolved . protocolImports ,
149+ } )
150+
139151 const isExcluded = ( moduleName : ModuleName ) => {
140152 if ( optionsResolved . include . length > 0 ) {
141153 return ! optionsResolved . include . some ( ( includedName ) => compareModuleNames ( moduleName , includedName ) )
@@ -209,9 +221,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
209221 const plugin : Plugin = {
210222 name : 'vite-plugin-node-polyfills' ,
211223 config ( config , env ) {
224+ const isBuild = env . command === 'build'
212225 const isDev = env . command === 'serve'
213226 // @ts -expect-error - this.meta.rolldownVersion only exists with rolldown-vite 7+
214227 const isRolldownVite = ! ! this ?. meta ?. rolldownVersion
228+ const isNativeInjectAvailable = isBuild && isRolldownVite
215229
216230 // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
217231 const defines = {
@@ -227,7 +241,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
227241 ...( isEnabled ( optionsResolved . globals . process , 'build' ) ? { process : 'vite-plugin-node-polyfills/shims/process' } : { } ) ,
228242 }
229243
230- const isNativeInjectAvailable = env . command === 'build' && isRolldownVite
231244 rawInjectPlugin = ( Object . keys ( shimsToInject ) . length > 0 && ! isNativeInjectAvailable ) ? inject ( shimsToInject ) as Plugin : false
232245 if ( rawInjectPlugin === false ) {
233246 delete injectPlugin . transform
@@ -272,6 +285,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
272285 define : defines ,
273286 } ,
274287 plugins : [
288+ trailingSlashNormalizer ,
275289 {
276290 name : 'vite-plugin-node-polyfills:optimizer' ,
277291 banner : isDev ? globalShimsBanner : undefined ,
@@ -320,8 +334,10 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
320334 }
321335 } ,
322336 }
337+
323338 return [
339+ trailingSlashNormalizer ,
324340 injectPlugin ,
325341 plugin ,
326- ]
342+ ] . flat ( )
327343}
0 commit comments