full.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.js");
  14. var _util = require("./util.js");
  15. var context = require("../index.js");
  16. var _plugin = require("./plugin.js");
  17. var _item = require("./item.js");
  18. var _configChain = require("./config-chain.js");
  19. var _deepArray = require("./helpers/deep-array.js");
  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.js");
  28. var _options = require("./validation/options.js");
  29. var _plugins = require("./validation/plugins.js");
  30. var _configApi = require("./helpers/config-api.js");
  31. var _partial = require("./partial.js");
  32. var _configError = require("../errors/config-error.js");
  33. var _default = exports.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. function enhanceError(context, fn) {
  153. return function* (arg1, arg2) {
  154. try {
  155. return yield* fn(arg1, arg2);
  156. } catch (e) {
  157. if (!/^\[BABEL\]/.test(e.message)) {
  158. var _context$filename;
  159. e.message = `[BABEL] ${(_context$filename = context.filename) != null ? _context$filename : "unknown file"}: ${e.message}`;
  160. }
  161. throw e;
  162. }
  163. };
  164. }
  165. const makeDescriptorLoader = apiFactory => (0, _caching.makeWeakCache)(function* ({
  166. value,
  167. options,
  168. dirname,
  169. alias
  170. }, cache) {
  171. if (options === false) throw new Error("Assertion failure");
  172. options = options || {};
  173. const externalDependencies = [];
  174. let item = value;
  175. if (typeof value === "function") {
  176. const factory = (0, _async.maybeAsync)(value, `You appear to be using an async plugin/preset, but Babel has been called synchronously`);
  177. const api = Object.assign({}, context, apiFactory(cache, externalDependencies));
  178. try {
  179. item = yield* factory(api, options, dirname);
  180. } catch (e) {
  181. if (alias) {
  182. e.message += ` (While processing: ${JSON.stringify(alias)})`;
  183. }
  184. throw e;
  185. }
  186. }
  187. if (!item || typeof item !== "object") {
  188. throw new Error("Plugin/Preset did not return an object.");
  189. }
  190. if ((0, _async.isThenable)(item)) {
  191. yield* [];
  192. 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)})`);
  193. }
  194. if (externalDependencies.length > 0 && (!cache.configured() || cache.mode() === "forever")) {
  195. let error = `A plugin/preset has external untracked dependencies ` + `(${externalDependencies[0]}), but the cache `;
  196. if (!cache.configured()) {
  197. error += `has not been configured to be invalidated when the external dependencies change. `;
  198. } else {
  199. error += ` has been configured to never be invalidated. `;
  200. }
  201. 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)})`;
  202. throw new Error(error);
  203. }
  204. return {
  205. value: item,
  206. options,
  207. dirname,
  208. alias,
  209. externalDependencies: (0, _deepArray.finalize)(externalDependencies)
  210. };
  211. });
  212. const pluginDescriptorLoader = makeDescriptorLoader(_configApi.makePluginAPI);
  213. const presetDescriptorLoader = makeDescriptorLoader(_configApi.makePresetAPI);
  214. const instantiatePlugin = (0, _caching.makeWeakCache)(function* ({
  215. value,
  216. options,
  217. dirname,
  218. alias,
  219. externalDependencies
  220. }, cache) {
  221. const pluginObj = (0, _plugins.validatePluginObject)(value);
  222. const plugin = Object.assign({}, pluginObj);
  223. if (plugin.visitor) {
  224. plugin.visitor = _traverse().default.explode(Object.assign({}, plugin.visitor));
  225. }
  226. if (plugin.inherits) {
  227. const inheritsDescriptor = {
  228. name: undefined,
  229. alias: `${alias}$inherits`,
  230. value: plugin.inherits,
  231. options,
  232. dirname
  233. };
  234. const inherits = yield* (0, _async.forwardAsync)(loadPluginDescriptor, run => {
  235. return cache.invalidate(data => run(inheritsDescriptor, data));
  236. });
  237. plugin.pre = chainMaybeAsync(inherits.pre, plugin.pre);
  238. plugin.post = chainMaybeAsync(inherits.post, plugin.post);
  239. plugin.manipulateOptions = chainMaybeAsync(inherits.manipulateOptions, plugin.manipulateOptions);
  240. plugin.visitor = _traverse().default.visitors.merge([inherits.visitor || {}, plugin.visitor || {}]);
  241. if (inherits.externalDependencies.length > 0) {
  242. if (externalDependencies.length === 0) {
  243. externalDependencies = inherits.externalDependencies;
  244. } else {
  245. externalDependencies = (0, _deepArray.finalize)([externalDependencies, inherits.externalDependencies]);
  246. }
  247. }
  248. }
  249. return new _plugin.default(plugin, options, alias, externalDependencies);
  250. });
  251. function* loadPluginDescriptor(descriptor, context) {
  252. if (descriptor.value instanceof _plugin.default) {
  253. if (descriptor.options) {
  254. throw new Error("Passed options to an existing Plugin instance will not work.");
  255. }
  256. return descriptor.value;
  257. }
  258. return yield* instantiatePlugin(yield* pluginDescriptorLoader(descriptor, context), context);
  259. }
  260. const needsFilename = val => val && typeof val !== "function";
  261. const validateIfOptionNeedsFilename = (options, descriptor) => {
  262. if (needsFilename(options.test) || needsFilename(options.include) || needsFilename(options.exclude)) {
  263. const formattedPresetName = descriptor.name ? `"${descriptor.name}"` : "/* your preset */";
  264. 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"));
  265. }
  266. };
  267. const validatePreset = (preset, context, descriptor) => {
  268. if (!context.filename) {
  269. var _options$overrides;
  270. const {
  271. options
  272. } = preset;
  273. validateIfOptionNeedsFilename(options, descriptor);
  274. (_options$overrides = options.overrides) == null || _options$overrides.forEach(overrideOptions => validateIfOptionNeedsFilename(overrideOptions, descriptor));
  275. }
  276. };
  277. const instantiatePreset = (0, _caching.makeWeakCacheSync)(({
  278. value,
  279. dirname,
  280. alias,
  281. externalDependencies
  282. }) => {
  283. return {
  284. options: (0, _options.validate)("preset", value),
  285. alias,
  286. dirname,
  287. externalDependencies
  288. };
  289. });
  290. function* loadPresetDescriptor(descriptor, context) {
  291. const preset = instantiatePreset(yield* presetDescriptorLoader(descriptor, context));
  292. validatePreset(preset, context, descriptor);
  293. return {
  294. chain: yield* (0, _configChain.buildPresetChain)(preset, context),
  295. externalDependencies: preset.externalDependencies
  296. };
  297. }
  298. function chainMaybeAsync(a, b) {
  299. if (!a) return b;
  300. if (!b) return a;
  301. return function (...args) {
  302. const res = a.apply(this, args);
  303. if (res && typeof res.then === "function") {
  304. return res.then(() => b.apply(this, args));
  305. }
  306. return b.apply(this, args);
  307. };
  308. }
  309. 0 && 0;
  310. //# sourceMappingURL=full.js.map