Skip to content

Commit b024ab6

Browse files
Polyfill api.targets in helper-plugin-utils
1 parent 88ddab5 commit b024ab6

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/babel-helper-plugin-utils/src/index.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
export function declare(builder) {
22
return (api, options, dirname) => {
3-
if (!api.assertVersion) {
4-
// Inject a custom version of 'assertVersion' for Babel 6 and early
5-
// versions of Babel 7's beta that didn't have it.
6-
api = Object.assign(copyApiObject(api), {
7-
assertVersion(range) {
8-
throwVersionError(range, api.version);
9-
},
10-
});
3+
let clonedApi;
4+
5+
for (const name of Object.keys(apiPolyfills)) {
6+
if (api[name]) continue;
7+
8+
// TODO: Use ??= when flow lets us to do so
9+
clonedApi = clonedApi ?? copyApiObject(api);
10+
clonedApi[name] = apiPolyfills[name](clonedApi);
1111
}
1212

13-
return builder(api, options || {}, dirname);
13+
return builder(clonedApi ?? api, options || {}, dirname);
1414
};
1515
}
1616

17+
const apiPolyfills = {
18+
// Not supported by Babel 7 and early versions of Babel 7 beta.
19+
// It's important that this is polyfilled for older Babel versions
20+
// since it's needed to report the version mismatch.
21+
assertVersion: api => range => {
22+
throwVersionError(range, api.version);
23+
},
24+
// This is supported starting from Babel 7.13
25+
// TODO(Babel 8): Remove this polyfill
26+
targets: () => () => {
27+
return {};
28+
},
29+
};
30+
1731
function copyApiObject(api) {
1832
// Babel >= 7 <= beta.41 passed the API as a new object that had
1933
// babel/core as the prototype. While slightly faster, it also

packages/babel-preset-env/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ function supportsTopLevelAwait(caller) {
264264
export default declare((api, opts) => {
265265
api.assertVersion(7);
266266

267-
// TODO(Babel 8): api.targets() is always defined, no need to fallback
268-
const babelTargets = api.targets?.() ?? {};
267+
const babelTargets = api.targets();
269268

270269
const {
271270
bugfixes,

0 commit comments

Comments
 (0)