Skip to content

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

Open

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.

Activity

  1. added
    bug
    on Nov 27, 2017
  2. added this to the 1.0 milestone on Dec 10, 2017
  3. redneb commented on May 13, 2018

    @redneb
    Author

    This has not been resolved. With the latest version of babel-minify I still have a problem when the above example is written with Object.defineProperty, e.g.

    (function () {
    	Object.defineProperty(
    		Math,
    		'addOne', {
    			value: function(x) {return x + 1}
    		});
    	console.log(Math.addOne(1), Math.addOne(2));
    }());
    

    is minified to:

    (function() {
        var a = Math.addOne;
        Object.defineProperty(Math, "addOne", {
            value: function(a) {
                return a + 1
            }
        }), console.log(a(1), a(2))
    })();
    

    As you can see, we have the same problem as before, i.e. the alias a to the method Math.addOne is defined before the method itself. This breaks core-js which uses Object.defineProperty to define its polyfills.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions