babel / babel/minify

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

Open
#738 1 comment 1 reaction 1 assignee Claimed by @vigneshshanmugam View on GitHub
bug
Dominant language
JavaScript
Stars
4.4k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.