@@ -31,7 +31,7 @@ interface PolyfillOptions {
31
31
*/
32
32
export const nodePolyfills = ( options : Partial < PolyfillOptions > = { } ) : Plugin => {
33
33
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' )
35
35
const optionsResolved : PolyfillOptions = {
36
36
protocolImports : true ,
37
37
// User options take priority.
@@ -65,9 +65,9 @@ export const nodePolyfills = (options: Partial<PolyfillOptions> = {}): Plugin =>
65
65
{
66
66
...inject ( {
67
67
// 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' ] ,
71
71
} ) ,
72
72
} ,
73
73
] ,
@@ -82,10 +82,29 @@ export const nodePolyfills = (options: Partial<PolyfillOptions> = {}): Plugin =>
82
82
process : 'process' ,
83
83
} ,
84
84
inject : [
85
- globalShims ,
85
+ globalShimsPath ,
86
86
] ,
87
87
plugins : [
88
88
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
+ } ,
89
108
] ,
90
109
} ,
91
110
} ,
0 commit comments