|
4 | 4 | * React.useLayoutEffect and React.createContext to be available globally |
5 | 5 | */ |
6 | 6 |
|
7 | | -// Force window.React to exist and have key methods |
8 | | -if (typeof window !== 'undefined') { |
9 | | - // Create stubs for common React methods |
10 | | - const noop = () => {}; |
11 | | - const noopReturn = () => () => {}; |
12 | | - const emptyObj = {}; |
| 7 | +// Safely check and set properties |
| 8 | +function safelySetProperty(obj: any, prop: string, value: any) { |
| 9 | + // Skip if property already exists |
| 10 | + if (obj[prop] !== undefined) return; |
13 | 11 |
|
14 | | - // Only add if missing |
15 | | - if (!window.React) { |
16 | | - (window as any).React = {}; |
| 12 | + try { |
| 13 | + // Try to set the property - this will fail if the property is read-only |
| 14 | + obj[prop] = value; |
| 15 | + } catch (e) { |
| 16 | + // Property is read-only or can't be assigned - just log in dev mode |
| 17 | + if (process.env.NODE_ENV === 'development') { |
| 18 | + console.debug(`Unable to set ${prop} on React`, e); |
| 19 | + } |
17 | 20 | } |
| 21 | +} |
| 22 | + |
| 23 | +// Create stubs for common React methods |
| 24 | +const noop = () => {}; |
| 25 | +const noopReturn = () => () => {}; |
| 26 | +const emptyObj = {}; |
| 27 | + |
| 28 | +// Apply React polyfills safely |
| 29 | +if (typeof window !== 'undefined') { |
| 30 | + const windowAny = window as any; |
18 | 31 |
|
19 | | - // Add missing methods - use type assertion for TypeScript |
20 | | - const reactShim = (window as any).React; |
21 | | - |
22 | | - if (!reactShim.useState) reactShim.useState = () => [undefined, noop]; |
23 | | - if (!reactShim.useEffect) reactShim.useEffect = noopReturn; |
24 | | - if (!reactShim.useLayoutEffect) reactShim.useLayoutEffect = reactShim.useEffect || noopReturn; |
25 | | - if (!reactShim.useRef) reactShim.useRef = () => ({ current: null }); |
26 | | - if (!reactShim.useContext) reactShim.useContext = () => emptyObj; |
27 | | - if (!reactShim.createContext) reactShim.createContext = () => ({ Provider: noop, Consumer: noop }); |
28 | | - if (!reactShim.forwardRef) reactShim.forwardRef = (fn: any) => fn; |
29 | | - if (!reactShim.createElement) reactShim.createElement = () => emptyObj; |
30 | | - if (!reactShim.cloneElement) reactShim.cloneElement = () => emptyObj; |
31 | | - |
32 | | - // Only add these if they don't exist |
33 | | - if (!reactShim.Children) { |
34 | | - reactShim.Children = { |
35 | | - map: noop, |
36 | | - forEach: noop, |
37 | | - count: noop, |
38 | | - only: noop, |
39 | | - }; |
| 32 | + // We won't try to create window.React if it doesn't exist |
| 33 | + // This avoids the error of trying to create a property on window |
| 34 | + if (windowAny.React) { |
| 35 | + // Only try to set properties that don't already exist |
| 36 | + safelySetProperty(windowAny.React, 'useState', () => [undefined, noop]); |
| 37 | + safelySetProperty(windowAny.React, 'useEffect', noopReturn); |
| 38 | + safelySetProperty(windowAny.React, 'useLayoutEffect', windowAny.React.useEffect || noopReturn); |
| 39 | + safelySetProperty(windowAny.React, 'useRef', () => ({ current: null })); |
| 40 | + safelySetProperty(windowAny.React, 'useContext', () => emptyObj); |
| 41 | + safelySetProperty(windowAny.React, 'createContext', () => ({ Provider: noop, Consumer: noop })); |
| 42 | + safelySetProperty(windowAny.React, 'forwardRef', (fn: any) => fn); |
| 43 | + safelySetProperty(windowAny.React, 'createElement', () => emptyObj); |
| 44 | + safelySetProperty(windowAny.React, 'cloneElement', () => emptyObj); |
| 45 | + |
| 46 | + // Add Children if it doesn't exist |
| 47 | + if (!windowAny.React.Children) { |
| 48 | + try { |
| 49 | + windowAny.React.Children = { |
| 50 | + map: noop, |
| 51 | + forEach: noop, |
| 52 | + count: noop, |
| 53 | + only: noop, |
| 54 | + }; |
| 55 | + } catch (e) { |
| 56 | + // Couldn't set Children |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + safelySetProperty(windowAny.React, 'Fragment', 'Fragment'); |
| 61 | + safelySetProperty(windowAny.React, 'StrictMode', 'StrictMode'); |
40 | 62 | } |
41 | | - |
42 | | - if (!reactShim.Fragment) reactShim.Fragment = 'Fragment'; |
43 | | - if (!reactShim.StrictMode) reactShim.StrictMode = 'StrictMode'; |
44 | 63 | } |
45 | 64 |
|
46 | 65 | // Export to ensure this file isn't tree-shaken |
|
0 commit comments