【问题标题】:React Router v3 per flow bundle splittingReact Router v3 每个流束拆分
【发布时间】:2024-01-22 21:26:01
【问题描述】:

React Roter v3 API 上是否有可用的方法来拆分每个流的捆绑包?从文档和来自互联网的示例来看,有一种拆分方法,但它是 per-component,使用 PlainRoutegetChildRoutesgetComponent 等。将它与 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


    【解决方案1】:

    是的!

    这是我在项目中的做法:

    如果您使用普通路由,请在 router.js 文件中:

    const componentRoutes = {
      path: '/',
      childRoutes: [
        {
          path: 'routeA',
          getChildRoutes (partialNextState, callback) {
            System.import('./aRoutes')
              .then(module => callback(null, module.default))
          }
        },
        {
          path: 'routeB',
          getChildRoutes (partialNextState, callback) {
            System.import('./bRoutes')
              .then(module => callback(null, module.default))
          }
        },
        {
          path: 'routeC',
          getChildRoutes (partialNextState, callback) {
            System.import('./cRoutes')
              .then(module => callback(null, module.default))
          }
        },
      ]
    }
    const Routes = () => {
      return (
        <Router history={browserHistory} routes={componentRoutes} />
      )
    }
    
    export default Routes
    

    aRoutes.js 内部:

    import aDashboard from './containers/aDashboard'
    import SomePage from './containers/SomePage'
    const aRoutes = [
      {
        path: 'aDashboard',
        component: aDashboard
      },
      {
        path: 'somePage',
        component: SomePage
      }
    ]
    
    export default aRoutes
    

    bRoutes.js 内部:

    import bDashboard from './containers/bDashboard'
    import SomePageB from './containers/SomePageB'
    
    const bRoutes = [
      {
        path: 'bDashboard',
        component: bDashboard
      },
      {
        path: 'somePageB',
        component: SomePageB
      }
    ]
    
    export default bRoutes
    

    cRoutes.js 内部:

    import cDashboard from './containers/cDashboard'
    import SomePageC from './containers/SomePagec'
    
    const cRoutes = [
      {
        path: 'cDashboard',
        component: cDashboard
      },
      {
        path: 'somePageC',
        component: SomePageC
      }
    ]
    
    export default cRoutes
    

    因此,对于您的情况,我将有 3 个“路由”文件,您可以使用 3 个 System.import 语句来拉入它们的 childRoutes。您可以将任意数量的组件放入每个路由文件中,但您将只有 3 个捆绑包,因为您只有 3 个 System.import 调用。通过这种方式,您可以将捆绑包拆分为仅三个文件,而不是每个组件。希望这会有所帮助

    【讨论】:

    • 帮助很大!
    • 我现在有一个问题:如果我有 3 个这样的块,对于第一个流(path: 'app',在您的示例中)0.js 被提取,对于第二个:0.js 和 @ 987654329@,第三个0.js1.js2.js。这些流是 100% 独立的,顺序无关紧要:最后一个总是获取所有块。
    • 嘿,如果我的回答不清楚,对不起。我更新了答案,希望能解决你的问题。如果这不起作用发布您正在使用的代码,我会尝试弄清楚为什么它会得到所有的块
    • *.com/questions/43581981/… 我在这里留下了更多细节。我想流之间的共享组件可能有问题。
    • 您是否尝试仅使用普通路由创建路由器?这似乎是唯一的区别。我的项目中似乎没有同样的问题