Skip to content

Instantly share code, notes, and snippets.

@FbN
Last active November 6, 2024 23:56
Show Gist options
  • Save FbN/0e651105937c8000f10fefdf9ec9af3d to your computer and use it in GitHub Desktop.
Save FbN/0e651105937c8000f10fefdf9ec9af3d to your computer and use it in GitHub Desktop.
vite.config.js node built-in polyfills
// yarn add --dev @esbuild-plugins/node-globals-polyfill
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
// yarn add --dev @esbuild-plugins/node-modules-polyfill
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
// You don't need to add this to deps, it's included by @esbuild-plugins/node-modules-polyfill
import rollupNodePolyFill from 'rollup-plugin-node-polyfills'
export default {
resolve: {
alias: {
// This Rollup aliases are extracted from @esbuild-plugins/node-modules-polyfill,
// see https://github.com/remorses/esbuild-plugins/blob/master/node-modules-polyfill/src/polyfills.ts
// process and buffer are excluded because already managed
// by node-globals-polyfill
util: 'rollup-plugin-node-polyfills/polyfills/util',
sys: 'util',
events: 'rollup-plugin-node-polyfills/polyfills/events',
stream: 'rollup-plugin-node-polyfills/polyfills/stream',
path: 'rollup-plugin-node-polyfills/polyfills/path',
querystring: 'rollup-plugin-node-polyfills/polyfills/qs',
punycode: 'rollup-plugin-node-polyfills/polyfills/punycode',
url: 'rollup-plugin-node-polyfills/polyfills/url',
string_decoder:
'rollup-plugin-node-polyfills/polyfills/string-decoder',
http: 'rollup-plugin-node-polyfills/polyfills/http',
https: 'rollup-plugin-node-polyfills/polyfills/http',
os: 'rollup-plugin-node-polyfills/polyfills/os',
assert: 'rollup-plugin-node-polyfills/polyfills/assert',
constants: 'rollup-plugin-node-polyfills/polyfills/constants',
_stream_duplex:
'rollup-plugin-node-polyfills/polyfills/readable-stream/duplex',
_stream_passthrough:
'rollup-plugin-node-polyfills/polyfills/readable-stream/passthrough',
_stream_readable:
'rollup-plugin-node-polyfills/polyfills/readable-stream/readable',
_stream_writable:
'rollup-plugin-node-polyfills/polyfills/readable-stream/writable',
_stream_transform:
'rollup-plugin-node-polyfills/polyfills/readable-stream/transform',
timers: 'rollup-plugin-node-polyfills/polyfills/timers',
console: 'rollup-plugin-node-polyfills/polyfills/console',
vm: 'rollup-plugin-node-polyfills/polyfills/vm',
zlib: 'rollup-plugin-node-polyfills/polyfills/zlib',
tty: 'rollup-plugin-node-polyfills/polyfills/tty',
domain: 'rollup-plugin-node-polyfills/polyfills/domain'
}
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: 'globalThis'
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true
}),
NodeModulesPolyfillPlugin()
]
}
},
build: {
rollupOptions: {
plugins: [
// Enable rollup polyfills plugin
// used during production bundling
rollupNodePolyFill()
]
}
}
}
@caioregatieri
Copy link

I solve it with:

electron.vite.config.ts
`
build: {
outDir: resolve(devPath, 'renderer'),

  rollupOptions: {
    plugins: [
      injectProcessEnvPlugin({
        NODE_ENV: 'production',
        platform: process.platform,
      }),
      rollupNodePolyFill,
    ],
  },
},

`
and this:

index.html
<script> global = globalThis; </script>

@xiangnanscu
Copy link

rollup-plugin-node-polyfills

@Terkea Hello, I use yours and get this error:

Error: Build failed with 2 errors:
error: Could not read from file: /root/zzb/node_modules/@esbuild-plugins/node-globals-polyfill/_virtual-process-polyfill_.js
error: Could not read from file: /root/zzb/node_modules/@esbuild-plugins/node-globals-polyfill/_virtual-buffer-polyfill_.js
    at failureErrorWithLog (/root/zzb/node_modules/esbuild/lib/main.js:1566:15)
    at /root/zzb/node_modules/esbuild/lib/main.js:1024:28
    at runOnEndCallbacks (/root/zzb/node_modules/esbuild/lib/main.js:1438:61)
    at buildResponseToResult (/root/zzb/node_modules/esbuild/lib/main.js:1022:7)
    at /root/zzb/node_modules/esbuild/lib/main.js:1134:14
    at responseCallbacks.<computed> (/root/zzb/node_modules/esbuild/lib/main.js:671:9)
    at handleIncomingPacket (/root/zzb/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/root/zzb/node_modules/esbuild/lib/main.js:647:7)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)

@dennisharrison
Copy link

rollup-plugin-node-polyfills

@Terkea Hello, I use yours and get this error:

Error: Build failed with 2 errors:
error: Could not read from file: /root/zzb/node_modules/@esbuild-plugins/node-globals-polyfill/_virtual-process-polyfill_.js
error: Could not read from file: /root/zzb/node_modules/@esbuild-plugins/node-globals-polyfill/_virtual-buffer-polyfill_.js
    at failureErrorWithLog (/root/zzb/node_modules/esbuild/lib/main.js:1566:15)
    at /root/zzb/node_modules/esbuild/lib/main.js:1024:28
    at runOnEndCallbacks (/root/zzb/node_modules/esbuild/lib/main.js:1438:61)
    at buildResponseToResult (/root/zzb/node_modules/esbuild/lib/main.js:1022:7)
    at /root/zzb/node_modules/esbuild/lib/main.js:1134:14
    at responseCallbacks.<computed> (/root/zzb/node_modules/esbuild/lib/main.js:671:9)
    at handleIncomingPacket (/root/zzb/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/root/zzb/node_modules/esbuild/lib/main.js:647:7)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)

Update to Vite 4.2 solved the issue for me.

@rebz
Copy link

rebz commented May 5, 2023

Have a project with a WebRTC video player component and we are receiving the following error:

this.once is not a function

Seems to be an issue with how Vite handles transforms around Duplex.once and _readable_stream.

Here is a GitHub thread where someone was discussing a similar issue:
nodejs/readable-stream#315 (comment)

Has anyone ran into this or have a solution for it?
I've tried a variety of solutions in this thread and other GitHub threads... none have worked so far. I am on the latest version of Vite ("vite": "^4.3.4",).

@txreplay
Copy link

Anyone know how to handle cytpo ? It seems to work in build mode but can't get it to work in dev with crypto-browserify

@minorvargas-hoag
Copy link

this worked. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment