Skip to content

Commit 3fe377a

Browse files
authored
Merge pull request #69 from Lemoncode/18_React_Hot_Loader_Implementation
18 react hot loader implementation
2 parents 604c5df + 744234a commit 3fe377a

File tree

6 files changed

+125
-14
lines changed

6 files changed

+125
-14
lines changed

18 ReactHotLoader/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"scripts": {
6+
"clean": "rimraf dist",
67
"postinstall": "tsd reinstall --overwrite --save",
78
"start": "webpack-dev-server",
89
"test": "karma start"
@@ -15,7 +16,7 @@
1516
"lodash": "^4.5.1",
1617
"object-assign": "^4.0.1",
1718
"q": "^1.4.1",
18-
"react": "^15.2.1",
19+
"react": "^15.3.0",
1920
"react-dom": "^15.2.1",
2021
"react-redux": "^4.4.5",
2122
"react-router": "^2.5.2",
@@ -41,14 +42,16 @@
4142
"karma-webpack": "^1.7.0",
4243
"mocha": "^2.4.5",
4344
"react-addons-test-utils": "^15.2.1",
45+
"react-hot-loader": "^1.3.0",
4446
"redux-mock-store": "^1.1.2",
47+
"rimraf": "^2.5.4",
4548
"sinon": "^1.17.3",
4649
"style-loader": "^0.13.0",
4750
"ts-loader": "^0.8.1",
4851
"tsd": "^0.6.5",
4952
"typescript": "^1.8.9",
5053
"url-loader": "^0.5.7",
5154
"webpack": "^1.12.13",
52-
"webpack-dev-server": "~1.10.1"
55+
"webpack-dev-server": "^1.14.1"
5356
}
5457
}

18 ReactHotLoader/readme.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# React Hot Loader + Redux dev tool support
2+
3+
React Hot loader allows us to introduce changes in the application
4+
source code meanwhile we are running our web server and get the changes into our page without having to reload the browser and not loosing the application
5+
state.
6+
7+
Redux dev tool is a chrome add-on that allows us to browse the state, replay actions, inject actions, export / import state...
8+
9+
# Steps to configure hot Loader
10+
11+
You don't need to update the source code of the app, all we have to do is install a list of npm packages, add some babel configuration, and then set a web server + updates on the webpack.config.
12+
13+
Packages to install (dev dependencies):
14+
15+
- react-hot-loader
16+
17+
```
18+
npm install react-hot-loader --save-dev
19+
```
20+
21+
webpack config updates:
22+
23+
```javascript
24+
//(...)
25+
module.exports = {
26+
27+
// TODO: remove hot loading entry points in production
28+
entry: [
29+
'webpack-dev-server/client?http://localhost:8080',
30+
'webpack/hot/only-dev-server',
31+
'./index.tsx',
32+
'./css/site.css',
33+
'../node_modules/toastr/build/toastr.css',
34+
'../node_modules/bootstrap/dist/css/bootstrap.css'
35+
],
36+
37+
devServer: {
38+
contentBase: './dist', //Content base
39+
inline: true, //Enable watch and live reload
40+
host: 'localhost',
41+
port: 8080,
42+
noInfo: true,
43+
hot: true,
44+
historyApiFallback: true
45+
},
46+
47+
48+
// (...)
49+
50+
module: {
51+
loaders: [
52+
{
53+
test: /\.(ts|tsx)$/,
54+
exclude: /node_modules/,
55+
loaders: ['react-hot', 'ts']
56+
},
57+
// (..)
58+
},
59+
60+
plugins:[
61+
new webpack.HotModuleReplacementPlugin(),
62+
new webpack.NoErrorsPlugin(),
63+
// (...)
64+
]
65+
}
66+
```
67+
68+
69+
# Steps to configure Redux dev tool
70+
71+
First we have to download the app from the chrome store.
72+
73+
Then we have to add a line of code to the create store to check
74+
wether is enabled the dev tool (app.tsx).
75+
76+
````javascript
77+
let store = createStore(
78+
reducers,
79+
compose(
80+
applyMiddleware(ReduxThunk)
81+
,nonTypedWindow.devToolsExtension ? nonTypedWindow.devToolsExtension() : f => f
82+
)
83+
);
84+
````

