Skip to content

Commit

Permalink
hot reloading, works. reducers cannot be imported via alias or else i…
Browse files Browse the repository at this point in the history
…t fails
  • Loading branch information
bdefore committed Jan 12, 2016
1 parent b5e5b56 commit 436d748
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
1 change: 0 additions & 1 deletion bin/merge-configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ module.exports = (userConfig) => {
// add routes and reducer aliases so that client has access to them
combinedWebpackConfig.resolve.alias = combinedWebpackConfig.resolve.alias || {};
combinedWebpackConfig.resolve.alias.routes = universalReduxConfig.routes;
combinedWebpackConfig.resolve.alias.reducers = universalReduxConfig.redux.reducers;
if (universalReduxConfig.redux.middleware) {
combinedWebpackConfig.resolve.alias.middleware = universalReduxConfig.redux.middleware;
} else {
Expand Down
2 changes: 0 additions & 2 deletions bin/user-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const path = require('path');

const userConfigPath = path.join(process.cwd(), './config/universal-redux.config.js');
console.log('getting config', userConfigPath);

function getConfig() {
try {
console.log('Trying to load project config from ', userConfigPath);
const config = require(path.resolve(userConfigPath));
console.log('Loaded project level config', config);
return config;
Expand Down
2 changes: 1 addition & 1 deletion config/universal-redux.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module.exports = (projectRoot, sourceRoot) => {
//
// Expects: String
*/
// middleware: sourceRoot + '/redux/middleware/index.js',
middleware: sourceRoot + '/redux/middleware/index.js',
},

/*
Expand Down
3 changes: 1 addition & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { Router, browserHistory } from 'react-router';
// dependencies of external source. these resolve via webpack aliases
// as assigned in merge-configs.js
import getRoutes from 'routes';
import reducers from 'reducers';
import middleware from 'middleware';

const dest = document.getElementById('content');
const store = createStore(middleware, browserHistory, reducers, window.__data);
const store = createStore(middleware, browserHistory, window.__data);

const component = (
<Router history={browserHistory}>
Expand Down
3 changes: 1 addition & 2 deletions src/server/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default (projectConfig, projectToolsConfig) => {
const tools = getTools(projectConfig, projectToolsConfig);
const config = configure(projectConfig);
const getRoutes = require(path.resolve(config.routes)).default;
const reducers = require(path.resolve(config.redux.reducers)).default;
const pretty = new PrettyError();

let CustomHtml;
Expand All @@ -39,7 +38,7 @@ export default (projectConfig, projectToolsConfig) => {

const middleware = config.redux.middleware ? require(path.resolve(config.redux.middleware)).default : [];
const history = createMemoryHistory();
const store = createStore(middleware, history, reducers);
const store = createStore(middleware, history);

function hydrateOnClient() {
res.status(200).send('<!doctype html>\n' + ReactDOM.renderToString(<CustomHtml assets={tools.assets()} store={store} headers={res._headers} />));
Expand Down
45 changes: 35 additions & 10 deletions src/shared/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import createLogger from 'redux-logger';
import { syncHistory } from 'redux-simple-router';
import { createStore as _createStore, applyMiddleware, compose } from 'redux';
import reducers from '../../../../src/redux/modules';

export default function createStore(customMiddleware, history, reducers, data) {
function hmr(store) {
// Attempt to assign to module.hot.accept in routes
// since that is relative to the files we care about
// Alternatively we could assign to universal-redux
// create.js around line 32 but that behaves the same.

// Verify HMR is available
// console.log('module.hot', module.hot);

if (module.hot) {
// Verify HMR is active
// console.log('module.hot is active', module.hot.active);

// Verify that universal-redux npm module is passing store
// See universal-redux client.js line 18
// See universal-redux renderer.js line 53
// console.log('has store?', store);

module.hot.accept('../../../../src/redux/modules', () => {
// But the accept handler never fires!
// console.log('in accept handler updating', updatedDependencies);

const nextRootReducer = require('../../../../src/redux/modules/index').default;

// Log out next root reducer
// console.log('root reducer', nextRootReducer);

store.replaceReducer(nextRootReducer);
});
}
}

export default function createStore(customMiddleware, history, data) {
const router = syncHistory(history);
const defaultMiddleware = [ router ];
const middleware = customMiddleware.concat(defaultMiddleware);
Expand All @@ -29,15 +62,7 @@ export default function createStore(customMiddleware, history, reducers, data) {
// only necessary for devtools https://github.com/rackt/redux-simple-router/pull/141#issuecomment-167587581
router.syncHistoryToStore(store);

// if (__DEVELOPMENT__ && module.hot) {
// console.log('assigning module.hot.accept');
// module.hot.accept('../../../../src/redux/modules', (updatedDependencies) => {
// // module.hot.accept('redux/modules', (updatedDependencies) => {
// console.log('accept', updatedDependencies);
// const nextRootReducer = require('../../../../src/redux/modules/index').default;
// store.replaceReducer(nextRootReducer);
// });
// }
hmr(store);

return store;
}

0 comments on commit 436d748

Please sign in to comment.