-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathpatch-finddomnode.js
More file actions
executable file
·79 lines (67 loc) · 2.56 KB
/
patch-finddomnode.js
File metadata and controls
executable file
·79 lines (67 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env node
/**
* React 19 findDOMNode Patcher
*
* This script patches node_modules files that use the deprecated findDOMNode
* to make them compatible with React 19.
*/
const fs = require('fs');
const path = require('path');
const glob = require('glob');
console.log('🔧 Patching findDOMNode usage for React 19 compatibility...');
// Find all JS files in node_modules that might use findDOMNode
const patterns = [
'node_modules/react-onclickoutside/**/*.js',
'node_modules/react-widgets/**/*.js'
];
const findDOMNodeReplacement = `
// React 19 compatibility: findDOMNode has been removed
const findDOMNode = (component) => {
if (!component) return null;
if (component.nodeType) return component;
if (component.current) return component.current;
return null;
};
`;
patterns.forEach(pattern => {
const files = glob.sync(pattern);
files.forEach(filePath => {
try {
let content = fs.readFileSync(filePath, 'utf8');
// Check if this file imports or uses findDOMNode
if (content.includes('findDOMNode')) {
console.log(`📝 Patching: ${filePath}`);
// Replace the import of findDOMNode
content = content.replace(
/import\s*{\s*([^}]*?)findDOMNode([^}]*?)\s*}\s*from\s*['"]react-dom['"]/g,
(match, before, after) => {
const otherImports = (before + after).replace(/,\s*,/g, ',').replace(/^,|,$/g, '').trim();
if (otherImports) {
return `import { ${otherImports} } from 'react-dom';\n${findDOMNodeReplacement}`;
} else {
return findDOMNodeReplacement;
}
}
);
// Also handle export pattern
content = content.replace(
/export\s*{\s*([^}]*?)findDOMNode([^}]*?)\s*}\s*from\s*['"]react-dom['"]/g,
(match, before, after) => {
const otherExports = (before + after).replace(/,\s*,/g, ',').replace(/^,|,$/g, '').trim();
if (otherExports) {
return `export { ${otherExports} } from 'react-dom';\n${findDOMNodeReplacement}\nexport { findDOMNode };`;
} else {
return `${findDOMNodeReplacement}\nexport { findDOMNode };`;
}
}
);
// Write the patched content back
fs.writeFileSync(filePath, content, 'utf8');
console.log(`✅ Successfully patched: ${filePath}`);
}
} catch (error) {
console.error(`❌ Error patching ${filePath}:`, error.message);
}
});
});
console.log('🎉 Patching complete! Try running npm start again.');