Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18 react hot loader implementation #69

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions 18 ReactHotLoader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"scripts": {
"clean": "rimraf dist",
"postinstall": "tsd reinstall --overwrite --save",
"start": "webpack-dev-server",
"test": "karma start"
Expand All @@ -15,7 +16,7 @@
"lodash": "^4.5.1",
"object-assign": "^4.0.1",
"q": "^1.4.1",
"react": "^15.2.1",
"react": "^15.3.0",
"react-dom": "^15.2.1",
"react-redux": "^4.4.5",
"react-router": "^2.5.2",
Expand All @@ -41,14 +42,16 @@
"karma-webpack": "^1.7.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^15.2.1",
"react-hot-loader": "^1.3.0",
"redux-mock-store": "^1.1.2",
"rimraf": "^2.5.4",
"sinon": "^1.17.3",
"style-loader": "^0.13.0",
"ts-loader": "^0.8.1",
"tsd": "^0.6.5",
"typescript": "^1.8.9",
"url-loader": "^0.5.7",
"webpack": "^1.12.13",
"webpack-dev-server": "~1.10.1"
"webpack-dev-server": "^1.14.1"
}
}
84 changes: 84 additions & 0 deletions 18 ReactHotLoader/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# React Hot Loader + Redux dev tool support
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were writing this info in AboutPage, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the about page we can have a summary, but is a good idea to have detailed readme.md when a given user navigates to this sample in Github the readme.md will be automatically displayed


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.

Redux dev tool is a chrome add-on that allows us to browse the state, replay actions, inject actions, export / import state...

# Steps to configure hot Loader

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.

Packages to install (dev dependencies):

- react-hot-loader

```
npm install react-hot-loader --save-dev
```

webpack config updates:

```javascript
//(...)
module.exports = {

// TODO: remove hot loading entry points in production
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./index.tsx',
'./css/site.css',
'../node_modules/toastr/build/toastr.css',
'../node_modules/bootstrap/dist/css/bootstrap.css'
],

devServer: {
contentBase: './dist', //Content base
inline: true, //Enable watch and live reload
host: 'localhost',
port: 8080,
noInfo: true,
hot: true,
historyApiFallback: true
},


// (...)

module: {
loaders: [
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
loaders: ['react-hot', 'ts']
},
// (..)
},

plugins:[
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
// (...)
]
}
```


# Steps to configure Redux dev tool

First we have to download the app from the chrome store.

Then we have to add a line of code to the create store to check
wether is enabled the dev tool (app.tsx).

````javascript
let store = createStore(
reducers,
compose(
applyMiddleware(ReduxThunk)
,nonTypedWindow.devToolsExtension ? nonTypedWindow.devToolsExtension() : f => f
)
);
````
3 changes: 3 additions & 0 deletions 18 ReactHotLoader/src/components/about/aboutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class About extends React.Component<Props, {}> {
return (
<div className="row">
<h2>About Page</h2>
<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>
<p>Redux dev tool is a chrome add-on that allows us to browse the state, replay actions, inject actions, export / import state...</p>
<p>More info about how this sample works in readme.md</p>
<Link to="/members">Members</Link>
</div>
);
Expand Down
11 changes: 8 additions & 3 deletions 18 ReactHotLoader/src/components/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { createStore, applyMiddleware } from 'redux';
import { createStore, applyMiddleware, compose } from 'redux';
import { Provider } from 'react-redux';
import Header from './common/header';
import reducers from '../reducers';
Expand All @@ -9,9 +9,14 @@ import SpinnerContainer from './common/spinner.container';
interface Props extends React.Props<App> {
}

const nonTypedWindow : any = window;

let store = createStore(
reducers
,applyMiddleware(ReduxThunk)
reducers,
compose(
applyMiddleware(ReduxThunk)
,nonTypedWindow.devToolsExtension ? nonTypedWindow.devToolsExtension() : f => f
)
);

export default class App extends React.Component<Props, {}> {
Expand Down
23 changes: 15 additions & 8 deletions 18 ReactHotLoader/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ module.exports = {
extensions: ['', '.js', '.ts', '.tsx']
},

// Toake into account: remove hot loading entry points in production
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./index.tsx',
'./css/site.css',
'../node_modules/toastr/build/toastr.css',
Expand All @@ -26,23 +29,25 @@ module.exports = {
filename: 'bundle.js'
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still using this configuration instead of create devServer.js and more dependencies like express.

Configuration:

devServer: {
    contentBase: './dist', //Content base
    inline: true, //Enable watch and live reload
    host: 'localhost',
    port: 8080,
    hot: true
  },

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Will.give a try

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


//https://webpack.github.io/docs/webpack-dev-server.html#webpack-dev-server-cli
devServer: {
contentBase: './dist', //Content base
inline: true, //Enable watch and live reload
host: 'localhost',
port: 8080
},
contentBase: './dist', //Content base
inline: true, //Enable watch and live reload
host: 'localhost',
port: 8080,
noInfo: true,
hot: true,
historyApiFallback: true
},

// http://webpack.github.io/docs/configuration.html#devtool
devtool: 'inline-source-map',
devtool: 'source-map',

module: {
loaders: [
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
loader: 'ts-loader'
loaders: ['react-hot', 'ts']
},
//Note: Doesn't exclude node_modules to load bootstrap
{
Expand All @@ -64,6 +69,8 @@ module.exports = {
},

plugins:[
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why we need this plugin. It's to avoid show erros while webpack is compiling

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove it and.checi

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//Generate index.html in /dist => https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html', //Name of file in ./dist/
Expand Down
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ start working with React and Typescript. Characteristics:

+ Using Immutablejs.
+ Using React Hot Loader.

##Call for contributors:

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.
Expand Down Expand Up @@ -166,3 +166,12 @@ Redux Saga it's an interesting alternative for redux-thunk, worth to take a look
Use webpack require.ensure to load routes on demand.

* [Lazy Loading Webpack / React Router](http://blog.mxstbr.com/2016/01/react-apps-with-pages/)

## 18 Add support for ReactHotloader and ReduxDev Tools.

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.

Redux dev tool is a chrome add-on that allows us to browse the state, replay actions, inject actions, export / import state...

* [React Hot Loader](https://github.com/gaearon/react-hot-loader)
* [Redux Dev Tool](https://github.com/gaearon/redux-devtools)