esamattis / immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
http://npm.im/immer-reducer
MIT License
225 stars 15 forks source link

customName required when minifying with terser #11

Closed aleffert closed 5 years ago

aleffert commented 5 years ago

I set up a new project using create react app and everything worked fine in dev, but when I did a production build, my actions mysteriously weren't executing. I was able to solve this problem by setting a customName on my class. Also some wonky stuff was going on with class names (they all seemed to be t) when I went to debug so I am pretty sure it was a minification problem.

It'd probably be good to document that you pretty much need to set up customName if you're using a minifier (or figure out some clever way around this).

p.s. this library was pretty much exactly what I was looking for. Thanks!

esamattis commented 5 years ago

That's weird. It should not be required. Can you put out repro repository somewhere with webpack config etc. so I can take a look?

Thanks for your kind words!

esamattis commented 5 years ago

If you have two reducer classes in different files the minifier might indeed generate same names for them during the name mangling but that case should be handled by the duplicate name detection:

https://github.com/epeli/immer-reducer/blob/ebd04a1a0c2be01c289d76dd31c15451d292d47b/src/immer-reducer.ts#L148-L164

This happens also when you dynamically generate reducer classes by returning them from a function.

Not sure why this is not working for you.

aleffert commented 5 years ago

I didn't have time to pull out an example, but I think I figured out the problem. It looks like the dedupe protection is all in createReducers? I was calling createActions first. When I swap the order so createReducers is first, the problem goes away. This makes sense, since the dedupe protection would actually change the customName after I've made the action creators and they'd no longer match.

esamattis commented 5 years ago

Oh that makes sense because createReducerFunction() actually mutates the classes by setting the customName automatically if it finds colliding class names.

esamattis commented 5 years ago

Fix released in 0.2.1. Thanks for reporting!