partial.js 5.3 KB

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