Skip to content

Commit aa52b00

Browse files
FindHaofacebook-github-bot
authored andcommitted
Implement safe dynamic import utility and update App component to use it (#25)
Summary: - Added a new utility function `safeImport` to handle dynamic imports gracefully, avoiding static analysis issues. - Updated the App component to utilize `safeImport` for importing internal utilities, ensuring that the application can handle missing modules without crashing. - Enhanced error handling to log warnings when module imports fail. This change improves the robustness of the application by allowing for safer dynamic imports of internal utilities. Pull Request resolved: #25 Reviewed By: Sibylau Differential Revision: D77968857 Pulled By: FindHao fbshipit-source-id: 0e1ef1ef0e85f1b146b414fcd9069fd1083eddcd
1 parent 8d7459c commit aa52b00

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

website/src/App.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ function App() {
7575
if (exists) {
7676
try {
7777
// Dynamically import internal utils only if fb directory exists
78-
const { getInternalWikiUrl } = await import('./utils/fb/internal_utils');
79-
const url = getInternalWikiUrl();
80-
setInternalWikiUrl(url);
78+
// Use safe import utility to completely bypass static analysis
79+
const { safeImport } = await import('./utils/safeImport');
80+
const module = await safeImport('./utils/fb/internal_utils');
81+
if (module && module.getInternalWikiUrl) {
82+
const url = module.getInternalWikiUrl();
83+
setInternalWikiUrl(url);
84+
}
8185
} catch (error) {
8286
console.warn('Failed to load internal utils:', error);
8387
}

website/src/utils/safeImport.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Safe dynamic import utility that handles missing modules gracefully
3+
* This avoids build-time module resolution issues
4+
*/
5+
6+
/**
7+
* Safely imports a module by path, returning null if it doesn't exist
8+
* Uses Function constructor to avoid static analysis
9+
*/
10+
export async function safeImport(modulePath: string): Promise<any | null> {
11+
try {
12+
// Use Function constructor to create dynamic import
13+
// This completely bypasses static analysis
14+
const dynamicImport = new Function('path', 'return import(path)');
15+
const module = await dynamicImport(modulePath);
16+
return module;
17+
} catch (error) {
18+
console.warn(`Module ${modulePath} not available:`, error);
19+
return null;
20+
}
21+
}

0 commit comments

Comments
 (0)