46a8c70e66080b9bd143138958e5c1bbf0d25d81661263849a897900b2512077fbbea4165a8dd6050eb0507200bf26feca1fef3e6dd57a75343be2f9f4da99 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.loadPlugin = loadPlugin;
  6. exports.loadPreset = loadPreset;
  7. exports.resolvePreset = exports.resolvePlugin = void 0;
  8. function _debug() {
  9. const data = require("debug");
  10. _debug = function () {
  11. return data;
  12. };
  13. return data;
  14. }
  15. function _path() {
  16. const data = require("path");
  17. _path = function () {
  18. return data;
  19. };
  20. return data;
  21. }
  22. var _async = require("../../gensync-utils/async.js");
  23. var _moduleTypes = require("./module-types.js");
  24. function _url() {
  25. const data = require("url");
  26. _url = function () {
  27. return data;
  28. };
  29. return data;
  30. }
  31. var _importMetaResolve = require("../../vendor/import-meta-resolve.js");
  32. require("module");
  33. function _fs() {
  34. const data = require("fs");
  35. _fs = function () {
  36. return data;
  37. };
  38. return data;
  39. }
  40. const debug = _debug()("babel:config:loading:files:plugins");
  41. const EXACT_RE = /^module:/;
  42. const BABEL_PLUGIN_PREFIX_RE = /^(?!@|module:|[^/]+\/|babel-plugin-)/;
  43. const BABEL_PRESET_PREFIX_RE = /^(?!@|module:|[^/]+\/|babel-preset-)/;
  44. const BABEL_PLUGIN_ORG_RE = /^(@babel\/)(?!plugin-|[^/]+\/)/;
  45. const BABEL_PRESET_ORG_RE = /^(@babel\/)(?!preset-|[^/]+\/)/;
  46. const OTHER_PLUGIN_ORG_RE = /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-plugin(?:-|\/|$)|[^/]+\/)/;
  47. const OTHER_PRESET_ORG_RE = /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-preset(?:-|\/|$)|[^/]+\/)/;
  48. const OTHER_ORG_DEFAULT_RE = /^(@(?!babel$)[^/]+)$/;
  49. const resolvePlugin = exports.resolvePlugin = resolveStandardizedName.bind(null, "plugin");
  50. const resolvePreset = exports.resolvePreset = resolveStandardizedName.bind(null, "preset");
  51. function* loadPlugin(name, dirname) {
  52. const {
  53. filepath,
  54. loader
  55. } = resolvePlugin(name, dirname, yield* (0, _async.isAsync)());
  56. const value = yield* requireModule("plugin", loader, filepath);
  57. debug("Loaded plugin %o from %o.", name, dirname);
  58. return {
  59. filepath,
  60. value
  61. };
  62. }
  63. function* loadPreset(name, dirname) {
  64. const {
  65. filepath,
  66. loader
  67. } = resolvePreset(name, dirname, yield* (0, _async.isAsync)());
  68. const value = yield* requireModule("preset", loader, filepath);
  69. debug("Loaded preset %o from %o.", name, dirname);
  70. return {
  71. filepath,
  72. value
  73. };
  74. }
  75. function standardizeName(type, name) {
  76. if (_path().isAbsolute(name)) return name;
  77. const isPreset = type === "preset";
  78. return name.replace(isPreset ? BABEL_PRESET_PREFIX_RE : BABEL_PLUGIN_PREFIX_RE, `babel-${type}-`).replace(isPreset ? BABEL_PRESET_ORG_RE : BABEL_PLUGIN_ORG_RE, `$1${type}-`).replace(isPreset ? OTHER_PRESET_ORG_RE : OTHER_PLUGIN_ORG_RE, `$1babel-${type}-`).replace(OTHER_ORG_DEFAULT_RE, `$1/babel-${type}`).replace(EXACT_RE, "");
  79. }
  80. function* resolveAlternativesHelper(type, name) {
  81. const standardizedName = standardizeName(type, name);
  82. const {
  83. error,
  84. value
  85. } = yield standardizedName;
  86. if (!error) return value;
  87. if (error.code !== "MODULE_NOT_FOUND") throw error;
  88. if (standardizedName !== name && !(yield name).error) {
  89. error.message += `\n- If you want to resolve "${name}", use "module:${name}"`;
  90. }
  91. if (!(yield standardizeName(type, "@babel/" + name)).error) {
  92. error.message += `\n- Did you mean "@babel/${name}"?`;
  93. }
  94. const oppositeType = type === "preset" ? "plugin" : "preset";
  95. if (!(yield standardizeName(oppositeType, name)).error) {
  96. error.message += `\n- Did you accidentally pass a ${oppositeType} as a ${type}?`;
  97. }
  98. if (type === "plugin") {
  99. const transformName = standardizedName.replace("-proposal-", "-transform-");
  100. if (transformName !== standardizedName && !(yield transformName).error) {
  101. error.message += `\n- Did you mean "${transformName}"?`;
  102. }
  103. }
  104. error.message += `\n
  105. Make sure that all the Babel plugins and presets you are using
  106. are defined as dependencies or devDependencies in your package.json
  107. file. It's possible that the missing plugin is loaded by a preset
  108. you are using that forgot to add the plugin to its dependencies: you
  109. can workaround this problem by explicitly adding the missing package
  110. to your top-level package.json.
  111. `;
  112. throw error;
  113. }
  114. function tryRequireResolve(id, dirname) {
  115. try {
  116. if (dirname) {
  117. return {
  118. error: null,
  119. value: (((v, w) => (v = v.split("."), w = w.split("."), +v[0] > +w[0] || v[0] == w[0] && +v[1] >= +w[1]))(process.versions.node, "8.9") ? require.resolve : (r, {
  120. paths: [b]
  121. }, M = require("module")) => {
  122. let f = M._findPath(r, M._nodeModulePaths(b).concat(b));
  123. if (f) return f;
  124. f = new Error(`Cannot resolve module '${r}'`);
  125. f.code = "MODULE_NOT_FOUND";
  126. throw f;
  127. })(id, {
  128. paths: [dirname]
  129. })
  130. };
  131. } else {
  132. return {
  133. error: null,
  134. value: require.resolve(id)
  135. };
  136. }
  137. } catch (error) {
  138. return {
  139. error,
  140. value: null
  141. };
  142. }
  143. }
  144. function tryImportMetaResolve(id, options) {
  145. try {
  146. return {
  147. error: null,
  148. value: (0, _importMetaResolve.resolve)(id, options)
  149. };
  150. } catch (error) {
  151. return {
  152. error,
  153. value: null
  154. };
  155. }
  156. }
  157. function resolveStandardizedNameForRequire(type, name, dirname) {
  158. const it = resolveAlternativesHelper(type, name);
  159. let res = it.next();
  160. while (!res.done) {
  161. res = it.next(tryRequireResolve(res.value, dirname));
  162. }
  163. return {
  164. loader: "require",
  165. filepath: res.value
  166. };
  167. }
  168. function resolveStandardizedNameForImport(type, name, dirname) {
  169. const parentUrl = (0, _url().pathToFileURL)(_path().join(dirname, "./babel-virtual-resolve-base.js")).href;
  170. const it = resolveAlternativesHelper(type, name);
  171. let res = it.next();
  172. while (!res.done) {
  173. res = it.next(tryImportMetaResolve(res.value, parentUrl));
  174. }
  175. return {
  176. loader: "auto",
  177. filepath: (0, _url().fileURLToPath)(res.value)
  178. };
  179. }
  180. function resolveStandardizedName(type, name, dirname, allowAsync) {
  181. if (!_moduleTypes.supportsESM || !allowAsync) {
  182. return resolveStandardizedNameForRequire(type, name, dirname);
  183. }
  184. try {
  185. const resolved = resolveStandardizedNameForImport(type, name, dirname);
  186. if (!(0, _fs().existsSync)(resolved.filepath)) {
  187. throw Object.assign(new Error(`Could not resolve "${name}" in file ${dirname}.`), {
  188. type: "MODULE_NOT_FOUND"
  189. });
  190. }
  191. return resolved;
  192. } catch (e) {
  193. try {
  194. return resolveStandardizedNameForRequire(type, name, dirname);
  195. } catch (e2) {
  196. if (e.type === "MODULE_NOT_FOUND") throw e;
  197. if (e2.type === "MODULE_NOT_FOUND") throw e2;
  198. throw e;
  199. }
  200. }
  201. }
  202. {
  203. var LOADING_MODULES = new Set();
  204. }
  205. function* requireModule(type, loader, name) {
  206. {
  207. if (!(yield* (0, _async.isAsync)()) && LOADING_MODULES.has(name)) {
  208. throw new Error(`Reentrant ${type} detected trying to load "${name}". This module is not ignored ` + "and is trying to load itself while compiling itself, leading to a dependency cycle. " + 'We recommend adding it to your "ignore" list in your babelrc, or to a .babelignore.');
  209. }
  210. }
  211. try {
  212. {
  213. LOADING_MODULES.add(name);
  214. }
  215. {
  216. return yield* (0, _moduleTypes.default)(name, loader, `You appear to be using a native ECMAScript module ${type}, ` + "which is only supported when running Babel asynchronously " + "or when using the Node.js `--experimental-require-module` flag.", `You appear to be using a ${type} that contains top-level await, ` + "which is only supported when running Babel asynchronously.", true);
  217. }
  218. } catch (err) {
  219. err.message = `[BABEL]: ${err.message} (While processing: ${name})`;
  220. throw err;
  221. } finally {
  222. {
  223. LOADING_MODULES.delete(name);
  224. }
  225. }
  226. }
  227. 0 && 0;
  228. //# sourceMappingURL=plugins.js.map