【发布时间】:2024-01-22 21:26:01
【问题描述】:
React Roter v3 API 上是否有可用的方法来拆分每个流的捆绑包?从文档和来自互联网的示例来看,有一种拆分方法,但它是 per-component,使用 PlainRoute 和 getChildRoutes、getComponent 等。将它与 System.imports 一起使用我得到了分别为每个 Route 拆分子捆绑包。
我尝试的是这样的,我在开发工具的“网络”选项卡中得到了0.js, 1.js ... 9.js 块。
getChildRoutes(_, fetchComponents) {
Promise.all([
System.import('./pages/page1.jsx'),
System.import('./pages/page2.jsx'),
...
System.import('./pages/page10.jsx')
])
.then(([page1, page2... page10]) => {
fetchComponents(null, [
{path: '...', indexRoute: {component: page1}},
{path: '...', component: page2},
...
{path: '...', component: page10}
])
})
}
那么像这样的结构有可能只有 3 个块(子包)吗?
<Router ...>
{/*flow 1*/}
<Rote component...>
<Route path... component... />
<Route component>
<IndexRoute .../>
<Route path ... component... />
<Route path ... component... />
<Route path ... component... />
...
</Route>
</Route>
{/*flow 2*/}
<Route>
...
</Route>
{/*flow 3*/}
<Route />
</Router>
如果使用 React Router 无法做到这一点,我将不胜感激任何提示如何使用 Webpack 正确做到这一点。
【问题讨论】:
标签: reactjs split webpack react-router bundle