11import { createRequire } from 'node:module'
22import inject from '@rollup/plugin-inject'
3+ import browserResolve from 'browser-resolve'
34import stdLibBrowser from 'node-stdlib-browser'
45import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
56import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
67import type { Plugin } from 'vite'
7- import { compareModuleNames , isEnabled , isNodeProtocolImport , toRegExp , withoutNodeProtocol } from './utils'
8+ import { compareModuleNames , isEnabled , isNodeProtocolImport , resolvePolyfill , toEntries , toRegExp , withoutNodeProtocol } from './utils'
89
9- export type BuildTarget = 'build' | 'dev'
10+ export type BareModuleName < T = ModuleName > = T extends `node:${infer P } ` ? P : never
11+ export type BareModuleNameWithSubpath < T = ModuleName > = T extends `node:${infer P } ` ? `${P } /${string } ` : never
1012export type BooleanOrBuildTarget = boolean | BuildTarget
13+ export type BuildTarget = 'build' | 'dev'
1114export type ModuleName = keyof typeof stdLibBrowser
12- export type ModuleNameWithoutNodePrefix < T = ModuleName > = T extends `node:${infer P } ` ? P : never
15+ export type OverrideOptions = {
16+
17+ }
1318
1419export type PolyfillOptions = {
1520 /**
@@ -22,7 +27,7 @@ export type PolyfillOptions = {
2227 * })
2328 * ```
2429 */
25- include ?: ModuleNameWithoutNodePrefix [ ] ,
30+ include ?: BareModuleName [ ] ,
2631 /**
2732 * @example
2833 *
@@ -32,7 +37,7 @@ export type PolyfillOptions = {
3237 * })
3338 * ```
3439 */
35- exclude ?: ModuleNameWithoutNodePrefix [ ] ,
40+ exclude ?: BareModuleName [ ] ,
3641 /**
3742 * Specify whether specific globals should be polyfilled.
3843 *
@@ -66,7 +71,7 @@ export type PolyfillOptions = {
6671 * })
6772 * ```
6873 */
69- overrides ?: { [ Key in ModuleNameWithoutNodePrefix ] ?: string } ,
74+ overrides ?: { [ Key in BareModuleName | BareModuleNameWithSubpath ] ?: string } ,
7075 /**
7176 * Specify whether the Node protocol version of an import (e.g. `node:buffer`) should be polyfilled too.
7277 *
@@ -76,14 +81,14 @@ export type PolyfillOptions = {
7681}
7782
7883export type PolyfillOptionsResolved = {
79- include : ModuleNameWithoutNodePrefix [ ] ,
80- exclude : ModuleNameWithoutNodePrefix [ ] ,
84+ include : BareModuleName [ ] ,
85+ exclude : BareModuleName [ ] ,
8186 globals : {
8287 Buffer : BooleanOrBuildTarget ,
8388 global : BooleanOrBuildTarget ,
8489 process : BooleanOrBuildTarget ,
8590 } ,
86- overrides : { [ Key in ModuleNameWithoutNodePrefix ] ?: string } ,
91+ overrides : { [ Key in BareModuleName | BareModuleNameWithSubpath ] ?: string } ,
8792 protocolImports : boolean ,
8893}
8994
@@ -153,16 +158,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
153158 return optionsResolved . exclude . some ( ( excludedName ) => compareModuleNames ( moduleName , excludedName ) )
154159 }
155160
156- const toOverride = ( name : ModuleNameWithoutNodePrefix ) : string | void => {
157- if ( isEnabled ( optionsResolved . globals . Buffer , 'dev' ) && / ^ b u f f e r $ / . test ( name ) ) {
161+ const toOverride = ( name : BareModuleName ) : string | void => {
162+ if ( / ^ b u f f e r $ / . test ( name ) ) {
158163 return 'vite-plugin-node-polyfills/shims/buffer'
159164 }
160165
161- if ( isEnabled ( optionsResolved . globals . global , 'dev' ) && / ^ g l o b a l $ / . test ( name ) ) {
166+ if ( / ^ g l o b a l $ / . test ( name ) ) {
162167 return 'vite-plugin-node-polyfills/shims/global'
163168 }
164169
165- if ( isEnabled ( optionsResolved . globals . process , 'dev' ) && / ^ p r o c e s s $ / . test ( name ) ) {
170+ if ( / ^ p r o c e s s $ / . test ( name ) ) {
166171 return 'vite-plugin-node-polyfills/shims/process'
167172 }
168173
@@ -201,6 +206,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
201206
202207 return {
203208 name : 'vite-plugin-node-polyfills' ,
209+ enforce : 'pre' ,
204210 config : ( config , env ) => {
205211 const isDev = env . command === 'serve'
206212
@@ -246,21 +252,25 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
246252 ...globalShimPaths ,
247253 ] ,
248254 plugins : [
249- esbuildPlugin ( polyfills ) ,
255+ esbuildPlugin ( {
256+ ...polyfills ,
257+ } ) ,
250258 // Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild).
251259 // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469
252260 {
253261 name : 'vite-plugin-node-polyfills-shims-resolver' ,
254- setup ( build ) {
262+ setup : ( build ) => {
255263 for ( const globalShimPath of globalShimPaths ) {
256264 const globalShimsFilter = toRegExp ( globalShimPath )
257265
258266 // https://esbuild.github.io/plugins/#on-resolve
259267 build . onResolve ( { filter : globalShimsFilter } , ( ) => {
268+ const resolved = browserResolve . sync ( globalShimPath )
269+
260270 return {
261271 // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468
262272 external : false ,
263- path : globalShimPath ,
273+ path : resolved ,
264274 }
265275 } )
266276 }
@@ -277,5 +287,28 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
277287 } ,
278288 }
279289 } ,
290+ async resolveId ( id ) {
291+ for ( const [ moduleName , modulePath ] of toEntries ( polyfills ) ) {
292+ if ( id . startsWith ( modulePath ) ) {
293+ // Grab the subpath without the forward slash. E.g. `path/posix` -> `posix`
294+ const moduleSubpath = id . slice ( modulePath . length + 1 )
295+
296+ if ( moduleSubpath . length > 0 ) {
297+ const moduleNameWithoutProtocol = withoutNodeProtocol ( moduleName )
298+ const overrideName = `${ moduleNameWithoutProtocol } /${ moduleSubpath } ` as const
299+ const override = optionsResolved . overrides [ overrideName ]
300+
301+ if ( ! override ) {
302+ // Todo: Maybe throw error?
303+ return undefined
304+ }
305+
306+ return await resolvePolyfill ( this , override )
307+ }
308+
309+ return browserResolve . sync ( modulePath )
310+ }
311+ }
312+ } ,
280313 }
281314}
0 commit comments