From 189836e7fe7f490afa07c2d3f4e3b87845daee5c Mon Sep 17 00:00:00 2001 From: Leif Dalan Date: Wed, 6 Jul 2016 15:39:00 -0700 Subject: [PATCH] Added code splitting with require.ensure. --- src/routes.js | 116 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 33 deletions(-) diff --git a/src/routes.js b/src/routes.js index 63527745d..6f6e0f7a2 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1,18 +1,5 @@ -import React from 'react'; -import {IndexRoute, Route} from 'react-router'; import { isLoaded as isAuthLoaded, load as loadAuth } from 'redux/modules/auth'; -import { - App, - Chat, - Home, - Widgets, - About, - Login, - LoginSuccess, - Survey, - NotFound, - } from 'containers'; - +// import {Login} from 'containers'; export default (store) => { const requireLogin = (nextState, replace, cb) => { function checkAuth() { @@ -30,29 +17,92 @@ export default (store) => { checkAuth(); } }; - /** * Please keep routes in alphabetical order */ - return ( - - { /* Home (main) route */ } - + if (typeof require.ensure !== 'function') require.ensure = (deps, cb) => cb(require); - { /* Routes requiring login */ } - - - - + return { + path: '/', + component: require('./containers/App/App'), + indexRoute: { + component: require('./containers/Home/Home') + }, + childRoutes: [{ + path: 'login', + getComponent(nextState, cb) { + console.time('gettingComponent'); + store.dispatch({ + type: 'WEBPACK_LOAD' + }); + require.ensure([], (require) => { + cb(null, require('./containers/Login/Login')); + store.dispatch({ + type: 'WEBPACK_LOAD_END' + }); + console.timeEnd('gettingComponent'); + }); + } - { /* Routes */ } - - - - + }, { + path: 'about', + getComponent(nextState, cb) { + console.time('gettingComponent'); + store.dispatch({ + type: 'WEBPACK_LOAD' + }); + require.ensure([], (require) => { + cb(null, require('./containers/About/About')); + store.dispatch({ + type: 'WEBPACK_LOAD_END' + }); + console.timeEnd('gettingComponent'); + }); + } - { /* Catch all route */ } - - - ); + }, { + path: 'survey', + getComponent(nextState, cb) { + require.ensure([], (require) => + cb(null, require('./containers/Survey/Survey'))); + } + }, { + path: 'widgets', + getComponent(nextState, cb) { + store.dispatch({ + type: 'WEBPACK_LOAD' + }); + require.ensure([], (require) => { + cb(null, require('./containers/Widgets/Widgets')); + store.dispatch({ + type: 'WEBPACK_LOAD_END' + }); + }); + } + }, { + onEnter: requireLogin, + childRoutes: [ + { + path: 'chat', + getComponent(nextState, cb) { + require.ensure([], (require) => + cb(null, require('./containers/Chat/Chat'))); + } + }, + { + path: 'loginSuccess', + getComponent(nextState, cb) { + require.ensure([], (require) => + cb(null, require('./containers/LoginSuccess/LoginSuccess'))); + } + } + ] + }, { + path: '*', + getComponent(nextState, cb) { + require.ensure([], (require) => + cb(null, require('./containers/NotFound/NotFound'))); + } + }] + }; };