`StubArray` workaround is incompatible with terser's `unsafe_arrows` option
Intended outcome:
MobX should work with terser's unsafe_arrows optimisation option.
Actual outcome:
TypeError: Object.setPrototypeOf called on null or undefined
Info
https://github.com/mobxjs/mobx/blob/58d432837dbd49acf3d73ed2609967358d423702/packages/mobx/src/types/legacyobservablearray.ts#L38-L49
The mobx codebases utilises an empty class here.
This empty class is downlevelled by mobx's build process to be var StubArray = function StubArray() {}; (unpkg).
When terser runs with the [unsafe_arrows](https://terser.org/docs/options/#:~:text=unsafe_arrows%20(default%3A,2015%20or%20greater.) option it will convert all functions that don't reference this to an arrow, converting the above line to var StubArray = () => {}. This then breaks the inherit function which assumes StubArray.proto is defined.
Note: unsafe_arrows is (by definition) an unsafe optimisation - though in my experience it is generally safe because it's so rare to do operations on a function's prototype (outside of old-school "class" code).
It would be great if this line could be changed to make mobx more minifyable.
Do you have an idea for workaround?
It's hard to say. TBH I'm not exactly sure why this workaround even exists! I can see it was added in https://github.com/mobxjs/mobx/commit/a9b5ed2559be4466fa1e1bb3735864d415324db3 but the commit has no attached PR nor does it have any useful commit message.
In that commit I can see that there used to be some code that mutated the prototype - but that code is long since gone -- in v6 from the looks of it based on this comment: https://github.com/mobxjs/mobx/blob/58d432837dbd49acf3d73ed2609967358d423702/packages/mobx/src/types/legacyobservablearray.ts#L51-L53
Seems to me like this code could just be class LegacyObservableArray<T> extends Array?
It also seems odd to me that MobX's build process downlevels classes to functions since all browser have had support since around 2016.
This workaround is not fixable I think as the arrow functions cannot act as base classes. The reason this downlevels this way is IIRC because when proxies are not used, this was the only way to reasonably correct inherit from the built-in array. If LegacyArray extends Array works nowadays, happy approve a PR for that!
I know it was a limitation in the past, and disabling proxies and using legacy array in the first place, is probably to target something from the past :) (Will be removed in next major)