Skip to content

StubArray workaround is incompatible with terser's unsafe_arrows option #3825

@bradzacher

Description

@bradzacher

Intended outcome:

MobX should work with terser's unsafe_arrows optimisation option.

Actual outcome:

TypeError: Object.setPrototypeOf called on null or undefined

Info

// Typescript workaround to make sure ObservableArray extends Array
class StubArray {}
function inherit(ctor, proto) {
if (Object.setPrototypeOf) {
Object.setPrototypeOf(ctor.prototype, proto)
} else if (ctor.prototype.__proto__ !== undefined) {
ctor.prototype.__proto__ = proto
} else {
ctor.prototype = proto
}
}
inherit(StubArray, Array.prototype)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions