full.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. var _deepArray = require("./helpers/deep-array");
  20. function _traverse() {
  21. const data = require("@babel/traverse");
  22. _traverse = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. var _caching = require("./caching");
  28. var _options = require("./validation/options");
  29. var _plugins = require("./validation/plugins");
  30. var _configApi = require("./helpers/config-api");
  31. var _partial = require("./partial");
  32. var _configError = require("../errors/config-error");
  33. var _default = _gensync()(function* loadFullConfig(inputOpts) {
  34. var _opts$assumptions;
  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 presetContext = Object.assign({}, context, {
  56. targets: options.targets
  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 externalDependencies = [];
  70. const ignored = yield* enhanceError(context, function* recursePresetDescriptors(rawPresets, pluginDescriptorsPass) {
  71. const presets = [];
  72. for (let i = 0; i < rawPresets.length; i++) {
  73. const descriptor = rawPresets[i];
  74. if (descriptor.options !== false) {
  75. try {
  76. var preset = yield* loadPresetDescriptor(descriptor, presetContext);
  77. } catch (e) {
  78. if (e.code === "BABEL_UNKNOWN_OPTION") {
  79. (0, _options.checkNoUnwrappedItemOptionPairs)(rawPresets, i, "preset", e);
  80. }
  81. throw e;
  82. }
  83. externalDependencies.push(preset.externalDependencies);
  84. if (descriptor.ownPass) {
  85. presets.push({
  86. preset: preset.chain,
  87. pass: []
  88. });
  89. } else {
  90. presets.unshift({
  91. preset: preset.chain,
  92. pass: pluginDescriptorsPass
  93. });
  94. }
  95. }
  96. }
  97. if (presets.length > 0) {
  98. pluginDescriptorsByPass.splice(1, 0, ...presets.map(o => o.pass).filter(p => p !== pluginDescriptorsPass));
  99. for (const {
  100. preset,
  101. pass
  102. } of presets) {
  103. if (!preset) return true;
  104. pass.push(...preset.plugins);
  105. const ignored = yield* recursePresetDescriptors(preset.presets, pass);
  106. if (ignored) return true;
  107. preset.options.forEach(opts => {
  108. (0, _util.mergeOptions)(optionDefaults, opts);
  109. });
  110. }
  111. }
  112. })(presetsDescriptors, pluginDescriptorsByPass[0]);
  113. if (ignored) return null;
  114. const opts = optionDefaults;
  115. (0, _util.mergeOptions)(opts, options);
  116. const pluginContext = Object.assign({}, presetContext, {
  117. assumptions: (_opts$assumptions = opts.assumptions) != null ? _opts$assumptions : {}
  118. });
  119. yield* enhanceError(context, function* loadPluginDescriptors() {
  120. pluginDescriptorsByPass[0].unshift(...initialPluginsDescriptors);
  121. for (const descs of pluginDescriptorsByPass) {
  122. const pass = [];
  123. passes.push(pass);
  124. for (let i = 0; i < descs.length; i++) {
  125. const descriptor = descs[i];
  126. if (descriptor.options !== false) {
  127. try {
  128. var plugin = yield* loadPluginDescriptor(descriptor, pluginContext);
  129. } catch (e) {
  130. if (e.code === "BABEL_UNKNOWN_PLUGIN_PROPERTY") {
  131. (0, _options.checkNoUnwrappedItemOptionPairs)(descs, i, "plugin", e);
  132. }
  133. throw e;
  134. }
  135. pass.push(plugin);
  136. externalDependencies.push(plugin.externalDependencies);
  137. }
  138. }
  139. }
  140. })();
  141. opts.plugins = passes[0];
  142. opts.presets = passes.slice(1).filter(plugins => plugins.length > 0).map(plugins => ({
  143. plugins
  144. }));
  145. opts.passPerPreset = opts.presets.length > 0;
  146. return {
  147. options: opts,
  148. passes: passes,
  149. externalDependencies: (0, _deepArray.finalize)(externalDependencies)
  150. };
  151. });
  152. exports.default = _default;
  153. function enhanceError(context, fn) {
  154. return function* (arg1, arg2) {
  155. try {
  156. return yield* fn(arg1, arg2);
  157. } catch (e) {
  158. if (!/^\[BABEL\]/.test(e.message)) {
  159. var _context$filename;
  160. e.message = `[BABEL] ${(_context$filename = context.filename) != null ? _context$filename : "unknown file"}: ${e.message}`;
  161. }
  162. throw e;
  163. }
  164. };
  165. }
  166. const makeDescriptorLoader = apiFactory => (0, _caching.makeWeakCache)(function* ({
  167. value,
  168. options,
  169. dirname,
  170. alias
  171. }, cache) {
  172. if (options === false) throw new Error("Assertion failure");
  173. options = options || {};
  174. const externalDependencies = [];
  175. let item = value;
  176. if (typeof value === "function") {
  177. const factory = (0, _async.maybeAsync)(value, `You appear to be using an async plugin/preset, but Babel has been called synchronously`);
  178. const api = Object.assign({}, context, apiFactory(cache, externalDependencies));
  179. try {
  180. item = yield* factory(api, options, dirname);
  181. } catch (e) {
  182. if (alias) {
  183. e.message += ` (While processing: ${JSON.stringify(alias)})`;
  184. }
  185. throw e;
  186. }
  187. }
  188. if (!item || typeof item !== "object") {
  189. throw new Error("Plugin/Preset did not return an object.");
  190. }
  191. if ((0, _async.isThenable)(item)) {
  192. yield* [];
  193. 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)})`);
  194. }
  195. if (externalDependencies.length > 0 && (!cache.configured() || cache.mode() === "forever")) {
  196. let error = `A plugin/preset has external untracked dependencies ` + `(${externalDependencies[0]}), but the cache `;
  197. if (!cache.configured()) {
  198. error += `has not been configured to be invalidated when the external dependencies change. `;
  199. } else {
  200. error += ` has been configured to never be invalidated. `;
  201. }
  202. error += `Plugins/presets should configure their cache to be invalidated when the external ` + `dependencies change, for example using \`api.cache.invalidate(() => ` + `statSync(filepath).mtimeMs)\` or \`api.cache.never()\`\n` + `(While processing: ${JSON.stringify(alias)})`;
  203. throw new Error(error);
  204. }
  205. return {
  206. value: item,
  207. options,
  208. dirname,
  209. alias,
  210. externalDependencies: (0, _deepArray.finalize)(externalDependencies)
  211. };
  212. });
  213. const pluginDescriptorLoader = makeDescriptorLoader(_configApi.makePluginAPI);
  214. const presetDescriptorLoader = makeDescriptorLoader(_configApi.makePresetAPI);
  215. const instantiatePlugin = (0, _caching.makeWeakCache)(function* ({
  216. value,
  217. options,
  218. dirname,
  219. alias,
  220. externalDependencies
  221. }, cache) {
  222. const pluginObj = (0, _plugins.validatePluginObject)(value);
  223. const plugin = Object.assign({}, pluginObj);
  224. if (plugin.visitor) {
  225. plugin.visitor = _traverse().default.explode(Object.assign({}, plugin.visitor));
  226. }
  227. if (plugin.inherits) {
  228. const inheritsDescriptor = {
  229. name: undefined,
  230. alias: `${alias}$inherits`,
  231. value: plugin.inherits,
  232. options,
  233. dirname
  234. };
  235. const inherits = yield* (0, _async.forwardAsync)(loadPluginDescriptor, run => {
  236. return cache.invalidate(data => run(inheritsDescriptor, data));
  237. });
  238. plugin.pre = chain(inherits.pre, plugin.pre);
  239. plugin.post = chain(inherits.post, plugin.post);
  240. plugin.manipulateOptions = chain(inherits.manipulateOptions, plugin.manipulateOptions);
  241. plugin.visitor = _traverse().default.visitors.merge([inherits.visitor || {}, plugin.visitor || {}]);
  242. if (inherits.externalDependencies.length > 0) {
  243. if (externalDependencies.length === 0) {
  244. externalDependencies = inherits.externalDependencies;
  245. } else {
  246. externalDependencies = (0, _deepArray.finalize)([externalDependencies, inherits.externalDependencies]);
  247. }
  248. }
  249. }
  250. return new _plugin.default(plugin, options, alias, externalDependencies);
  251. });
  252. function* loadPluginDescriptor(descriptor, context) {
  253. if (descriptor.value instanceof _plugin.default) {
  254. if (descriptor.options) {
  255. throw new Error("Passed options to an existing Plugin instance will not work.");
  256. }
  257. return descriptor.value;
  258. }
  259. return yield* instantiatePlugin(yield* pluginDescriptorLoader(descriptor, context), context);
  260. }
  261. const needsFilename = val => val && typeof val !== "function";
  262. const validateIfOptionNeedsFilename = (options, descriptor) => {
  263. if (needsFilename(options.test) || needsFilename(options.include) || needsFilename(options.exclude)) {
  264. const formattedPresetName = descriptor.name ? `"${descriptor.name}"` : "/* your preset */";
  265. throw new _configError.default([`Preset ${formattedPresetName} requires a filename to be set when babel is called directly,`, `\`\`\``, `babel.transformSync(code, { filename: 'file.ts', presets: [${formattedPresetName}] });`, `\`\`\``, `See https://babeljs.io/docs/en/options#filename for more information.`].join("\n"));
  266. }
  267. };
  268. const validatePreset = (preset, context, descriptor) => {
  269. if (!context.filename) {
  270. var _options$overrides;
  271. const {
  272. options
  273. } = preset;
  274. validateIfOptionNeedsFilename(options, descriptor);
  275. (_options$overrides = options.overrides) == null ? void 0 : _options$overrides.forEach(overrideOptions => validateIfOptionNeedsFilename(overrideOptions, descriptor));
  276. }
  277. };
  278. const instantiatePreset = (0, _caching.makeWeakCacheSync)(({
  279. value,
  280. dirname,
  281. alias,
  282. externalDependencies
  283. }) => {
  284. return {
  285. options: (0, _options.validate)("preset", value),
  286. alias,
  287. dirname,
  288. externalDependencies
  289. };
  290. });
  291. function* loadPresetDescriptor(descriptor, context) {
  292. const preset = instantiatePreset(yield* presetDescriptorLoader(descriptor, context));
  293. validatePreset(preset, context, descriptor);
  294. return {
  295. chain: yield* (0, _configChain.buildPresetChain)(preset, context),
  296. externalDependencies: preset.externalDependencies
  297. };
  298. }
  299. function chain(a, b) {
  300. const fns = [a, b].filter(Boolean);
  301. if (fns.length <= 1) return fns[0];
  302. return function (...args) {
  303. for (const fn of fns) {
  304. fn.apply(this, args);
  305. }
  306. };
  307. }
  308. 0 && 0;
  309. //# sourceMappingURL=full.js.map