full.js 8.9 KB

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