Skip to content

Commit 1f21fce

Browse files
authored
Merge pull request #41 from iroy2000/adding-lazy-loading-capabilities
Adding lazy loading capabilities
2 parents cbb386d + a851cd7 commit 1f21fce

File tree

10 files changed

+107
-11
lines changed

10 files changed

+107
-11
lines changed

.babelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
"env": {
1212
"development": {
1313
"presets": ["react-hmre"]
14-
}
14+
},
15+
"production": {
16+
"presets": ["react-optimize"]
17+
},
1518
},
1619
"plugins": [
1720
["typecheck", {

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ __Note:__ This boilerplate is not to show case how to code in React / Redux, it
2121

2222
* `Isomorphic (Universal) support`, feel free to add server side support to it, or you can use something like [Next.js](https://github.com/zeit/next.js/), [react-server](https://github.com/redfin/react-server) or [electrode](http://www.electrode.io/)
2323

24-
__Note:__ We also have a branch called `slim`, which we have removed Saga and a few dependencies. To checkout that branch, you can run the following command :
25-
26-
`git clone -b slim https://github.com/iroy2000/react-redux-boilerplate.git`
2724

2825
### Features / Benefits
2926

@@ -38,6 +35,7 @@ Development
3835
* Webpack 3
3936
* Reselect
4037
* Saga
38+
* Lazy Loading component supports
4139
* Type Checking with Babel Type Check ( Flow syntax )
4240
* ESLint for syntax check
4341
* Jest and Enzyme for Unit testing

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-redux-boilerplate",
3-
"version": "0.9.52",
3+
"version": "0.9.55",
44
"description": "React Redux Boilerplate is a workflow boilerplate that make life easier for developers by providing a virtual development environment and production ready build process framework out of the box.",
55
"scripts": {
66
"dev": "cross-env NODE_ENV=development webpack-dashboard -p 3300 -c red -t dashboard -- node bin/commands.js dev",
@@ -28,13 +28,15 @@
2828
"babel-eslint": "^8.2.1",
2929
"babel-jest": "^22.1.0",
3030
"babel-loader": "^7.1.2",
31+
"babel-plugin-syntax-dynamic-import": "^6.18.0",
3132
"babel-plugin-transform-decorators-legacy": "^1.3.4",
3233
"babel-plugin-transform-runtime": "^6.23.0",
3334
"babel-plugin-typecheck": "^3.9.0",
3435
"babel-polyfill": "^6.26.0",
3536
"babel-preset-env": "^1.6.1",
3637
"babel-preset-react": "^6.24.1",
3738
"babel-preset-react-hmre": "^1.1.1",
39+
"babel-preset-react-optimize": "^1.0.1",
3840
"babel-preset-stage-0": "^6.24.1",
3941
"babel-register": "^6.26.0",
4042
"babel-runtime": "^6.26.0",
@@ -85,6 +87,7 @@
8587
"react": "^16.2.0",
8688
"react-dom": "^16.2.0",
8789
"react-helmet": "^5.2.0",
90+
"react-loadable": "^5.3.1",
8891
"react-redux": "^5.0.6",
8992
"react-router": "^4.2.0",
9093
"react-router-dom": "^4.2.2",
@@ -96,7 +99,10 @@
9699
},
97100
"jest": {
98101
"verbose": true,
99-
"testPathIgnorePatterns": ["<rootDir>/__tests__/setup/", "<rootDir>/node_modules/"],
102+
"testPathIgnorePatterns": [
103+
"<rootDir>/__tests__/setup/",
104+
"<rootDir>/node_modules/"
105+
],
100106
"setupTestFrameworkScriptFile": "<rootDir>/__tests__/setup/jestTestSetup.js",
101107
"moduleNameMapper": {
102108
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",

src/js/common/components/Example/Example.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
h1 {
1313
font-size: 50px;
14-
.emphsize {
14+
.emphasize {
1515
font-size: 1.5em;
1616
font-weight: bold;
1717
@if 3 < 5 {

src/js/common/components/Example/Example.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Example extends PureComponent {
1010
if (result && result.size && result.size > 0) {
1111
return (
1212
<div className="exampleOutput">
13-
<h1>Let&apos;s Get <span className="emphsize">Started</span></h1>
13+
<h1>Let&apos;s Get <span className="emphasize">Started</span></h1>
1414
<p>If you see this screen, it means you are all setup \o/</p>
1515
<p>The following JSON are showing contents coming from Redux, Saga and Config.</p>
1616
<pre>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.loader {
2+
color: #f07d00;
3+
font-size: 20px;
4+
margin: 100px auto;
5+
width: 1em;
6+
height: 1em;
7+
border-radius: 50%;
8+
position: relative;
9+
text-indent: -9999em;
10+
-webkit-animation: spinner 1s infinite linear;
11+
animation: spinner 1s infinite linear;
12+
-webkit-transform: translateZ(0);
13+
-ms-transform: translateZ(0);
14+
transform: translateZ(0);
15+
}
16+
@keyframes spinner {
17+
0%,
18+
100% {
19+
box-shadow: 0 -3em 0 0.2em, 2em -2em 0 0em, 3em 0 0 -1em, 2em 2em 0 -1em, 0 3em 0 -1em, -2em 2em 0 -1em, -3em 0 0 -1em, -2em -2em 0 0;
20+
}
21+
12.5% {
22+
box-shadow: 0 -3em 0 0, 2em -2em 0 0.2em, 3em 0 0 0, 2em 2em 0 -1em, 0 3em 0 -1em, -2em 2em 0 -1em, -3em 0 0 -1em, -2em -2em 0 -1em;
23+
}
24+
25% {
25+
box-shadow: 0 -3em 0 -0.5em, 2em -2em 0 0, 3em 0 0 0.2em, 2em 2em 0 0, 0 3em 0 -1em, -2em 2em 0 -1em, -3em 0 0 -1em, -2em -2em 0 -1em;
26+
}
27+
37.5% {
28+
box-shadow: 0 -3em 0 -1em, 2em -2em 0 -1em, 3em 0em 0 0, 2em 2em 0 0.2em, 0 3em 0 0em, -2em 2em 0 -1em, -3em 0em 0 -1em, -2em -2em 0 -1em;
29+
}
30+
50% {
31+
box-shadow: 0 -3em 0 -1em, 2em -2em 0 -1em, 3em 0 0 -1em, 2em 2em 0 0em, 0 3em 0 0.2em, -2em 2em 0 0, -3em 0em 0 -1em, -2em -2em 0 -1em;
32+
}
33+
62.5% {
34+
box-shadow: 0 -3em 0 -1em, 2em -2em 0 -1em, 3em 0 0 -1em, 2em 2em 0 -1em, 0 3em 0 0, -2em 2em 0 0.2em, -3em 0 0 0, -2em -2em 0 -1em;
35+
}
36+
75% {
37+
box-shadow: 0em -3em 0 -1em, 2em -2em 0 -1em, 3em 0em 0 -1em, 2em 2em 0 -1em, 0 3em 0 -1em, -2em 2em 0 0, -3em 0em 0 0.2em, -2em -2em 0 0;
38+
}
39+
87.5% {
40+
box-shadow: 0em -3em 0 0, 2em -2em 0 -1em, 3em 0 0 -1em, 2em 2em 0 -1em, 0 3em 0 -1em, -2em 2em 0 0, -3em 0em 0 0, -2em -2em 0 0.2em;
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { PureComponent } from 'react';
2+
3+
import './LazyLoading.css'
4+
5+
class LazyLoading extends PureComponent {
6+
getMessage() {
7+
const {
8+
isLoading,
9+
timedOut,
10+
pastDelay,
11+
error,
12+
} = this.props;
13+
14+
const errorMessage = 'We can&apos;t pull up information at this point, please try again.'
15+
16+
if (isLoading) {
17+
if (timedOut) {
18+
return <div>{errorMessage}</div>;
19+
} else if (pastDelay) {
20+
return <div className="loader">Loading...</div>;
21+
}
22+
return null;
23+
} else if (error) {
24+
return <div>{errorMessage}</div>;
25+
}
26+
27+
return null;
28+
}
29+
30+
render() {
31+
return this.getMessage()
32+
}
33+
}
34+
35+
export default LazyLoading;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as LazyLoading } from './LazyLoading';

src/js/views/example/index.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import React, { Component, Fragment } from 'react';
22
import PropTypes from 'prop-types';
33
import { connect } from 'react-redux';
4+
import Loadable from 'react-loadable';
45

6+
import LazyLoading from '../../common/components/LazyLoading/LazyLoading'
57
import { actions as exampleActions } from '../../redux/modules/example';
68
import { exampleSelector } from '../../redux/selectors/exampleSelector';
7-
import { Example, ExampleWithError } from '../../common/components/Example';
9+
import { ExampleWithError } from '../../common/components/Example';
810
import { ErrorBoundary } from '../../common/components/Utilities';
911

1012
require('../../../style/index.css');
1113

14+
const LazyExample = Loadable({
15+
loader: () => import('../../common/components/Example/Example'),
16+
loading: LazyLoading,
17+
})
18+
1219
const mapStateToProps = state => ({
1320
example: exampleSelector(state),
1421
});
@@ -30,7 +37,7 @@ class ExampleView extends Component {
3037
render() {
3138
return (
3239
<Fragment>
33-
<Example {...this.props} />
40+
<LazyExample {...this.props} />
3441
<ErrorBoundary>
3542
<ExampleWithError {...this.props} />
3643
</ErrorBoundary>

webpack.config.common.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ const COMMON_LOADERS = [
6868
loader: 'babel-loader',
6969
options: {
7070
cacheDirectory: true,
71-
plugins: ['transform-runtime', 'transform-decorators-legacy'],
71+
plugins: [
72+
'transform-runtime',
73+
'transform-decorators-legacy',
74+
'syntax-dynamic-import'
75+
],
7276
},
7377
},
7478
{

0 commit comments

Comments
 (0)