File size: 7,285 Bytes
1df763a
1
{"version":3,"names":["apiPolyfills","assertVersion","api","range","throwVersionError","version","Object","assign","targets","assumption","undefined","declare","builder","options","dirname","_clonedApi2","clonedApi","name","keys","_clonedApi","copyApiObject","declarePreset","exports","proto","test","getPrototypeOf","has","obj","key","prototype","hasOwnProperty","call","Number","isInteger","Error","limit","stackTraceLimit","err","slice","code"],"sources":["../src/index.ts"],"sourcesContent":["import type {\n  PluginAPI,\n  PluginObject,\n  PluginPass,\n  PresetAPI,\n  PresetObject,\n} from \"@babel/core\";\n\ntype APIPolyfillFactory<T extends keyof PluginAPI> = (\n  api: PluginAPI,\n) => PluginAPI[T];\n\ntype APIPolyfills = {\n  assertVersion: APIPolyfillFactory<\"assertVersion\">;\n};\n\nconst apiPolyfills: APIPolyfills = {\n  // Not supported by Babel 7 and early versions of Babel 7 beta.\n  // It's important that this is polyfilled for older Babel versions\n  // since it's needed to report the version mismatch.\n  assertVersion: (api: PluginAPI) => (range: number | string) => {\n    throwVersionError(range, api.version);\n  },\n};\nif (!process.env.BABEL_8_BREAKING) {\n  Object.assign(apiPolyfills, {\n    // This is supported starting from Babel 7.13\n    targets: () => () => {\n      return {};\n    },\n    // This is supported starting from Babel 7.13\n    assumption: () => () => {\n      return undefined;\n    },\n  });\n}\n\nexport function declare<State = {}, Option = {}>(\n  builder: (\n    api: PluginAPI,\n    options: Option,\n    dirname: string,\n  ) => PluginObject<State & PluginPass>,\n): (\n  api: PluginAPI,\n  options: Option,\n  dirname: string,\n) => PluginObject<State & PluginPass> {\n  return (api, options: Option, dirname: string) => {\n    let clonedApi: PluginAPI;\n\n    for (const name of Object.keys(\n      apiPolyfills,\n    ) as (keyof typeof apiPolyfills)[]) {\n      if (api[name]) continue;\n\n      clonedApi ??= copyApiObject(api);\n      clonedApi[name] = apiPolyfills[name](clonedApi);\n    }\n\n    // @ts-expect-error options || {} may not be assigned to Options\n    return builder(clonedApi ?? api, options || {}, dirname);\n  };\n}\n\nexport const declarePreset = declare as <Option = {}>(\n  builder: (api: PresetAPI, options: Option, dirname: string) => PresetObject,\n) => (api: PresetAPI, options: Option, dirname: string) => PresetObject;\n\nfunction copyApiObject(api: PluginAPI): PluginAPI {\n  // Babel >= 7 <= beta.41 passed the API as a new object that had\n  // babel/core as the prototype. While slightly faster, it also\n  // means that the Object.assign copy below fails. Rather than\n  // keep complexity, the Babel 6 behavior has been reverted and this\n  // normalizes all that for Babel 7.\n  let proto = null;\n  if (typeof api.version === \"string\" && /^7\\./.test(api.version)) {\n    proto = Object.getPrototypeOf(api);\n    if (\n      proto &&\n      (!has(proto, \"version\") ||\n        !has(proto, \"transform\") ||\n        !has(proto, \"template\") ||\n        !has(proto, \"types\"))\n    ) {\n      proto = null;\n    }\n  }\n\n  return {\n    ...proto,\n    ...api,\n  };\n}\n\nfunction has(obj: {}, key: string) {\n  return Object.prototype.hasOwnProperty.call(obj, key);\n}\n\nfunction throwVersionError(range: string | number, version: string) {\n  if (typeof range === \"number\") {\n    if (!Number.isInteger(range)) {\n      throw new Error(\"Expected string or integer value.\");\n    }\n    range = `^${range}.0.0-0`;\n  }\n  if (typeof range !== \"string\") {\n    throw new Error(\"Expected string or integer value.\");\n  }\n\n  const limit = Error.stackTraceLimit;\n\n  if (typeof limit === \"number\" && limit < 25) {\n    // Bump up the limit if needed so that users are more likely\n    // to be able to see what is calling Babel.\n    Error.stackTraceLimit = 25;\n  }\n\n  let err;\n  if (version.slice(0, 2) === \"7.\") {\n    err = new Error(\n      `Requires Babel \"^7.0.0-beta.41\", but was loaded with \"${version}\". ` +\n        `You'll need to update your @babel/core version.`,\n    );\n  } else {\n    err = new Error(\n      `Requires Babel \"${range}\", but was loaded with \"${version}\". ` +\n        `If you are sure you have a compatible version of @babel/core, ` +\n        `it is likely that something in your build process is loading the ` +\n        `wrong version. Inspect the stack trace of this error to look for ` +\n        `the first entry that doesn't mention \"@babel/core\" or \"babel-core\" ` +\n        `to see what is calling Babel.`,\n    );\n  }\n\n  if (typeof limit === \"number\") {\n    Error.stackTraceLimit = limit;\n  }\n\n  throw Object.assign(err, {\n    code: \"BABEL_VERSION_UNSUPPORTED\",\n    version,\n    range,\n  } as any);\n}\n"],"mappings":";;;;;;;AAgBA,MAAMA,YAA0B,GAAG;EAIjCC,aAAa,EAAGC,GAAc,IAAMC,KAAsB,IAAK;IAC7DC,iBAAiB,CAACD,KAAK,EAAED,GAAG,CAACG,OAAO,CAAC;EACvC;AACF,CAAC;AACkC;EACjCC,MAAM,CAACC,MAAM,CAACP,YAAY,EAAE;IAE1BQ,OAAO,EAAEA,CAAA,KAAM,MAAM;MACnB,OAAO,CAAC,CAAC;IACX,CAAC;IAEDC,UAAU,EAAEA,CAAA,KAAM,MAAM;MACtB,OAAOC,SAAS;IAClB;EACF,CAAC,CAAC;AACJ;AAEO,SAASC,OAAOA,CACrBC,OAIqC,EAKD;EACpC,OAAO,CAACV,GAAG,EAAEW,OAAe,EAAEC,OAAe,KAAK;IAAA,IAAAC,WAAA;IAChD,IAAIC,SAAoB;IAExB,KAAK,MAAMC,IAAI,IAAIX,MAAM,CAACY,IAAI,CAC5BlB,YACF,CAAC,EAAmC;MAAA,IAAAmB,UAAA;MAClC,IAAIjB,GAAG,CAACe,IAAI,CAAC,EAAE;MAEf,CAAAE,UAAA,GAAAH,SAAS,YAAAG,UAAA,GAATH,SAAS,GAAKI,aAAa,CAAClB,GAAG,CAAC;MAChCc,SAAS,CAACC,IAAI,CAAC,GAAGjB,YAAY,CAACiB,IAAI,CAAC,CAACD,SAAS,CAAC;IACjD;IAGA,OAAOJ,OAAO,EAAAG,WAAA,GAACC,SAAS,YAAAD,WAAA,GAAIb,GAAG,EAAEW,OAAO,IAAI,CAAC,CAAC,EAAEC,OAAO,CAAC;EAC1D,CAAC;AACH;AAEO,MAAMO,aAAa,GAAGV,OAE0C;AAACW,OAAA,CAAAD,aAAA,GAAAA,aAAA;AAExE,SAASD,aAAaA,CAAClB,GAAc,EAAa;EAMhD,IAAIqB,KAAK,GAAG,IAAI;EACh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