Ensure that the backward-compat logic for plugin-utils copies over the version API properly. (#7580)
This commit is contained in:
parent
b8d1d221f8
commit
017d0e7078
@ -174,8 +174,7 @@ const loadDescriptor = makeWeakCache(
|
||||
|
||||
let item = value;
|
||||
if (typeof value === "function") {
|
||||
const api = Object.assign(Object.create(context), makeAPI(cache));
|
||||
|
||||
const api = Object.assign({}, context, makeAPI(cache));
|
||||
try {
|
||||
item = value(api, options, dirname);
|
||||
} catch (e) {
|
||||
|
||||
@ -3,7 +3,7 @@ export function declare(builder) {
|
||||
if (!api.assertVersion) {
|
||||
// Inject a custom version of 'assertVersion' for Babel 6 and early
|
||||
// versions of Babel 7's beta that didn't have it.
|
||||
api = Object.assign({}, api, {
|
||||
api = Object.assign(copyApiObject(api), {
|
||||
assertVersion(range) {
|
||||
throwVersionError(range, api.version);
|
||||
},
|
||||
@ -14,6 +14,33 @@ export function declare(builder) {
|
||||
};
|
||||
}
|
||||
|
||||
function copyApiObject(api) {
|
||||
// Babel >= 7 <= beta.41 passed the API as a new object that had
|
||||
// babel/core as the prototype. While slightly faster, it also
|
||||
// means that the Object.assign copy below fails. Rather than
|
||||
// keep complexity, the Babel 6 behavior has been reverted and this
|
||||
// normalizes all that for Babel 7.
|
||||
let proto = null;
|
||||
if (typeof api.version === "string" && /^7\./.test(api.version)) {
|
||||
proto = Object.getPrototypeOf(api);
|
||||
if (
|
||||
proto &&
|
||||
(!has(proto, "version") ||
|
||||
!has(proto, "transform") ||
|
||||
!has(proto, "template") ||
|
||||
!has(proto, "types"))
|
||||
) {
|
||||
proto = null;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign({}, proto, api);
|
||||
}
|
||||
|
||||
function has(obj, key) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||
}
|
||||
|
||||
function throwVersionError(range, version) {
|
||||
if (typeof range === "number") {
|
||||
if (!Number.isInteger(range)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user