full.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _gensync() {
  7. const data = _interopRequireDefault(require("gensync"));
  8. _gensync = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _async = require("../gensync-utils/async");
  14. var _util = require("./util");
  15. var context = _interopRequireWildcard(require("../index"));
  16. var _plugin = _interopRequireDefault(require("./plugin"));
  17. var _item = require("./item");
  18. var _configChain = require("./config-chain");
  19. function _traverse() {
  20. const data = _interopRequireDefault(require("@babel/traverse"));
  21. _traverse = function () {
  22. return data;
  23. };
  24. return data;
  25. }
  26. var _caching = require("./caching");
  27. var _options = require("./validation/options");
  28. var _plugins = require("./validation/plugins");
  29. var _configApi = _interopRequireDefault(require("./helpers/config-api"));
  30. var _partial = _interopRequireDefault(require("./partial"));
  31. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  32. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  33. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  34. var _default = (0, _gensync().default)(function* loadFullConfig(inputOpts) {
  35. const result = yield* (0, _partial.default)(inputOpts);
  36. if (!result) {
  37. return null;
  38. }
  39. const {
  40. options,
  41. context,
  42. fileHandling
  43. } = result;
  44. if (fileHandling === "ignored") {
  45. return null;
  46. }
  47. const optionDefaults = {};
  48. const {
  49. plugins,
  50. presets
  51. } = options;
  52. if (!plugins || !presets) {
  53. throw new Error("Assertion failure - plugins and presets exist");
  54. }
  55. const toDescriptor = item => {
  56. const desc = (0, _item.getItemDescriptor)(item);
  57. if (!desc) {
  58. throw new Error("Assertion failure - must be config item");
  59. }
  60. return desc;
  61. };
  62. const presetsDescriptors = presets.map(toDescriptor);
  63. const initialPluginsDescriptors = plugins.map(toDescriptor);
  64. const pluginDescriptorsByPass = [[]];
  65. const passes = [];
  66. const ignored = yield* enhanceError(context, function* recursePresetDescriptors(rawPresets, pluginDescriptorsPass) {
  67. const presets = [];
  68. for (let i = 0; i < rawPresets.length; i++) {
  69. const descriptor = rawPresets[i];
  70. if (descriptor.options !== false) {
  71. try {
  72. if (descriptor.ownPass) {
  73. presets.push({
  74. preset: yield* loadPresetDescriptor(descriptor, context),
  75. pass: []
  76. });
  77. } else {
  78. presets.unshift({
  79. preset: yield* loadPresetDescriptor(descriptor, context),
  80. pass: pluginDescriptorsPass
  81. });
  82. }
  83. } catch (e) {
  84. if (e.code === "BABEL_UNKNOWN_OPTION") {
  85. (0, _options.checkNoUnwrappedItemOptionPairs)(rawPresets, i, "preset", e);
  86. }
  87. throw e;
  88. }
  89. }
  90. }
  91. if (presets.length > 0) {
  92. pluginDescriptorsByPass.splice(1, 0, ...presets.map(o => o.pass).filter(p => p !== pluginDescriptorsPass));
  93. for (const {
  94. preset,
  95. pass
  96. } of presets) {
  97. if (!preset) return true;
  98. pass.push(...preset.plugins);
  99. const ignored = yield* recursePresetDescriptors(preset.presets, pass);
  100. if (ignored) return true;
  101. preset.options.forEach(opts => {
  102. (0, _util.mergeOptions)(optionDefaults, opts);
  103. });
  104. }
  105. }
  106. })(presetsDescriptors, pluginDescriptorsByPass[0]);
  107. if (ignored) return null;
  108. const opts = optionDefaults;
  109. (0, _util.mergeOptions)(opts, options);
  110. yield* enhanceError(context, function* loadPluginDescriptors() {
  111. pluginDescriptorsByPass[0].unshift(...initialPluginsDescriptors);
  112. for (const descs of pluginDescriptorsByPass) {
  113. const pass = [];
  114. passes.push(pass);
  115. for (let i = 0; i < descs.length; i++) {
  116. const descriptor = descs[i];
  117. if (descriptor.options !== false) {
  118. try {
  119. pass.push(yield* loadPluginDescriptor(descriptor, context));
  120. } catch (e) {
  121. if (e.code === "BABEL_UNKNOWN_PLUGIN_PROPERTY") {
  122. (0, _options.checkNoUnwrappedItemOptionPairs)(descs, i, "plugin", e);
  123. }
  124. throw e;
  125. }
  126. }
  127. }
  128. }
  129. })();
  130. opts.plugins = passes[0];
  131. opts.presets = passes.slice(1).filter(plugins => plugins.length > 0).map(plugins => ({
  132. plugins
  133. }));
  134. opts.passPerPreset = opts.presets.length > 0;
  135. return {
  136. options: opts,
  137. passes: passes
  138. };
  139. });
  140. exports.default = _default;
  141. function enhanceError(context, fn) {
  142. return function* (arg1, arg2) {
  143. try {
  144. return yield* fn(arg1, arg2);
  145. } catch (e) {
  146. if (!/^\[BABEL\]/.test(e.message)) {
  147. e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`;
  148. }
  149. throw e;
  150. }
  151. };
  152. }
  153. const loadDescriptor = (0, _caching.makeWeakCache)(function* ({
  154. value,
  155. options,
  156. dirname,
  157. alias
  158. }, cache) {
  159. if (options === false) throw new Error("Assertion failure");
  160. options = options || {};
  161. let item = value;
  162. if (typeof value === "function") {
  163. const api = Object.assign({}, context, (0, _configApi.default)(cache));
  164. try {
  165. item = value(api, options, dirname);
  166. } catch (e) {
  167. if (alias) {
  168. e.message += ` (While processing: ${JSON.stringify(alias)})`;
  169. }
  170. throw e;
  171. }
  172. }
  173. if (!item || typeof item !== "object") {
  174. throw new Error("Plugin/Preset did not return an object.");
  175. }
  176. if (typeof item.then === "function") {
  177. yield* [];
  178. throw new Error(`You appear to be using an async plugin, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, ` + `you may need to upgrade your @babel/core version.`);
  179. }
  180. return {
  181. value: item,
  182. options,
  183. dirname,
  184. alias
  185. };
  186. });
  187. function* loadPluginDescriptor(descriptor, context) {
  188. if (descriptor.value instanceof _plugin.default) {
  189. if (descriptor.options) {
  190. throw new Error("Passed options to an existing Plugin instance will not work.");
  191. }
  192. return descriptor.value;
  193. }
  194. return yield* instantiatePlugin(yield* loadDescriptor(descriptor, context), context);
  195. }
  196. const instantiatePlugin = (0, _caching.makeWeakCache)(function* ({
  197. value,
  198. options,
  199. dirname,
  200. alias
  201. }, cache) {
  202. const pluginObj = (0, _plugins.validatePluginObject)(value);
  203. const plugin = Object.assign({}, pluginObj);
  204. if (plugin.visitor) {
  205. plugin.visitor = _traverse().default.explode(Object.assign({}, plugin.visitor));
  206. }
  207. if (plugin.inherits) {
  208. const inheritsDescriptor = {
  209. name: undefined,
  210. alias: `${alias}$inherits`,
  211. value: plugin.inherits,
  212. options,
  213. dirname
  214. };
  215. const inherits = yield* (0, _async.forwardAsync)(loadPluginDescriptor, run => {
  216. return cache.invalidate(data => run(inheritsDescriptor, data));
  217. });
  218. plugin.pre = chain(inherits.pre, plugin.pre);
  219. plugin.post = chain(inherits.post, plugin.post);
  220. plugin.manipulateOptions = chain(inherits.manipulateOptions, plugin.manipulateOptions);
  221. plugin.visitor = _traverse().default.visitors.merge([inherits.visitor || {}, plugin.visitor || {}]);
  222. }
  223. return new _plugin.default(plugin, options, alias);
  224. });
  225. const validateIfOptionNeedsFilename = (options, descriptor) => {
  226. if (options.test || options.include || options.exclude) {
  227. const formattedPresetName = descriptor.name ? `"${descriptor.name}"` : "/* your preset */";
  228. throw new Error([`Preset ${formattedPresetName} requires a filename to be set when babel is called directly,`, `\`\`\``, `babel.transform(code, { filename: 'file.ts', presets: [${formattedPresetName}] });`, `\`\`\``, `See https://babeljs.io/docs/en/options#filename for more information.`].join("\n"));
  229. }
  230. };
  231. const validatePreset = (preset, context, descriptor) => {
  232. if (!context.filename) {
  233. const {
  234. options
  235. } = preset;
  236. validateIfOptionNeedsFilename(options, descriptor);
  237. if (options.overrides) {
  238. options.overrides.forEach(overrideOptions => validateIfOptionNeedsFilename(overrideOptions, descriptor));
  239. }
  240. }
  241. };
  242. function* loadPresetDescriptor(descriptor, context) {
  243. const preset = instantiatePreset(yield* loadDescriptor(descriptor, context));
  244. validatePreset(preset, context, descriptor);
  245. return yield* (0, _configChain.buildPresetChain)(preset, context);
  246. }
  247. const instantiatePreset = (0, _caching.makeWeakCacheSync)(({
  248. value,
  249. dirname,
  250. alias
  251. }) => {
  252. return {
  253. options: (0, _options.validate)("preset", value),
  254. alias,
  255. dirname
  256. };
  257. });
  258. function chain(a, b) {
  259. const fns = [a, b].filter(Boolean);
  260. if (fns.length <= 1) return fns[0];
  261. return function (...args) {
  262. for (const fn of fns) {
  263. fn.apply(this, args);
  264. }
  265. };
  266. }