-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.tsx
48 lines (40 loc) · 1.29 KB
/
route.tsx
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
import { Fragment } from "react";
import { Routes, Route } from "react-router-dom";
type servedType = {
[key: string | number | symbol]: any;
};
/**
* File System 기반 라우터
* 수정 금지
*/
const PRESERVED = import.meta.glob<Record<string, string>>("../pages/{_app,404}.{js,jsx,ts,tsx}", {
eager: true,
});
const ROUTES = import.meta.glob<Record<string, string>>("../pages/**/[a-z]*.{js,jsx,ts,tsx}", {
eager: true,
});
const preserve: servedType = Object.keys(PRESERVED).reduce((p, file) => {
const key = file.replace(/\.\.\/pages\/|\.(js|jsx|ts|tsx)$/g, "");
return { ...p, [key]: PRESERVED[file].default };
}, {});
const routes = Object.keys(ROUTES).map((route) => {
const path = route
.replace(/\.\.\/pages|index|\.(js|jsx|ts|tsx)$/g, "")
.replace(/\[\.{3}[^/]+\]/, "*")
.replace(/\[([^/]+)\]/g, ":$1");
return { path, component: ROUTES[route].default };
});
export default function FileRoutes() {
const App = preserve?.["_app"] || Fragment;
const NotFound = preserve?.["404"] || Fragment;
return (
<App>
<Routes>
{routes.map(({ path, component: Component = Fragment }) => {
return <Route key={path} path={path} element={<Component />} />;
})}
<Route path="*" element={<NotFound />} />
</Routes>
</App>
);
}