@@ -4,14 +4,20 @@ 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 { compareModuleNames , isEnabled , isNodeProtocolImport , toRegExp , withoutNodeProtocol } from './utils'
8-
9- type TransformHook = Extract < Plugin [ 'transform' ] , Function >
10-
11- export type BuildTarget = 'build' | 'dev'
12- export type BooleanOrBuildTarget = boolean | BuildTarget
13- export type ModuleName = keyof typeof stdLibBrowser
14- export type ModuleNameWithoutNodePrefix < T = ModuleName > = T extends `node:${infer P } ` ? P : never
7+ import { getModulesToPolyfill } from './modules'
8+ import { buildTrailingSlashNormalizer } from './plugins'
9+ import {
10+ type BooleanOrBuildTarget ,
11+ type ModuleName ,
12+ type ModuleNameWithoutNodePrefix ,
13+ type TransformHook ,
14+ compareModuleNames ,
15+ globalShimBanners ,
16+ isEnabled ,
17+ isNodeProtocolImport ,
18+ toRegExp ,
19+ withoutNodeProtocol ,
20+ } from './utils'
1521
1622export type PolyfillOptions = {
1723 /**
@@ -89,21 +95,6 @@ export type PolyfillOptionsResolved = {
8995 protocolImports : boolean ,
9096}
9197
92- const globalShimBanners = {
93- buffer : [
94- `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'` ,
95- `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill` ,
96- ] ,
97- global : [
98- `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'` ,
99- `globalThis.global = globalThis.global || __global_polyfill` ,
100- ] ,
101- process : [
102- `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'` ,
103- `globalThis.process = globalThis.process || __process_polyfill` ,
104- ] ,
105- }
106-
10798/**
10899 * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
109100 *
@@ -147,6 +138,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
147138 } ,
148139 }
149140
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+
150151 const isExcluded = ( moduleName : ModuleName ) => {
151152 if ( optionsResolved . include . length > 0 ) {
152153 return ! optionsResolved . include . some ( ( includedName ) => compareModuleNames ( moduleName , includedName ) )
@@ -220,9 +221,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
220221 const plugin : Plugin = {
221222 name : 'vite-plugin-node-polyfills' ,
222223 config ( config , env ) {
224+ const isBuild = env . command === 'build'
223225 const isDev = env . command === 'serve'
224226 // @ts -expect-error - this.meta.rolldownVersion only exists with rolldown-vite 7+
225227 const isRolldownVite = ! ! this ?. meta ?. rolldownVersion
228+ const isNativeInjectAvailable = isBuild && isRolldownVite
226229
227230 // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
228231 const defines = {
@@ -238,7 +241,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
238241 ...( isEnabled ( optionsResolved . globals . process , 'build' ) ? { process : 'vite-plugin-node-polyfills/shims/process' } : { } ) ,
239242 }
240243
241- const isNativeInjectAvailable = env . command === 'build' && isRolldownVite
242244 rawInjectPlugin = ( Object . keys ( shimsToInject ) . length > 0 && ! isNativeInjectAvailable ) ? inject ( shimsToInject ) as Plugin : false
243245 if ( rawInjectPlugin === false ) {
244246 delete injectPlugin . transform
@@ -283,6 +285,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
283285 define : defines ,
284286 } ,
285287 plugins : [
288+ trailingSlashNormalizer ,
286289 {
287290 name : 'vite-plugin-node-polyfills:optimizer' ,
288291 banner : isDev ? globalShimsBanner : undefined ,
@@ -331,8 +334,10 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
331334 }
332335 } ,
333336 }
337+
334338 return [
339+ trailingSlashNormalizer ,
335340 injectPlugin ,
336341 plugin ,
337- ]
342+ ] . flat ( )
338343}
0 commit comments