Why can't preset-env automatically polyfill the latest features #12642
-
input: const a = 1;
const b = 2;
a ||= b;
const str = 'hello';
str.matchAll(/1/) config: {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"chrome": "50"
},
"bugfixes":true,
"useBuiltIns": "usage",
"corejs":3
}
]
]
} output: "use strict";
const a = 1;
const b = 2;
a || (a = b);
const str = 'hello';
str.matchAll(/1/); can be seen the proposal-logical-assignment syntax has been converted, but String.prototype.matchAll has not been polyfill If I add the proposals configuration of corejs {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"chrome": "50"
},
"bugfixes":true,
"useBuiltIns": "usage",
"corejs":{
"version":3,
"proposals":true
}
}
]
]
} output: "use strict";
require("core-js/modules/esnext.string.match-all.js");
const a = 1;
const b = 2;
a || (a = b);
const str = 'hello';
str.matchAll(/1/); So I am curious why the latest features cannot be automatically converted |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
What is the difference between the two configs? |
Beta Was this translation helpful? Give feedback.
-
Babel uses this list to determine which polyfills are supported by your core-js version. As you can see, the stable polyfill for Your configure specifies |
Beta Was this translation helpful? Give feedback.
Babel uses this list to determine which polyfills are supported by your core-js version.
es.*
packages are stable,esnext.*
are proposal.As you can see, the stable polyfill for
matchAll
(es.string.match-all
) has been introduced in core-js 3.1.Your configure specifies
corejs: 3
which means "Babel should generate code compatible with any core-js version": it cannot inject thees.string.match-all
polyfill because it would throw with core-js 3.0.You can specify something like
corejs: "3.8"
instead, which tells Babel "I'm using at least version 3.8": Babel can them safely inject by default the polyfills introduced after version 3.0.0 of core-js.