react-redux icon indicating copy to clipboard operation
react-redux copied to clipboard

Incompatability with Babel 6 + Webpack Hot Reload

Open kaibyao opened this issue 10 years ago • 13 comments

You can see a working example of this bug here: https://github.com/ohkaiby/webpack-reactredux-hotreload-bug

Basically store.replaceReducer() is passing in a babel6-transpiled reducer function, which is an object whose default property is the actual reducer function. However, replaceReducer (inside createStore.js) is expecting to run a function, so it ends up error'ing.

The problem is, I'm not sure whether this is a bug with Babel 6, babel-loader, or React-Redux. It seems that Babel is correctly transpiling local dependencies, but this results in an error where untranspiled ES5 code in React-Redux expecting to run a function. Therefore the real bug is with how Babel6-transpiled functions are passed vs. what untranspiled code is expecting to run.

kaibyao avatar Dec 12 '15 11:12 kaibyao

Hi @ohkaiby, thanks for the report (and creating a working example :+1:). I am looking at babel 6 stuff this weekend so hopefully we can get this resolved too.

ellbee avatar Dec 12 '15 11:12 ellbee

Just change

const updatedReducer = require('./reducer');

to

const updatedReducer = require('./reducer').default;

which matches the new semantics of Babel 6.

gaearon avatar Dec 12 '15 20:12 gaearon

This was the breaking change in Babel 6: removing backward compatibility with CommonJS for default exports. They are now explicit and ES6-centric rather than trying-to-be-like-CommonJS-but-not-quite.

gaearon avatar Dec 12 '15 20:12 gaearon

Or you can use this to support CommonJS default exports: https://github.com/59naga/babel-plugin-add-module-exports

chentsulin avatar Dec 13 '15 02:12 chentsulin

Thanks guys, did not realize the ES6 update broke CommonJS compatability. babel-plugin-add-module-exports ended up working.

kaibyao avatar Dec 14 '15 00:12 kaibyao

Thanks guys, did not realize the ES6 update broke CommonJS compatability.

It just changes semantics to match ES6 more closely. If you only develop an app (and not a library) just use require(...).default (or better ES6 :-) and don't worry.

gaearon avatar Dec 14 '15 09:12 gaearon

if (module.hot) {
    module.hot.accept('./reducer', () => {
        const updatedReducer = require('./reducer');
        createdStore.replaceReducer( updatedReducer );
    });
}

Is this bit required at all when using webpack HMR? I cannot find the documentation for it or how it ended up in my code. Removing it does not appear to have effect either.

gajus avatar Feb 11 '16 18:02 gajus

I can see it is required. HMR of reducers does not work without. I still cannot find the official docs reference.

gajus avatar Feb 11 '16 18:02 gajus

@gajus The documentation is unfortunately not in the README, it is in one of the release notes for Redux: https://github.com/rackt/redux/releases/tag/v2.0.0

kaibyao avatar Feb 11 '16 18:02 kaibyao

Thank you @ohkaiby

gajus avatar Feb 11 '16 18:02 gajus

I can see it is required. HMR of reducers does not work without. I still cannot find the official docs reference.

This library is React Redux. It has no relation to HMR, especially for reducers. Therefore there is no reason why this information would be in README.

gaearon avatar Feb 11 '16 19:02 gaearon

Sorry, @gaearon. Wrong thread to discuss the issue. Though I think @ohkaiby was talking about Redux documentation.

I couldn't find anything searching "site:rackt.org HMR", which would be useful. I came across this issue searching for "store.replaceReducer".

gajus avatar Feb 11 '16 19:02 gajus

Yeah, we don’t really have docs for HMR at the moment. It would be nice to add as a “recipe”, contributions to docs are always welcome! https://github.com/rackt/redux/issues/721

gaearon avatar Feb 11 '16 20:02 gaearon