diff --git a/.gitignore b/.gitignore index 4f4cd64..c0ca768 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,5 @@ robotMsg.json site/public/example/ src/components/**/_example/*.md example/ios/Pods -example/ios/build \ No newline at end of file +example/ios/build +example/src/componentList.ts \ No newline at end of file diff --git a/example/package.json b/example/package.json index fc474c2..f3d447d 100644 --- a/example/package.json +++ b/example/package.json @@ -6,11 +6,12 @@ "postinstall": "cd ios && pod install", "android": "react-native run-android", "ios": "react-native run-ios", - "web": "craco start", - "build:web": "craco build", + "web": "npm run gen-router && craco start", + "build:web": "npm run gen-router && craco build", "lint": "eslint .", - "start": "react-native start", - "test": "jest" + "start": "npm run gen-router && react-native start", + "test": "jest", + "gen-router": "node ./scripts/gen-router.js" }, "dependencies": { "@craco/craco": "^7.0.0", diff --git a/example/scripts/gen-router.js b/example/scripts/gen-router.js new file mode 100644 index 0000000..fdd50fe --- /dev/null +++ b/example/scripts/gen-router.js @@ -0,0 +1,29 @@ +// react native在import时路径不能使用字符串拼接形式,用脚本生成一份router配置 +const prettier = require('prettier'); +const fs = require('fs'); +const path = require('path'); +const prettierRc = require('../../.prettierrc.js'); +const componentConfig = require('../src/config.json'); + +console.log('正在生成路由文件...'); +const keys = []; +componentConfig.forEach((config) => { + config?.children?.forEach((child) => { + keys.push(child.key); + }); +}); + +const code = `import { lazy, LazyExoticComponent } from 'react'; + +const componentsMap: Record JSX.Element>> = { + ${keys.map((key) => `${key}: lazy(() => import('@src/components/${key}/_example/index'))`)} +} +export default componentsMap +`; +const formattedCode = prettier.format(code, { + ...prettierRc, + parser: 'babel', +}); + +fs.writeFileSync(path.resolve(__dirname, '../src/componentList.ts'), formattedCode, { encoding: 'utf-8' }); +console.log('已生成路由文件👏🏻'); diff --git a/example/src/App.tsx b/example/src/App.tsx index fcc5be1..0af0e50 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -8,9 +8,10 @@ import Animated, { interpolate, useAnimatedStyle } from 'react-native-reanimated import { ThemeProvider, useTheme } from '@src/theme'; import { Text, ScrollView, View, Button } from '@src/components'; import { SafeAreaProvider, initialWindowMetrics, useSafeAreaInsets } from 'react-native-safe-area-context'; -import { lazy, useCallback, Suspense, useState, useEffect, useMemo } from 'react'; +import { useCallback, Suspense, useState, useEffect, useMemo } from 'react'; import { ListItem } from './components/ListItem'; -import componentList from './component-list'; +import componentConfig from './config.json'; +import componentsMap from './componentList'; const Drawer = createDrawerNavigator(); const Stack = createStackNavigator(); @@ -24,7 +25,7 @@ function ExampleList() { - {componentList?.map?.((list, index) => { + {componentConfig?.map?.((list, index) => { const endIndex = (list?.children?.length ?? 0) - 1; return ( @@ -123,7 +124,7 @@ function App(): JSX.Element { const linkingMap = useMemo(() => { const result: Record = {}; - componentList.forEach((component) => { + componentConfig.forEach((component) => { component?.children?.forEach((child) => { result[child.key] = child.key; }); @@ -166,9 +167,9 @@ function App(): JSX.Element { }} > - {componentList?.map?.((list) => { + {componentConfig?.map?.((list) => { return list?.children?.map((item) => { - const LazyComponent = lazy(() => import(`@src/components/${item.key}/_example/index`)); + const LazyComponent = componentsMap[item.key]; function component(_props: any) { return ( diff --git a/example/src/component-list.ts b/example/src/component-list.ts deleted file mode 100644 index a0185e7..0000000 --- a/example/src/component-list.ts +++ /dev/null @@ -1,35 +0,0 @@ -export default [ - { - title: '基础组件', - children: [ - { - title: 'Base 基础容器', - key: 'Base', - }, - { - title: 'Button 按钮', - key: 'Button', - }, - ], - }, - { - title: '布局', - children: [], - }, - { - title: '导航', - children: [], - }, - { - title: '输入', - children: [], - }, - { - title: '数据展示', - children: [], - }, - { - title: '消息提醒', - children: [], - }, -]; diff --git a/example/src/config.json b/example/src/config.json new file mode 100644 index 0000000..2fe7111 --- /dev/null +++ b/example/src/config.json @@ -0,0 +1,35 @@ +[ + { + "title": "基础组件", + "children": [ + { + "title": "Base 基础容器", + "key": "Base" + }, + { + "title": "Button 按钮", + "key": "Button" + } + ] + }, + { + "title": "布局", + "children": [] + }, + { + "title": "导航", + "children": [] + }, + { + "title": "输入", + "children": [] + }, + { + "title": "数据展示", + "children": [] + }, + { + "title": "消息提醒", + "children": [] + } +] \ No newline at end of file diff --git a/site/web/site.config.js b/site/web/site.config.js index 539d062..443ba8a 100644 --- a/site/web/site.config.js +++ b/site/web/site.config.js @@ -1,4 +1,4 @@ -import baseConfig from '@example/component-list.ts'; +import baseConfig from '@example/config.json'; const componentConfig = baseConfig.map((config) => { const result = {