partial.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loadPrivatePartialConfig;
  6. exports.loadPartialConfig = void 0;
  7. function _path() {
  8. const data = require("path");
  9. _path = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _gensync() {
  15. const data = require("gensync");
  16. _gensync = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. var _plugin = require("./plugin");
  22. var _util = require("./util");
  23. var _item = require("./item");
  24. var _configChain = require("./config-chain");
  25. var _environment = require("./helpers/environment");
  26. var _options = require("./validation/options");
  27. var _files = require("./files");
  28. var _resolveTargets = require("./resolve-targets");
  29. const _excluded = ["showIgnoredFiles"];
  30. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  31. function resolveRootMode(rootDir, rootMode) {
  32. switch (rootMode) {
  33. case "root":
  34. return rootDir;
  35. case "upward-optional":
  36. {
  37. const upwardRootDir = (0, _files.findConfigUpwards)(rootDir);
  38. return upwardRootDir === null ? rootDir : upwardRootDir;
  39. }
  40. case "upward":
  41. {
  42. const upwardRootDir = (0, _files.findConfigUpwards)(rootDir);
  43. if (upwardRootDir !== null) return upwardRootDir;
  44. throw Object.assign(new Error(`Babel was run with rootMode:"upward" but a root could not ` + `be found when searching upward from "${rootDir}".\n` + `One of the following config files must be in the directory tree: ` + `"${_files.ROOT_CONFIG_FILENAMES.join(", ")}".`), {
  45. code: "BABEL_ROOT_NOT_FOUND",
  46. dirname: rootDir
  47. });
  48. }
  49. default:
  50. throw new Error(`Assertion failure - unknown rootMode value.`);
  51. }
  52. }
  53. function* loadPrivatePartialConfig(inputOpts) {
  54. if (inputOpts != null && (typeof inputOpts !== "object" || Array.isArray(inputOpts))) {
  55. throw new Error("Babel options must be an object, null, or undefined");
  56. }
  57. const args = inputOpts ? (0, _options.validate)("arguments", inputOpts) : {};
  58. const {
  59. envName = (0, _environment.getEnv)(),
  60. cwd = ".",
  61. root: rootDir = ".",
  62. rootMode = "root",
  63. caller,
  64. cloneInputAst = true
  65. } = args;
  66. const absoluteCwd = _path().resolve(cwd);
  67. const absoluteRootDir = resolveRootMode(_path().resolve(absoluteCwd, rootDir), rootMode);
  68. const filename = typeof args.filename === "string" ? _path().resolve(cwd, args.filename) : undefined;
  69. const showConfigPath = yield* (0, _files.resolveShowConfigPath)(absoluteCwd);
  70. const context = {
  71. filename,
  72. cwd: absoluteCwd,
  73. root: absoluteRootDir,
  74. envName,
  75. caller,
  76. showConfig: showConfigPath === filename
  77. };
  78. const configChain = yield* (0, _configChain.buildRootChain)(args, context);
  79. if (!configChain) return null;
  80. const merged = {
  81. assumptions: {}
  82. };
  83. configChain.options.forEach(opts => {
  84. (0, _util.mergeOptions)(merged, opts);
  85. });
  86. const options = Object.assign({}, merged, {
  87. targets: (0, _resolveTargets.resolveTargets)(merged, absoluteRootDir),
  88. cloneInputAst,
  89. babelrc: false,
  90. configFile: false,
  91. browserslistConfigFile: false,
  92. passPerPreset: false,
  93. envName: context.envName,
  94. cwd: context.cwd,
  95. root: context.root,
  96. rootMode: "root",
  97. filename: typeof context.filename === "string" ? context.filename : undefined,
  98. plugins: configChain.plugins.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor)),
  99. presets: configChain.presets.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor))
  100. });
  101. return {
  102. options,
  103. context,
  104. fileHandling: configChain.fileHandling,
  105. ignore: configChain.ignore,
  106. babelrc: configChain.babelrc,
  107. config: configChain.config,
  108. files: configChain.files
  109. };
  110. }
  111. const loadPartialConfig = _gensync()(function* (opts) {
  112. let showIgnoredFiles = false;
  113. if (typeof opts === "object" && opts !== null && !Array.isArray(opts)) {
  114. var _opts = opts;
  115. ({
  116. showIgnoredFiles
  117. } = _opts);
  118. opts = _objectWithoutPropertiesLoose(_opts, _excluded);
  119. _opts;
  120. }
  121. const result = yield* loadPrivatePartialConfig(opts);
  122. if (!result) return null;
  123. const {
  124. options,
  125. babelrc,
  126. ignore,
  127. config,
  128. fileHandling,
  129. files
  130. } = result;
  131. if (fileHandling === "ignored" && !showIgnoredFiles) {
  132. return null;
  133. }
  134. (options.plugins || []).forEach(item => {
  135. if (item.value instanceof _plugin.default) {
  136. throw new Error("Passing cached plugin instances is not supported in " + "babel.loadPartialConfig()");
  137. }
  138. });
  139. return new PartialConfig(options, babelrc ? babelrc.filepath : undefined, ignore ? ignore.filepath : undefined, config ? config.filepath : undefined, fileHandling, files);
  140. });
  141. exports.loadPartialConfig = loadPartialConfig;
  142. class PartialConfig {
  143. constructor(options, babelrc, ignore, config, fileHandling, files) {
  144. this.options = void 0;
  145. this.babelrc = void 0;
  146. this.babelignore = void 0;
  147. this.config = void 0;
  148. this.fileHandling = void 0;
  149. this.files = void 0;
  150. this.options = options;
  151. this.babelignore = ignore;
  152. this.babelrc = babelrc;
  153. this.config = config;
  154. this.fileHandling = fileHandling;
  155. this.files = files;
  156. Object.freeze(this);
  157. }
  158. hasFilesystemConfig() {
  159. return this.babelrc !== undefined || this.config !== undefined;
  160. }
  161. }
  162. Object.freeze(PartialConfig.prototype);
  163. 0 && 0;
  164. //# sourceMappingURL=partial.js.map