babel-polyfills icon indicating copy to clipboard operation
babel-polyfills copied to clipboard

polyfill-corejs3 won't polyfill some Promise static methods with method: "usage-pure"

Open giniyat202 opened this issue 4 years ago • 1 comments

I've been experimenting with polyfill-corejs3 a bit and tried the following setup (also on this repo https://github.com/giniyat202/babel-polyfill-promise):

.browserslist:

chrome >= 75

.babelrc:

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    [
      "polyfill-corejs3",
      {
        "method": "usage-pure"
      }
    ]
  ]
}

input.js:

Promise.all();
Promise.allSettled();
Promise.any();
Promise.race();

I'm expecting both Promise.allSettled and Promise.any to be polyfilled. However, this is not the case here.

expected output:

"use strict";

var _allSettled = _interopRequireDefault(require("core-js-pure/features/promise/all-settled.js"));

var _any = _interopRequireDefault(require("core-js-pure/features/promise/any.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

Promise.all();
(0, _allSettled.default)();
(0, _any.default)();
Promise.race();

actual output:

"use strict";

Promise.all();
Promise.allSettled();
Promise.any();
Promise.race();

usage-global correctly provides the two missing polyfills.

I've noticed that these methods are defined without a pure reference: https://github.com/babel/babel-polyfills/blob/fa4f9f3c42d50702b69f2aa8943840a5ba42c3fe/packages/babel-plugin-polyfill-corejs3/src/built-in-definitions.js#L338-L350

I've tried passing "promise/all-settled" and "promise/any" there and I started seeing correct output.

giniyat202 avatar Oct 08 '21 14:10 giniyat202

...or Promise should be polyfilled completely. With the current core-js architecture, promise/index that's loaded on Promise contains es.promise.{ all-settled, any }, so it should be polyfilled in chrome >= 75 and promise/{ all-settled, any } contains main es.promise module. (core-js@4 should optimize it, however the issue not about it.)

zloirock avatar Oct 08 '21 15:10 zloirock

Does this issue mean that usage-pure is still buggy and it should be avoided in production?

nyngwang avatar Jan 25 '24 11:01 nyngwang

That means that usage-pure is still buggy with targets. https://github.com/babel/babel-polyfills/pull/198 adds a kind of workaround for that.

zloirock avatar Jan 25 '24 11:01 zloirock

@zloirock Oh, thank you so much :) Then I think I will pick usage-global for my project since this one is stable, right? Just a little question: if I want to contribute to this project as he/she did in the PR, what background knowledge should I have? Is there a guide or something?

nyngwang avatar Jan 25 '24 13:01 nyngwang

@nyngwang look at https://github.com/babel/babel-polyfills/blob/main/CONTRIBUTING.md and https://github.com/babel/babel/blob/main/CONTRIBUTING.md.

zloirock avatar Jan 25 '24 13:01 zloirock