|
1 | 1 | export function declare(builder) { |
2 | 2 | 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); |
11 | 11 | } |
12 | 12 |
|
13 | | - return builder(api, options || {}, dirname); |
| 13 | + return builder(clonedApi ?? api, options || {}, dirname); |
14 | 14 | }; |
15 | 15 | } |
16 | 16 |
|
| 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 | + |
17 | 31 | function copyApiObject(api) { |
18 | 32 | // Babel >= 7 <= beta.41 passed the API as a new object that had |
19 | 33 | // babel/core as the prototype. While slightly faster, it also |
|
0 commit comments