fix(vite): add error handling for parsing `NUXT_VITE_NODE_OPTIONS` · nuxt/nuxt@41a564d

Original file line numberDiff line numberDiff line change

@@ -5,7 +5,17 @@ import { Buffer } from 'node:buffer'

55

import { isTest } from 'std-env'

66

import type { ViteNodeFetch, ViteNodeRequestMap, ViteNodeServerOptions } from './plugins/vite-node.ts'

77
8-

export const viteNodeOptions: ViteNodeServerOptions = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}')

8+

function getViteNodeOptionsEnvVar () {

9+

const envVar = process.env.NUXT_VITE_NODE_OPTIONS

10+

try {

11+

return JSON.parse(envVar || '{}')

12+

} catch (e) {

13+

console.error('vite-node-shared: Failed to parse NUXT_VITE_NODE_OPTIONS environment variable.', e)

14+

return {}

15+

}

16+

}

17+
18+

export const viteNodeOptions: ViteNodeServerOptions = getViteNodeOptionsEnvVar()

919
1020

const pendingRequests = new Map<number, { resolve: (value: any) => void, reject: (reason?: any) => void }>()

1121

let requestIdCounter = 0