full.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loadFullConfig;
  6. var _util = require("./util");
  7. var context = _interopRequireWildcard(require("../index"));
  8. var _plugin = _interopRequireDefault(require("./plugin"));
  9. var _item = require("./item");
  10. var _configChain = require("./config-chain");
  11. function _traverse() {
  12. const data = _interopRequireDefault(require("@babel/traverse"));
  13. _traverse = function () {
  14. return data;
  15. };
  16. return data;
  17. }
  18. var _caching = require("./caching");
  19. var _options = require("./validation/options");
  20. var _plugins = require("./validation/plugins");
  21. var _configApi = _interopRequireDefault(require("./helpers/config-api"));
  22. var _partial = _interopRequireDefault(require("./partial"));
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  25. function loadFullConfig(inputOpts) {
  26. const result = (0, _partial.default)(inputOpts);
  27. if (!result) {
  28. return null;
  29. }
  30. const {
  31. options,
  32. context
  33. } = result;
  34. const optionDefaults = {};
  35. const passes = [[]];
  36. try {
  37. const {
  38. plugins,
  39. presets
  40. } = options;
  41. if (!plugins || !presets) {
  42. throw new Error("Assertion failure - plugins and presets exist");
  43. }
  44. const ignored = function recurseDescriptors(config, pass) {
  45. const plugins = config.plugins.reduce((acc, descriptor) => {
  46. if (descriptor.options !== false) {
  47. acc.push(loadPluginDescriptor(descriptor, context));
  48. }
  49. return acc;
  50. }, []);
  51. const presets = config.presets.reduce((acc, descriptor) => {
  52. if (descriptor.options !== false) {
  53. acc.push({
  54. preset: loadPresetDescriptor(descriptor, context),
  55. pass: descriptor.ownPass ? [] : pass
  56. });
  57. }
  58. return acc;
  59. }, []);
  60. if (presets.length > 0) {
  61. passes.splice(1, 0, ...presets.map(o => o.pass).filter(p => p !== pass));
  62. for (const _ref of presets) {
  63. const {
  64. preset,
  65. pass
  66. } = _ref;
  67. if (!preset) return true;
  68. const ignored = recurseDescriptors({
  69. plugins: preset.plugins,
  70. presets: preset.presets
  71. }, pass);
  72. if (ignored) return true;
  73. preset.options.forEach(opts => {
  74. (0, _util.mergeOptions)(optionDefaults, opts);
  75. });
  76. }
  77. }
  78. if (plugins.length > 0) {
  79. pass.unshift(...plugins);
  80. }
  81. }({
  82. plugins: plugins.map(item => {
  83. const desc = (0, _item.getItemDescriptor)(item);
  84. if (!desc) {
  85. throw new Error("Assertion failure - must be config item");
  86. }
  87. return desc;
  88. }),
  89. presets: presets.map(item => {
  90. const desc = (0, _item.getItemDescriptor)(item);
  91. if (!desc) {
  92. throw new Error("Assertion failure - must be config item");
  93. }
  94. return desc;
  95. })
  96. }, passes[0]);
  97. if (ignored) return null;
  98. } catch (e) {
  99. if (!/^\[BABEL\]/.test(e.message)) {
  100. e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`;
  101. }
  102. throw e;
  103. }
  104. const opts = optionDefaults;
  105. (0, _util.mergeOptions)(opts, options);
  106. opts.plugins = passes[0];
  107. opts.presets = passes.slice(1).filter(plugins => plugins.length > 0).map(plugins => ({
  108. plugins
  109. }));
  110. opts.passPerPreset = opts.presets.length > 0;
  111. return {
  112. options: opts,
  113. passes: passes
  114. };
  115. }
  116. const loadDescriptor = (0, _caching.makeWeakCache)(({
  117. value,
  118. options,
  119. dirname,
  120. alias
  121. }, cache) => {
  122. if (options === false) throw new Error("Assertion failure");
  123. options = options || {};
  124. let item = value;
  125. if (typeof value === "function") {
  126. const api = Object.assign({}, context, (0, _configApi.default)(cache));
  127. try {
  128. item = value(api, options, dirname);
  129. } catch (e) {
  130. if (alias) {
  131. e.message += ` (While processing: ${JSON.stringify(alias)})`;
  132. }
  133. throw e;
  134. }
  135. }
  136. if (!item || typeof item !== "object") {
  137. throw new Error("Plugin/Preset did not return an object.");
  138. }
  139. if (typeof item.then === "function") {
  140. 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.`);
  141. }
  142. return {
  143. value: item,
  144. options,
  145. dirname,
  146. alias
  147. };
  148. });
  149. function loadPluginDescriptor(descriptor, context) {
  150. if (descriptor.value instanceof _plugin.default) {
  151. if (descriptor.options) {
  152. throw new Error("Passed options to an existing Plugin instance will not work.");
  153. }
  154. return descriptor.value;
  155. }
  156. return instantiatePlugin(loadDescriptor(descriptor, context), context);
  157. }
  158. const instantiatePlugin = (0, _caching.makeWeakCache)(({
  159. value,
  160. options,
  161. dirname,
  162. alias
  163. }, cache) => {
  164. const pluginObj = (0, _plugins.validatePluginObject)(value);
  165. const plugin = Object.assign({}, pluginObj);
  166. if (plugin.visitor) {
  167. plugin.visitor = _traverse().default.explode(Object.assign({}, plugin.visitor));
  168. }
  169. if (plugin.inherits) {
  170. const inheritsDescriptor = {
  171. name: undefined,
  172. alias: `${alias}$inherits`,
  173. value: plugin.inherits,
  174. options,
  175. dirname
  176. };
  177. const inherits = cache.invalidate(data => loadPluginDescriptor(inheritsDescriptor, data));
  178. plugin.pre = chain(inherits.pre, plugin.pre);
  179. plugin.post = chain(inherits.post, plugin.post);
  180. plugin.manipulateOptions = chain(inherits.manipulateOptions, plugin.manipulateOptions);
  181. plugin.visitor = _traverse().default.visitors.merge([inherits.visitor || {}, plugin.visitor || {}]);
  182. }
  183. return new _plugin.default(plugin, options, alias);
  184. });
  185. const loadPresetDescriptor = (descriptor, context) => {
  186. return (0, _configChain.buildPresetChain)(instantiatePreset(loadDescriptor(descriptor, context)), context);
  187. };
  188. const instantiatePreset = (0, _caching.makeWeakCache)(({
  189. value,
  190. dirname,
  191. alias
  192. }) => {
  193. return {
  194. options: (0, _options.validate)("preset", value),
  195. alias,
  196. dirname
  197. };
  198. });
  199. function chain(a, b) {
  200. const fns = [a, b].filter(Boolean);
  201. if (fns.length <= 1) return fns[0];
  202. return function (...args) {
  203. for (const fn of fns) {
  204. fn.apply(this, args);
  205. }
  206. };
  207. }