React Router v3 每个流包拆分

React Router v3 per flow bundle splitting

有没有办法利用 React Roter v3 API 上的可用内容来拆分每个流的捆绑包?从文档和互联网示例中,有一种拆分方法,但它是 per-component,使用 PlainRoute with getChildRoutes, getComponent 等。将它与 System.imports 一起使用我会被拆分每条路线的子捆绑包。

我试过的是这样的,我在开发工具的网络选项卡中得到了 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 正确执行此操作的任何提示。

是的!

以下是我在项目中的做法:

如果您使用普通路由,请在您的 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 个 "route" 文件,您使用 3 个 System.import 语句来拉入它们的子路由。您可以将任意数量的组件放入每个路由文件中,但您将只有 3 个包,因为您只有 3 个 System.import 调用。通过这种方式,您可以将捆绑包拆分为三个文件,而不是每个组件。希望这有帮助