Skip to content

Commit

Permalink
Fix for vite plugin crypto exports not defined when deployed
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-toth committed Aug 21, 2024
1 parent 24ad973 commit 1b8b656
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
20 changes: 19 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { nodePolyfills } from 'vite-plugin-node-polyfills'
import { replaceCodePlugin } from './plugins/vite-plugin-replace'

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
Expand Down Expand Up @@ -48,7 +49,24 @@ export default defineNuxtConfig({
define: {
'process.env.DEBUG': false,
},
plugins: [ nodePolyfills() ]
plugins: [
nodePolyfills(),
replaceCodePlugin({
replacements: [
{
from: `if ((crypto && crypto.getRandomValues) || !process.browser) {
exports.randomFill = randomFill
exports.randomFillSync = randomFillSync
} else {
exports.randomFill = oldBrowser
exports.randomFillSync = oldBrowser
}`,
to: `exports.randomFill = randomFill
exports.randomFillSync = randomFillSync`,
}
]
})
]
},

/**
Expand Down
14 changes: 14 additions & 0 deletions plugins/vite-plugin-replace/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Plugin } from 'vite'

interface ViteReplacement {
from: string | RegExp
to: string | Function
}

export interface VitePluginReplaceConfig {
replacements: ViteReplacement[]
}

export declare function replaceCodePlugin(
config: VitePluginReplaceConfig
): Plugin
53 changes: 53 additions & 0 deletions plugins/vite-plugin-replace/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/vite-plugin-replace/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b8b656

Please sign in to comment.