Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit d75a64f

Browse files
authored
Enable HMR (#550)
1 parent 1b48cad commit d75a64f

File tree

11 files changed

+85
-51
lines changed

11 files changed

+85
-51
lines changed

src/components/App/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import makeClassName from 'classnames';
1111
import log from 'loglevel';
1212
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
1313

14-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
14+
import { ApplicationState } from '../../reducers';
15+
import { ConnectedReduxProps } from '../../configureStore';
1516
import styles from './styles.module.scss';
1617
import { ApiState, actions as apiActions } from '../../reducers/api';
1718
import {

src/components/FileTree/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import FileTreeNode, {
99
PublicProps as FileTreeNodeProps,
1010
} from '../FileTreeNode';
1111
import Loading from '../Loading';
12-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
12+
import { ApplicationState } from '../../reducers';
13+
import { ConnectedReduxProps } from '../../configureStore';
1314
import {
1415
FileTree,
1516
TreeNode,

src/components/LinterProvider/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from 'react';
22
import { connect } from 'react-redux';
33

4-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
4+
import { ApplicationState } from '../../reducers';
5+
import { ConnectedReduxProps } from '../../configureStore';
56
import {
67
LinterMessageMap,
78
LinterMessagesByPath,

src/components/Navbar/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Link } from 'react-router-dom';
55

66
import { gettext } from '../../utils';
77
import LoginButton from '../LoginButton';
8-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
8+
import { ApplicationState } from '../../reducers';
9+
import { ConnectedReduxProps } from '../../configureStore';
910
import { User, selectCurrentUser, requestLogOut } from '../../reducers/users';
1011
import styles from './styles.module.scss';
1112

src/components/VersionChooser/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { connect } from 'react-redux';
44
import { withRouter, RouteComponentProps } from 'react-router-dom';
55

66
import Loading from '../Loading';
7-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
7+
import { ApplicationState } from '../../reducers';
8+
import { ConnectedReduxProps } from '../../configureStore';
89
import VersionSelect from '../VersionSelect';
910
import { VersionsMap, fetchVersionsList } from '../../reducers/versions';
1011
import { gettext } from '../../utils';

src/configureStore.tsx

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Middleware,
55
Store,
66
applyMiddleware,
7-
combineReducers,
87
createStore,
98
} from 'redux';
109
import { composeWithDevTools } from 'redux-devtools-extension';
@@ -15,21 +14,7 @@ import thunk, {
1514
ThunkMiddleware,
1615
} from 'redux-thunk';
1716

18-
import api, { ApiState } from './reducers/api';
19-
import errors, { ErrorsState } from './reducers/errors';
20-
import fileTree, { FileTreeState } from './reducers/fileTree';
21-
import linter, { LinterState } from './reducers/linter';
22-
import users, { UsersState } from './reducers/users';
23-
import versions, { VersionsState } from './reducers/versions';
24-
25-
export type ApplicationState = {
26-
api: ApiState;
27-
errors: ErrorsState;
28-
fileTree: FileTreeState;
29-
linter: LinterState;
30-
users: UsersState;
31-
versions: VersionsState;
32-
};
17+
import createRootReducer, { ApplicationState } from './reducers';
3318

3419
export type ThunkActionCreator<PromiseResult = void> = ThunkAction<
3520
Promise<PromiseResult>,
@@ -48,37 +33,37 @@ export type ConnectedReduxProps<A extends Action = AnyAction> = {
4833
dispatch: ThunkDispatch<A>;
4934
};
5035

51-
const createRootReducer = () => {
52-
return combineReducers<ApplicationState>({
53-
api,
54-
errors,
55-
fileTree,
56-
linter,
57-
users,
58-
versions,
59-
});
60-
};
61-
6236
const configureStore = (
6337
preloadedState?: ApplicationState,
6438
): Store<ApplicationState> => {
6539
const allMiddleware: Middleware[] = [
6640
thunk as ThunkMiddleware<ApplicationState, AnyAction>,
6741
];
68-
let addDevTools = false;
42+
const isDevelopment = process.env.NODE_ENV === 'development';
6943

70-
if (process.env.NODE_ENV === 'development') {
44+
if (isDevelopment) {
7145
allMiddleware.push(createLogger());
72-
addDevTools = true;
7346
}
7447

7548
let middleware = applyMiddleware(...allMiddleware);
76-
if (addDevTools) {
49+
if (isDevelopment) {
7750
const composeEnhancers = composeWithDevTools({});
7851
middleware = composeEnhancers(middleware);
7952
}
8053

81-
return createStore(createRootReducer(), preloadedState, middleware);
54+
const store = createStore(createRootReducer(), preloadedState, middleware);
55+
56+
if (isDevelopment) {
57+
/* istanbul ignore next */
58+
if (module.hot) {
59+
/* istanbul ignore next */
60+
module.hot.accept('./reducers', () => {
61+
store.replaceReducer(createRootReducer());
62+
});
63+
}
64+
}
65+
66+
return store;
8267
};
8368

8469
export default configureStore;

src/index.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,25 @@ if (authToken === process.env.REACT_APP_AUTH_TOKEN_PLACEHOLDER) {
2121
);
2222
}
2323

24-
ReactDOM.render(
25-
<Provider store={store}>
26-
<BrowserRouter>
27-
<App authToken={authToken} />
28-
</BrowserRouter>
29-
</Provider>,
30-
rootElement,
31-
);
24+
const render = (AppComponent: typeof App) => {
25+
ReactDOM.render(
26+
<Provider store={store}>
27+
<BrowserRouter>
28+
<AppComponent authToken={authToken} />
29+
</BrowserRouter>
30+
</Provider>,
31+
rootElement,
32+
);
33+
};
34+
35+
render(App);
36+
37+
/* istanbul ignore next */
38+
if (module.hot) {
39+
/* istanbul ignore next */
40+
module.hot.accept('./components/App', () => {
41+
// eslint-disable-next-line global-require
42+
const NextApp = require('./components/App').default;
43+
render(NextApp);
44+
});
45+
}

src/pages/Browse/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { connect } from 'react-redux';
44
import { Col, Row } from 'react-bootstrap';
55
import log from 'loglevel';
66

7-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
7+
import { ApplicationState } from '../../reducers';
8+
import { ConnectedReduxProps } from '../../configureStore';
89
import { ApiState } from '../../reducers/api';
910
import FileTree from '../../components/FileTree';
1011
import {

src/pages/Compare/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { Col, Row } from 'react-bootstrap';
33
import { RouteComponentProps } from 'react-router-dom';
44
import { connect } from 'react-redux';
55

6-
import { ApplicationState, ConnectedReduxProps } from '../../configureStore';
6+
import { ApplicationState } from '../../reducers';
7+
import { ConnectedReduxProps } from '../../configureStore';
78
import FileTree from '../../components/FileTree';
89
import DiffView from '../../components/DiffView';
910
import Loading from '../../components/Loading';

src/reducers/index.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { combineReducers } from 'redux';
2+
3+
import api, { ApiState } from './api';
4+
import errors, { ErrorsState } from './errors';
5+
import fileTree, { FileTreeState } from './fileTree';
6+
import linter, { LinterState } from './linter';
7+
import users, { UsersState } from './users';
8+
import versions, { VersionsState } from './versions';
9+
10+
export type ApplicationState = {
11+
api: ApiState;
12+
errors: ErrorsState;
13+
fileTree: FileTreeState;
14+
linter: LinterState;
15+
users: UsersState;
16+
versions: VersionsState;
17+
};
18+
19+
const createRootReducer = () => {
20+
return combineReducers<ApplicationState>({
21+
api,
22+
errors,
23+
fileTree,
24+
linter,
25+
users,
26+
versions,
27+
});
28+
};
29+
30+
export default createRootReducer;

src/test-helpers.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { ShallowWrapper, shallow } from 'enzyme';
44
import { Store } from 'redux';
55
import log from 'loglevel';
66

7-
import configureStore, {
8-
ApplicationState,
9-
ThunkActionCreator,
10-
} from './configureStore';
7+
import configureStore, { ThunkActionCreator } from './configureStore';
8+
import { ApplicationState } from './reducers';
119
import {
1210
ExternalLinterResult,
1311
ExternalLinterMessage,

0 commit comments

Comments
 (0)