partial.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 = _interopRequireDefault(require("path"));
  9. _path = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. var _plugin = _interopRequireDefault(require("./plugin"));
  15. var _util = require("./util");
  16. var _item = require("./item");
  17. var _configChain = require("./config-chain");
  18. var _environment = require("./helpers/environment");
  19. var _options = require("./validation/options");
  20. var _files = require("./files");
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function resolveRootMode(rootDir, rootMode) {
  23. switch (rootMode) {
  24. case "root":
  25. return rootDir;
  26. case "upward-optional":
  27. {
  28. const upwardRootDir = (0, _files.findConfigUpwards)(rootDir);
  29. return upwardRootDir === null ? rootDir : upwardRootDir;
  30. }
  31. case "upward":
  32. {
  33. const upwardRootDir = (0, _files.findConfigUpwards)(rootDir);
  34. if (upwardRootDir !== null) return upwardRootDir;
  35. throw Object.assign(new Error(`Babel was run with rootMode:"upward" but a root could not ` + `be found when searching upward from "${rootDir}"`), {
  36. code: "BABEL_ROOT_NOT_FOUND",
  37. dirname: rootDir
  38. });
  39. }
  40. default:
  41. throw new Error(`Assertion failure - unknown rootMode value`);
  42. }
  43. }
  44. function loadPrivatePartialConfig(inputOpts) {
  45. if (inputOpts != null && (typeof inputOpts !== "object" || Array.isArray(inputOpts))) {
  46. throw new Error("Babel options must be an object, null, or undefined");
  47. }
  48. const args = inputOpts ? (0, _options.validate)("arguments", inputOpts) : {};
  49. const {
  50. envName = (0, _environment.getEnv)(),
  51. cwd = ".",
  52. root: rootDir = ".",
  53. rootMode = "root",
  54. caller
  55. } = args;
  56. const absoluteCwd = _path().default.resolve(cwd);
  57. const absoluteRootDir = resolveRootMode(_path().default.resolve(absoluteCwd, rootDir), rootMode);
  58. const context = {
  59. filename: typeof args.filename === "string" ? _path().default.resolve(cwd, args.filename) : undefined,
  60. cwd: absoluteCwd,
  61. root: absoluteRootDir,
  62. envName,
  63. caller
  64. };
  65. const configChain = (0, _configChain.buildRootChain)(args, context);
  66. if (!configChain) return null;
  67. const options = {};
  68. configChain.options.forEach(opts => {
  69. (0, _util.mergeOptions)(options, opts);
  70. });
  71. options.babelrc = false;
  72. options.configFile = false;
  73. options.passPerPreset = false;
  74. options.envName = context.envName;
  75. options.cwd = context.cwd;
  76. options.root = context.root;
  77. options.filename = typeof context.filename === "string" ? context.filename : undefined;
  78. options.plugins = configChain.plugins.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor));
  79. options.presets = configChain.presets.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor));
  80. return {
  81. options,
  82. context,
  83. ignore: configChain.ignore,
  84. babelrc: configChain.babelrc,
  85. config: configChain.config
  86. };
  87. }
  88. function loadPartialConfig(inputOpts) {
  89. const result = loadPrivatePartialConfig(inputOpts);
  90. if (!result) return null;
  91. const {
  92. options,
  93. babelrc,
  94. ignore,
  95. config
  96. } = result;
  97. (options.plugins || []).forEach(item => {
  98. if (item.value instanceof _plugin.default) {
  99. throw new Error("Passing cached plugin instances is not supported in " + "babel.loadPartialConfig()");
  100. }
  101. });
  102. return new PartialConfig(options, babelrc ? babelrc.filepath : undefined, ignore ? ignore.filepath : undefined, config ? config.filepath : undefined);
  103. }
  104. class PartialConfig {
  105. constructor(options, babelrc, ignore, config) {
  106. this.options = options;
  107. this.babelignore = ignore;
  108. this.babelrc = babelrc;
  109. this.config = config;
  110. Object.freeze(this);
  111. }
  112. hasFilesystemConfig() {
  113. return this.babelrc !== undefined || this.config !== undefined;
  114. }
  115. }
  116. Object.freeze(PartialConfig.prototype);