mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
|
|
import * as thunkModule from 'redux-thunk';
|
|
import { syncHistory, routeReducer } from 'react-router-redux';
|
|
import * as Store from './store';
|
|
|
|
export default function configureStore(history: HistoryModule.History, initialState?: Store.ApplicationState) {
|
|
// Build middleware
|
|
const thunk = (thunkModule as any).default; // Workaround for TypeScript not importing thunk module as expected
|
|
const reduxRouterMiddleware = syncHistory(history);
|
|
const middlewares = [thunk, reduxRouterMiddleware];
|
|
const devToolsExtension = (window as any).devToolsExtension; // If devTools is installed, connect to it
|
|
|
|
const finalCreateStore = compose(
|
|
applyMiddleware(...middlewares),
|
|
devToolsExtension ? devToolsExtension() : f => f
|
|
)(createStore)
|
|
|
|
// Combine all reducers
|
|
const allReducers = buildRootReducer(Store.reducers);
|
|
|
|
const store = finalCreateStore(allReducers, initialState) as Redux.Store;
|
|
|
|
// Required for replaying actions from devtools to work
|
|
reduxRouterMiddleware.listenForReplays(store);
|
|
|
|
// Enable Webpack hot module replacement for reducers
|
|
if (module.hot) {
|
|
module.hot.accept('./store', () => {
|
|
const nextRootReducer = require<typeof Store>('./store');
|
|
store.replaceReducer(buildRootReducer(nextRootReducer.reducers));
|
|
});
|
|
}
|
|
|
|
return store;
|
|
}
|
|
|
|
function buildRootReducer(allReducers) {
|
|
return combineReducers(Object.assign({}, allReducers, { routing: routeReducer })) as Redux.Reducer;
|
|
}
|