Skip to content

babel-minify breaks polyfills added to global built-in objects #738

@redneb

Description

@redneb

Suppose that I try to add a new method to the global Math object as follows:

(function () {
	Math["addOne"] = function(x) {return x + 1};
	console.log(Math.addOne(1), Math.addOne(2));
}());

Then minifying that with the babel-minify cli application, I get the following output (pretty printed):

(function() {
    var a = Math.addOne;
    Math.addOne = function(a) {
        return a + 1
    },
    console.log(a(1), a(2))
}
)();

As you can see, an alias to the method is created, but that happens before the method was defined, so the alias will be invalid and the code will not work.

If change Math["addOne"] = ... to Math.addOne = ... in the definition of the method, then everything works fine. The problem is that in my actual code, I cannot control how the method is defined, because the method is a polyfill for Math.sign which is provided by core-js, which uses Object.defineProperty to define it.

Metadata

Metadata

Labels

bugConfirmed bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions