-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement code splitting with react-loadable (#84)
- Loading branch information
1 parent
1ffe020
commit 8958b05
Showing
25 changed files
with
317 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { compose } from 'redux'; | ||
import loadable from 'components/lib/loadable/loadable'; | ||
import liftFetchData from 'components/lib/liftfetchdata/liftFetchData'; | ||
import Loader from 'components/lib/loader/Loader'; | ||
import Error from 'components/lib/error/Error'; | ||
import path from 'path'; | ||
|
||
const webpackRequireWeakId = () => require.resolveWeak('./Index'); | ||
|
||
const LoadingComponent = ({ isLoading, error, pastDelay }) => { | ||
if (isLoading && pastDelay) { | ||
return <Loader />; | ||
} else if (error) { | ||
console.log('ERROR IS BEING RENDERED', error); | ||
return <Error>Error! Component failed to load</Error>; | ||
} | ||
return null; | ||
}; | ||
|
||
// NOTE: We're making a trade off for more aggresive code splitting (i.e. includes | ||
// action creators) for waterfall requests when fetching the chunk and the data | ||
// in the client. | ||
const enhance = compose(liftFetchData(webpackRequireWeakId), loadable); | ||
|
||
export default enhance({ | ||
loader: () => import('./Index'), | ||
LoadingComponent, | ||
webpackRequireWeakId | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.root { | ||
color: crimson; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { PropTypes } from 'react'; | ||
import styles from './Error.css'; | ||
|
||
const Error = ({ children }) => <div className={styles.root}>{children}</div>; | ||
|
||
Error.propTypes = { | ||
children: PropTypes.node | ||
}; | ||
|
||
Error.defaultProps = { | ||
children: 'Error' | ||
}; | ||
|
||
export default Error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Lifts static fetchData method from weakId component to `Component`, deliberately | ||
* circumventing static require / import to prevent Webpack from bundling the | ||
* weakId component and it's deps. | ||
*/ | ||
export default webpackRequireWeakId => | ||
Component => { | ||
const weakId = webpackRequireWeakId(); | ||
if (__webpack_modules__[weakId]) { | ||
const module = __webpack_require__(weakId).default; | ||
if (module && typeof module.fetchData === 'function') { | ||
Component.fetchData = (...args) => module.fetchData(...args); | ||
} | ||
} | ||
return Component; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { Component } from 'react'; | ||
import Loadable from 'react-loadable'; | ||
|
||
let moduleIds = new Set(); | ||
|
||
/** | ||
* Wraps `Loadable` in order to support `flushServerSideRequires` (or a variation thereof) | ||
* as proposed here -- https://medium.com/@thejameskyle/react-loadable-2674c59de178 | ||
*/ | ||
const WrappedLoadable = options => { | ||
const BaseComponent = Loadable(options); | ||
return class extends Component { | ||
constructor() { | ||
super(); | ||
moduleIds.add(options.webpackRequireWeakId()); | ||
} | ||
|
||
render() { | ||
return <BaseComponent {...this.props} /> | ||
} | ||
} | ||
} | ||
|
||
WrappedLoadable.flushModuleIds = () => { | ||
const ids = [...moduleIds]; | ||
moduleIds.clear(); | ||
return ids; | ||
} | ||
|
||
export default WrappedLoadable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.root { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react'; | ||
import styles from './Loader.css'; | ||
|
||
const Loader = () => <div className={styles.root}>Loading...</div> | ||
|
||
export default Loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.