Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit f9103fe

Browse files
committed
vite 초기 캐시 없을 때 generouted 패키지 오류 수정
1 parent 2a472b0 commit f9103fe

File tree

6 files changed

+100
-11
lines changed

6 files changed

+100
-11
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"dayjs": "^1.11.4",
1515
"electron-store": "^8.1.0",
1616
"formik": "^2.2.9",
17-
"generouted": "^1.5.0",
1817
"react": "^18.2.0",
1918
"react-dom": "^18.2.0",
2019
"react-router-dom": "^6.3.0",
@@ -64,4 +63,4 @@
6463
"./resources/**"
6564
]
6665
}
67-
}
66+
}

src/components/Routes/Routes.tsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {
2+
LoaderFn,
3+
Outlet,
4+
ReactLocation,
5+
Route,
6+
Router,
7+
RouterProps,
8+
} from '@tanstack/react-location';
9+
import { Fragment } from 'react';
10+
11+
type Element = () => JSX.Element;
12+
type Module = { default: Element; Loader: LoaderFn; Pending: Element; Failure: Element };
13+
14+
const PRESERVED = import.meta.glob<Module>('/src/pages/(_app|404).tsx', { eager: true });
15+
const ROUTES = import.meta.glob<Module>('/src/pages/**/[a-z[]*.tsx');
16+
17+
const preservedRoutes: Partial<Record<string, () => JSX.Element>> = Object.keys(PRESERVED).reduce(
18+
(routes, key) => {
19+
const path = key.replace(/\/src\/pages\/|\.tsx$/g, '');
20+
return { ...routes, [path]: PRESERVED[key]?.default };
21+
},
22+
{},
23+
);
24+
25+
const regularRoutes = Object.keys(ROUTES).reduce<Route[]>((routes, key) => {
26+
const module = ROUTES[key];
27+
const route: Route = {
28+
element: () => module().then(mod => (mod?.default ? <mod.default /> : <></>)),
29+
loader: async (...args) => module().then(mod => mod?.Loader?.(...args)),
30+
pendingElement: async () => module().then(mod => (mod?.Pending ? <mod.Pending /> : null)),
31+
errorElement: async () => module().then(mod => (mod?.Failure ? <mod.Failure /> : null)),
32+
};
33+
34+
const segments = key
35+
.replace(/\/src\/pages|\.tsx$/g, '')
36+
.replace(/\[\.{3}.+\]/, '*')
37+
.replace(/\[([^\]]+)\]/g, ':$1')
38+
.split('/')
39+
.filter(Boolean);
40+
41+
segments.reduce((parent, segment, index) => {
42+
const path = segment.replace(/index|\./g, '/');
43+
const root = index === 0;
44+
const leaf = index === segments.length - 1 && segments.length > 1;
45+
const node = !root && !leaf;
46+
const insert = /^\w|\//.test(path) ? 'unshift' : 'push';
47+
48+
if (root) {
49+
const dynamic = path.startsWith(':') || path === '*';
50+
if (dynamic) return parent;
51+
52+
const last = segments.length === 1;
53+
if (last) {
54+
routes.push({ path, ...route });
55+
return parent;
56+
}
57+
}
58+
59+
if (root || node) {
60+
const current = root ? routes : parent.children;
61+
const found = current?.find(route => route.path === path);
62+
if (found) found.children ??= [];
63+
else current?.[insert]({ path, children: [] });
64+
return found || (current?.[insert === 'unshift' ? 0 : current.length - 1] as Route);
65+
}
66+
67+
if (leaf) {
68+
parent?.children?.[insert]({ path, ...route });
69+
}
70+
71+
return parent;
72+
}, {} as Route);
73+
74+
return routes;
75+
}, []);
76+
77+
const App = preservedRoutes?.['_app'] || Fragment;
78+
const NotFound = preservedRoutes?.['404'] || Fragment;
79+
80+
const location = new ReactLocation();
81+
const routes = [...regularRoutes, { path: '*', element: <NotFound /> }];
82+
83+
const Routes = (props: Omit<RouterProps, 'children' | 'location' | 'routes'> = {}) => {
84+
return (
85+
<Router {...props} location={location} routes={routes}>
86+
<App>
87+
<Outlet />
88+
</App>
89+
</Router>
90+
);
91+
};
92+
93+
export default Routes;

src/components/Routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './Routes';
2+
export { default } from './Routes';

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ReactDOM from 'react-dom/client';
22

3-
import { Routes } from 'generouted';
43
import { RecoilRoot } from 'recoil';
4+
import Routes from '~/components/Routes';
55

66
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
77
<RecoilRoot>

vite.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import tsconfigPaths from 'vite-tsconfig-paths';
1111
export default defineConfig({
1212
plugins: [
1313
react(),
14+
checker({}),
15+
tsconfigPaths(),
16+
svgr(),
1417
electron({
1518
main: {
1619
entry: 'electron/index.ts',
@@ -21,9 +24,6 @@ export default defineConfig({
2124
},
2225
},
2326
}),
24-
tsconfigPaths(),
25-
svgr(),
26-
checker({}),
2727
],
2828
server: {
2929
port: 3000,

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,11 +2216,6 @@ functions-have-names@^1.2.2:
22162216
resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834"
22172217
integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ==
22182218

2219-
generouted@^1.5.0:
2220-
version "1.5.0"
2221-
resolved "https://registry.yarnpkg.com/generouted/-/generouted-1.5.0.tgz#87a795de1f6558d96a95d2f4b46ce881cb631a82"
2222-
integrity sha512-6U2o6cpT98lUDObZc5Z/vyAemUVhbxMazv35tIIPzEMdYgmDRLg1zOYpihlzDZ+0RTaaW9K2ymLOpxTRS1YEPw==
2223-
22242219
gensync@^1.0.0-beta.2:
22252220
version "1.0.0-beta.2"
22262221
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"

0 commit comments

Comments
 (0)