UNPKG

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