@@ -31,7 +31,7 @@ interface PolyfillOptions {
3131 */
3232export const nodePolyfills = ( options : Partial < PolyfillOptions > = { } ) : Plugin => {
3333 const require = createRequire ( import . meta. url )
34- const globalShims = require . resolve ( 'node-stdlib-browser/helpers/esbuild/shim' )
34+ const globalShimsPath = require . resolve ( 'node-stdlib-browser/helpers/esbuild/shim' )
3535 const optionsResolved : PolyfillOptions = {
3636 protocolImports : true ,
3737 // User options take priority.
@@ -65,9 +65,9 @@ export const nodePolyfills = (options: Partial<PolyfillOptions> = {}): Plugin =>
6565 {
6666 ...inject ( {
6767 // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
68- global : [ globalShims , 'global' ] ,
69- process : [ globalShims , 'process' ] ,
70- Buffer : [ globalShims , 'Buffer' ] ,
68+ global : [ globalShimsPath , 'global' ] ,
69+ process : [ globalShimsPath , 'process' ] ,
70+ Buffer : [ globalShimsPath , 'Buffer' ] ,
7171 } ) ,
7272 } ,
7373 ] ,
@@ -82,10 +82,29 @@ export const nodePolyfills = (options: Partial<PolyfillOptions> = {}): Plugin =>
8282 process : 'process' ,
8383 } ,
8484 inject : [
85- globalShims ,
85+ globalShimsPath ,
8686 ] ,
8787 plugins : [
8888 esbuildPlugin ( polyfills ) ,
89+ // Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild).
90+ // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469
91+ {
92+ name : 'vite-plugin-node-polyfills-shims-resolver' ,
93+ setup ( build ) {
94+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
95+ const escapedGlobalShimsPath = globalShimsPath . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
96+ const filter = new RegExp ( `^${ escapedGlobalShimsPath } $` )
97+
98+ // https://esbuild.github.io/plugins/#on-resolve
99+ build . onResolve ( { filter } , ( ) => {
100+ return {
101+ // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468
102+ external : false ,
103+ path : globalShimsPath ,
104+ }
105+ } )
106+ } ,
107+ } ,
89108 ] ,
90109 } ,
91110 } ,
0 commit comments