From a67cdb4ae1e02a43bd6afc8bae9ad6b87a5e2f33 Mon Sep 17 00:00:00 2001 From: bennybrainless <242895261+bennybrainless@users.noreply.github.com> Date: Thu, 20 Nov 2025 23:54:30 -0600 Subject: [PATCH] Add robust crypto polyfill for Vite build --- discord/vite.config.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/discord/vite.config.ts b/discord/vite.config.ts index eb48216..b4313db 100644 --- a/discord/vite.config.ts +++ b/discord/vite.config.ts @@ -1,7 +1,24 @@ +import { randomFillSync, webcrypto } from 'node:crypto' import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import path from "path"; +// Ensure a Web Crypto implementation with getRandomValues exists for Vite's build pipeline +if (typeof globalThis.crypto?.getRandomValues !== 'function') { + const cryptoWithRandomValues = + webcrypto && typeof webcrypto.getRandomValues === 'function' + ? (webcrypto as unknown as Crypto) + : { + getRandomValues: (array: T) => { + randomFillSync(array) + return array + }, + } + + // @ts-expect-error - assigning Web Crypto to the global crypto reference for Node builds + globalThis.crypto = cryptoWithRandomValues +} + // https://vitejs.dev/config/ export default defineConfig({ base: './',