Skip to content

Commit 22063d4

Browse files
author
Daniel Levitt
committed
Fix hot reloading
Thanks to reduxjs/redux#1455
1 parent 4b27c81 commit 22063d4

File tree

8 files changed

+65
-65
lines changed

8 files changed

+65
-65
lines changed

.babelrc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,5 @@
22
"presets": ["es2015", "stage-2", "react"],
33
"plugins": [
44
"transform-class-properties"
5-
],
6-
"env": {
7-
"development": {
8-
"plugins": [
9-
["react-transform", {
10-
"transforms": [{
11-
"transform": "react-transform-hmr",
12-
"imports": ["react"],
13-
"locals": ["module"]
14-
}]
15-
}]
16-
]
17-
}
18-
}
5+
]
196
}

app/scripts/index.jsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import 'intl';
22
import React from 'react';
3-
import { render } from 'react-dom';
3+
import ReactDOM from 'react-dom';
44
import { Provider } from 'react-redux';
55
import { createHashHistory } from 'history';
6-
import { Router, useRouterHistory } from 'react-router';
7-
import { IntlProvider } from 'react-intl';
8-
import routes from 'routes/routes';
6+
import { useRouterHistory } from 'react-router';
97
import { syncHistoryWithStore } from 'react-router-redux';
108
import { store } from 'stores/configureStore';
119
import 'styles/base.css';
@@ -16,12 +14,32 @@ const appHistory = syncHistoryWithStore(hashHistory, store);
1614
const state = store.getState();
1715
const target = document.getElementById('app');
1816

19-
const node = (
20-
<Provider store={store}>
21-
<IntlProvider locale={state.settings.intlData.id} messages={state.settings.intlData.messages}>
22-
<Router history={appHistory}>{routes}</Router>
23-
</IntlProvider>
24-
</Provider>
25-
);
17+
let render = () => {
18+
const Root = require('./root').default;
19+
ReactDOM.render(
20+
<Provider store={store}>
21+
<Root {...{ state, appHistory }} />
22+
</Provider>,
23+
target
24+
);
25+
};
2626

27-
render(node, target);
27+
if (module.hot) {
28+
const renderApp = render;
29+
const renderError = (err) => {
30+
const RedBox = require('redbox-react');
31+
ReactDOM.render(<RedBox error={err} />, target);
32+
};
33+
34+
render = () => {
35+
try {
36+
renderApp();
37+
} catch (err) {
38+
renderError(err);
39+
}
40+
};
41+
42+
module.hot.accept('./root', () => setTimeout(render));
43+
}
44+
45+
render();

app/scripts/root.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import { Router } from 'react-router';
3+
import { IntlProvider } from 'react-intl';
4+
import routes from 'routes/';
5+
6+
export default ({ state, appHistory }) => (
7+
<IntlProvider locale={state.settings.intlData.id} messages={state.settings.intlData.messages}>
8+
<Router history={appHistory} routes={routes} />
9+
</IntlProvider>
10+
);

app/scripts/routes/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { App, Playlist, Settings } from 'containers/';
2+
import { ImportOptions, ImportKakapo, ImportSearch, ImportCustomUrl } from 'components/';
3+
4+
export default {
5+
path: '/',
6+
component: App,
7+
indexRoute: { component: ImportOptions },
8+
childRoutes: [
9+
{ path: 'settings', component: Settings },
10+
{ path: 'kakapo', component: ImportKakapo },
11+
{ path: 'youtube', component: ImportSearch },
12+
{ path: 'soundcloud', component: ImportSearch },
13+
{ path: 'custom', component: ImportCustomUrl },
14+
{ path: 'playlist(/:playlistId)', component: Playlist }
15+
]
16+
};

app/scripts/routes/routes.jsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/scripts/stores/configureStore.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,10 @@ function configureStore(debug = false) {
1010
/* istanbul ignore if */
1111
if (debug) enhancer.push(devTools());
1212

13-
const store = compose(...enhancer)(createStore)(
13+
return compose(...enhancer)(createStore)(
1414
rootReducer,
1515
window.__INITIAL_STATE__
1616
);
17-
18-
/* istanbul ignore if */
19-
if (module.hot) {
20-
module.hot.accept('../reducers', () => {
21-
const nextRootReducer = require('../reducers/index');
22-
23-
store.replaceReducer(nextRootReducer);
24-
});
25-
}
26-
27-
return store;
2817
}
2918

3019
export const store = configureStore(__DEV__ && !__TEST__);

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"babel-cli": "^6.6.5",
4242
"babel-eslint": "^5.0.0",
4343
"babel-loader": "^6.2.4",
44-
"babel-plugin-react-transform": "^2.0.2",
4544
"babel-plugin-transform-class-properties": "^6.6.0",
4645
"babel-preset-es2015": "^6.6.0",
4746
"babel-preset-react": "^6.5.0",
@@ -88,7 +87,7 @@
8887
"postcss-nested": "^1.0.0",
8988
"rcedit": "^0.4.0",
9089
"react-addons-test-utils": "^0.14.7",
91-
"react-transform-hmr": "^1.0.4",
90+
"redbox-react": "^1.2.2",
9291
"remote-redux-devtools": "^0.1.4",
9392
"request": "^2.69.0",
9493
"sinon": "^1.17.3",

tools/webpack.config.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ const config = {
3737
debug: DEBUG,
3838
devtool: DEBUG ? '#eval' : false,
3939
plugins: [
40-
new webpack.ProvidePlugin({
41-
fetch: 'imports?this=>global!exports?global.fetch!whatwg-fetch'
42-
}),
43-
new webpack.IgnorePlugin(/react\/lib\/ReactContext/),
44-
new webpack.optimize.OccurenceOrderPlugin(),
4540
new webpack.DefinePlugin({
4641
'process.env': {
4742
NODE_ENV: JSON.stringify(DEBUG ? 'development' : 'production')
@@ -51,6 +46,11 @@ const config = {
5146
__DEV__: DEBUG,
5247
__TEST__: TEST
5348
}),
49+
new webpack.ProvidePlugin({
50+
fetch: 'imports?this=>global!exports?global.fetch!whatwg-fetch'
51+
}),
52+
new webpack.IgnorePlugin(/react\/lib\/ReactContext/),
53+
new webpack.optimize.OccurenceOrderPlugin(),
5454
new ExtractTextPlugin('styles.css'),
5555
...(!DEBUG ? [
5656
new webpack.optimize.DedupePlugin(),
@@ -61,9 +61,7 @@ const config = {
6161
}
6262
}),
6363
new webpack.optimize.AggressiveMergingPlugin()
64-
] : [
65-
new webpack.NoErrorsPlugin()
66-
]),
64+
] : []),
6765
...(!TEST ? [ new webpack.HotModuleReplacementPlugin() ] : [])
6866
],
6967
module: {

0 commit comments

Comments
 (0)