|
1 | | -import { readFileSync } from 'node:fs' |
2 | 1 | import { createRequire } from 'node:module' |
3 | 2 | import inject from '@rollup/plugin-inject' |
4 | 3 | import stdLibBrowser from 'node-stdlib-browser' |
@@ -88,6 +87,17 @@ export type PolyfillOptionsResolved = { |
88 | 87 | protocolImports: boolean, |
89 | 88 | } |
90 | 89 |
|
| 90 | +const globalShimsBanner = [ |
| 91 | + `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`, |
| 92 | + `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`, |
| 93 | + `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`, |
| 94 | + ``, |
| 95 | + `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`, |
| 96 | + `globalThis.global = globalThis.global || __global_polyfill`, |
| 97 | + `globalThis.process = globalThis.process || __process_polyfill`, |
| 98 | + ``, |
| 99 | +].join('\n') |
| 100 | + |
91 | 101 | /** |
92 | 102 | * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports. |
93 | 103 | * |
@@ -123,8 +133,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { |
123 | 133 | require.resolve('vite-plugin-node-polyfills/shims/global'), |
124 | 134 | require.resolve('vite-plugin-node-polyfills/shims/process'), |
125 | 135 | ] |
126 | | - const globalShimsBannerPath = require.resolve('vite-plugin-node-polyfills/shims/banner') |
127 | | - const globalShimsBanner = readFileSync(globalShimsBannerPath, 'utf-8') |
128 | 136 | const optionsResolved: PolyfillOptionsResolved = { |
129 | 137 | include: [], |
130 | 138 | exclude: [], |
@@ -232,7 +240,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { |
232 | 240 | // Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild). |
233 | 241 | // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469 |
234 | 242 | { |
235 | | - |
236 | 243 | name: 'vite-plugin-node-polyfills-shims-resolver', |
237 | 244 | setup(build) { |
238 | 245 | for (const globalShimPath of globalShimPaths) { |
|
0 commit comments