mobxjs / mobx-react-lite

Lightweight React bindings for MobX based on React 16.8 and Hooks
https://mobx.js.org/react-integration.html
MIT License
2.13k stars 91 forks source link

Naked import statement to batchingForReactDom tree-shaken in production build #273

Closed JonatanRydh closed 4 years ago

JonatanRydh commented 4 years ago

In documentation it states that you should simply add: import 'mobx-react-lite/batchingForReactDom';

to configure batching. In many webpack builds however it will not work since it will be tree-shaken if it is not declared having side-effects. I guess the solution is to add: "sideEffects": ["batching*"] to package.json in this project.

A workaround until fixed is for users to explicit configure this like this:

import ReactDOM from 'react-dom';
import { observerBatching } from 'mobx-react-lite';
observerBatching(ReactDOM.unstable_batchedUpdates);
danielkcz commented 4 years ago

Thanks, I did not realize that. I wonder if sideEffects field is somewhat standardized. Will other bundlers supporting tree shaking respect it as well?

JonatanRydh commented 4 years ago

I'm not sure how standardized it is, but at least the standard webpack configuration that comes with latest react-script will look at the current "sideEffects":false and happily ignore that import in production builds. So I'm thinking this should be a fairly common problem as it is now.

danielkcz commented 4 years ago

Alright, 2.0.5 is out. I tested it against the prod build of CRA and seems correct.

JonatanRydh commented 4 years ago

Confirmed, it works in 2.0.5.

viridia commented 4 years ago

I'm really confused about this warning, it says to import a module from mobx-react-lite, but I'm using mobx-react, which has no such import.

danielkcz commented 4 years ago

@viridia It certainly does have it since 6.2.0