|
|
@@ -1,55 +1,20 @@
|
|
|
-/// <reference types="vite/client" />
|
|
|
-
|
|
|
-import React, { Suspense, lazy } from 'react';
|
|
|
+/**
|
|
|
+ * [파일명 규칙 안내]
|
|
|
+ * - 이 프로젝트는 src/pages 경로 하위의 컴포넌트를 자동으로 lazy import 하며,
|
|
|
+ * - 각 파일은 반드시 PascalCase (대문자 시작)로 작성되어야 합니다.
|
|
|
+ * 예: about/Ceo.tsx, performance/History.tsx 등
|
|
|
+ * - 단, index.tsx는 예외적으로 허용됩니다.
|
|
|
+ *
|
|
|
+ * 해당 규칙은 lazyImport.tsx에서 강제 적용되며, 이를 지키지 않으면 라우팅 실패가 발생합니다.
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+import React from 'react';
|
|
|
import { Routes, Route } from 'react-router-dom';
|
|
|
import Header from '@/components/Header';
|
|
|
-import Layout from '@/components/Layout';
|
|
|
-import menuItems from '@/data/menuItems';
|
|
|
-
|
|
|
-// 모든 페이지를 glob으로 가져오기
|
|
|
-const pages = import.meta.glob('@/pages/**/*.tsx');
|
|
|
-
|
|
|
-// lazy import 유틸
|
|
|
-const lazyImport = (path: string) => {
|
|
|
- const importer = pages[`/src/pages/${path}.tsx`];
|
|
|
- return importer
|
|
|
- ? lazy(importer as () => Promise<{ default: React.ComponentType }>)
|
|
|
- : lazy(() => Promise.resolve({ default: () => <div>페이지를 찾을 수 없습니다.</div> }));
|
|
|
-};
|
|
|
-
|
|
|
-// Suspense 래핑
|
|
|
-const withSuspense = (Component: React.LazyExoticComponent<React.ComponentType>) => (
|
|
|
- <Suspense fallback={<div>로딩 중...</div>}>
|
|
|
- <Component />
|
|
|
- </Suspense>
|
|
|
-);
|
|
|
-
|
|
|
-// 메뉴 기반 라우팅
|
|
|
-const renderRoutesFromMenu = () => {
|
|
|
- return menuItems.map((parent) => {
|
|
|
- const parentPath = parent.href.replace(/^\//, '');
|
|
|
-
|
|
|
- return (
|
|
|
- <Route key={parent.href} path={parentPath} element={<Layout />}>
|
|
|
- {parent.children.map((child) => {
|
|
|
- const childPath = child.href.replace(/^\//, '');
|
|
|
- const fullPath = `${parentPath}/${childPath}`;
|
|
|
- const Component = lazyImport(fullPath);
|
|
|
-
|
|
|
- return (
|
|
|
- <Route
|
|
|
- key={child.href}
|
|
|
- path={childPath}
|
|
|
- element={withSuspense(Component)}
|
|
|
- />
|
|
|
- );
|
|
|
- })}
|
|
|
- </Route>
|
|
|
- );
|
|
|
- });
|
|
|
-};
|
|
|
+import { lazyImport, withSuspense } from '@/utils/lazyImport';
|
|
|
+import { renderRoutesFromMenu } from '@/routes/generateRoutes';
|
|
|
|
|
|
-// 메인 및 404 페이지도 lazy import로 처리
|
|
|
const Main = lazyImport('main/index');
|
|
|
const NotFound = lazyImport('NotFound');
|
|
|
|