Skip to content

Commit

Permalink
feat: 修改动态组件导入方式
Browse files Browse the repository at this point in the history
  • Loading branch information
niyg-mw committed Nov 13, 2023
1 parent 59330b7 commit e54c067
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,28 @@ const SlideBox = styled(Box)(({ theme, open }) => ({
width: "100%",
}));

const modules = import.meta.glob("./components/Pages/**/*.tsx");

export default function MiniDrawer() {
const { active, dialogOpen, activeApp } = useGlobaltore((state) => state);

const Component = React.useMemo(() => {
if (!activeApp) return null;
return React.lazy(() => import(`@/components/Pages/${activeApp}`));
}, [activeApp]);

// 获取动态导入的组件模块
const modulePath = `./components/Pages/${activeApp}/index.tsx`;
const module = modules[modulePath];

if (module) {
// 动态导入并返回 Promise
return React.lazy(
() => module() as Promise<{ default: React.ComponentType<any> }>
);
} else {
console.error(`Module not found for ${modulePath}`);
return null;
}
}, [activeApp, modules]);
return (
<Paper
sx={{
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import App from "@/App.tsx";
import "./index.css";
import { ThemeProvider, createTheme } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
Expand Down
10 changes: 10 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export default defineConfig({
"@": "/src",
},
},
build: {
rollupOptions: {
output: {
manualChunks: {
"react-vendor": ["react", "react-dom", "react-router-dom"],
},
},
},
target: "es2015",
},
optimizeDeps: {
include: ["react/jsx-runtime"],
},
Expand Down

0 comments on commit e54c067

Please sign in to comment.