18 ReactHotLoader/src/components/about/aboutPage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default class About extends React.Component<Props, {}> {
1010
return (
1111
<div className="row">
1212
<h2>About Page</h2>
13+
<p>React Hot loader allows us to introduce changes in the application source code meanwhile we are running our web server and get the changes into our page without having to reload the browser and not loosing the application state.</p>
14+
<p>Redux dev tool is a chrome add-on that allows us to browse the state, replay actions, inject actions, export / import state...</p>
15+
<p>More info about how this sample works in readme.md</p>
1316
<Link to="/members">Members</Link>
1417
</div>
1518
);

18 ReactHotLoader/src/components/app.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { createStore, applyMiddleware } from 'redux';
2+
import { createStore, applyMiddleware, compose } from 'redux';
33
import { Provider } from 'react-redux';
44
import Header from './common/header';
55
import reducers from '../reducers';
@@ -9,9 +9,14 @@ import SpinnerContainer from './common/spinner.container';
99
interface Props extends React.Props<App> {
1010
}
1111

12+
const nonTypedWindow : any = window;
13+
1214
let store = createStore(
13-
reducers
14-
,applyMiddleware(ReduxThunk)
15+
reducers,
16+
compose(
17+
applyMiddleware(ReduxThunk)
18+
,nonTypedWindow.devToolsExtension ? nonTypedWindow.devToolsExtension() : f => f
19+
)
1520
);
1621

1722
export default class App extends React.Component<Props, {}> {

18 ReactHotLoader/webpack.config.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ module.exports = {
1414
extensions: ['', '.js', '.ts', '.tsx']
1515
},
1616

17+
// Toake into account: remove hot loading entry points in production
1718
entry: [
19+
'webpack-dev-server/client?http://localhost:8080',
20+
'webpack/hot/only-dev-server',
1821
'./index.tsx',
1922
'./css/site.css',
2023
'../node_modules/toastr/build/toastr.css',
@@ -26,23 +29,25 @@ module.exports = {
2629
filename: 'bundle.js'
2730
},
2831

29-
//https://webpack.github.io/docs/webpack-dev-server.html#webpack-dev-server-cli
3032
devServer: {
31-
contentBase: './dist', //Content base
32-
inline: true, //Enable watch and live reload
33-
host: 'localhost',
34-
port: 8080
35-
},
33+
contentBase: './dist', //Content base
34+
inline: true, //Enable watch and live reload
35+
host: 'localhost',
36+
port: 8080,
37+
noInfo: true,
38+
hot: true,
39+
historyApiFallback: true
40+
},
3641

3742
// http://webpack.github.io/docs/configuration.html#devtool
38-
devtool: 'inline-source-map',
43+
devtool: 'source-map',
3944

4045
module: {
4146
loaders: [
4247
{
4348
test: /\.(ts|tsx)$/,
4449
exclude: /node_modules/,
45-
loader: 'ts-loader'
50+
loaders: ['react-hot', 'ts']
4651
},
4752
//Note: Doesn't exclude node_modules to load bootstrap
4853
{
@@ -64,6 +69,8 @@ module.exports = {
6469
},
6570

6671
plugins:[
72+
new webpack.HotModuleReplacementPlugin(),
73+
new webpack.NoErrorsPlugin(),
6774
//Generate index.html in /dist => https://github.com/ampedandwired/html-webpack-plugin
6875
new HtmlWebpackPlugin({
6976
filename: 'index.html', //Name of file in ./dist/

readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ start working with React and Typescript. Characteristics:
1616

1717
+ Using Immutablejs.
1818
+ Using React Hot Loader.
19-
19+
2020
##Call for contributors:
2121

2222
Some months ago this project started as something internal... let's create some simple samples that cover react / redux / typescript scenarios that could serve as a guidance and reference in the future... now, we and other developers are using this repo as quick by sample guidance. We keep on adding more samples to it, but we have found that older samples need some updates / refactoring.
@@ -166,3 +166,12 @@ Redux Saga it's an interesting alternative for redux-thunk, worth to take a look
166166
Use webpack require.ensure to load routes on demand.
167167

168168
* [Lazy Loading Webpack / React Router](http://blog.mxstbr.com/2016/01/react-apps-with-pages/)
169+
170+
## 18 Add support for ReactHotloader and ReduxDev Tools.
171+
172+
React Hot loader allows us to introduce changes in the application source code meanwhile we are running our web server and get the changes into our page without having to reload the browser and not losing the application state.
173+
174+
Redux dev tool is a chrome add-on that allows us to browse the state, replay actions, inject actions, export / import state...
175+
176+
* [React Hot Loader](https://github.com/gaearon/react-hot-loader)
177+
* [Redux Dev Tool](https://github.com/gaearon/redux-devtools)

0 commit comments

Comments
 (0)