Skip to content

Commit 9774327

Browse files
authored
fix: resolve Metro bundler error for optional FlashList dependency (#121)
* fix: resolve Metro bundler error for optional FlashList dependency - Separate optional library imports into dedicated file - Fix "Requiring unknown module" error when FlashList is not installed * chore: fix lint * chore: add v1.9.1 patch changeset * chore: update changeset * docs: add clarifying comment for FlashList dynamic require
1 parent d2b6faf commit 9774327

File tree

7 files changed

+33
-23
lines changed

7 files changed

+33
-23
lines changed

.changeset/purple-trains-poke.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"react-native-gesture-image-viewer": patch
3+
---
4+
5+
fix: resolve Metro bundler error for optional FlashList dependency
6+
7+
- Separate optional library imports into dedicated file
8+
- Fix "Requiring unknown module" error when FlashList is not installed
9+
- Enable dynamic feature detection without breaking Metro static analysis
10+
11+
[Related issue](https://github.com/saseungmin/react-native-gesture-image-viewer/issues/120)
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
[
2-
"overview",
3-
"installation",
4-
"quick-start"
5-
]
1+
["overview", "installation", "quick-start"]
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
[
2-
"overview",
3-
"installation",
4-
"quick-start"
5-
]
1+
["overview", "installation", "quick-start"]

docs/tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
"useDefineForClassFields": true,
1414
"allowImportingTsExtensions": true
1515
},
16-
"include": ["docs", "theme", "rspress.config.ts", "components", "expo-snack.d.ts"],
16+
"include": [
17+
"docs",
18+
"theme",
19+
"rspress.config.ts",
20+
"components",
21+
"expo-snack.d.ts"
22+
],
1723
"mdx": {
1824
"checkMdx": true
1925
}

src/utils/FlashList.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// NOTE - Dynamic require to prevent Metro bundler static analysis errors when FlashList is not installed
2+
export let FlashList: any = null;
3+
4+
try {
5+
FlashList = require('@shopify/flash-list').FlashList;
6+
} catch {
7+
FlashList = null;
8+
}

src/utils.ts renamed to src/utils/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FlatList as RNFlatList, ScrollView as RNScrollView } from 'react-native';
22
import { FlatList as GestureFlatList, ScrollView as GestureScrollView } from 'react-native-gesture-handler';
3-
import type { FlatListComponent, ScrollViewComponent } from './types';
3+
import type { FlatListComponent, ScrollViewComponent } from '../types';
4+
import { FlashList } from './FlashList';
45

56
export const isScrollViewLike = (component: React.ComponentType<any>): component is ScrollViewComponent => {
67
return component === RNScrollView || component === GestureScrollView;
@@ -15,14 +16,8 @@ export const isFlatListLike = (component: React.ComponentType<any>): component i
1516
};
1617

1718
export const isFlashListLike = (component: React.ComponentType<any>): boolean => {
18-
try {
19-
const FlashList = require('@shopify/flash-list')?.FlashList;
20-
21-
if (FlashList && component === FlashList) {
22-
return true;
23-
}
24-
} catch {
25-
// do nothing
19+
if (FlashList && component === FlashList) {
20+
return true;
2621
}
2722

2823
return component?.displayName === 'FlashList' || component?.name === 'FlashList';

tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@
2626
"target": "ESNext",
2727
"verbatimModuleSyntax": true
2828
},
29-
"exclude": [
30-
"docs/**/*",
31-
]
29+
"exclude": ["docs/**/*"]
3230
}

0 commit comments

Comments
 (0)