UNPKG

5.65 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 = require("path");
11
12 _path = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _gensync() {
20 const data = require("gensync");
21
22 _gensync = function () {
23 return data;
24 };
25
26 return data;
27}
28
29var _plugin = 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
43var _resolveTargets = require("./resolve-targets");
44
45const _excluded = ["showIgnoredFiles"];
46
47function _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; }
48
49function resolveRootMode(rootDir, rootMode) {
50 switch (rootMode) {
51 case "root":
52 return rootDir;
53
54 case "upward-optional":
55 {
56 const upwardRootDir = (0, _files.findConfigUpwards)(rootDir);
57 return upwardRootDir === null ? rootDir : upwardRootDir;
58 }
59
60 case "upward":
61 {
62 const upwardRootDir = (0, _files.findConfigUpwards)(rootDir);
63 if (upwardRootDir !== null) return upwardRootDir;
64 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(", ")}".`), {
65 code: "BABEL_ROOT_NOT_FOUND",
66 dirname: rootDir
67 });
68 }
69
70 default:
71 throw new Error(`Assertion failure - unknown rootMode value.`);
72 }
73}
74
75function* loadPrivatePartialConfig(inputOpts) {
76 if (inputOpts != null && (typeof inputOpts !== "object" || Array.isArray(inputOpts))) {
77 throw new Error("Babel options must be an object, null, or undefined");
78 }
79
80 const args = inputOpts ? (0, _options.validate)("arguments", inputOpts) : {};
81 const {
82 envName = (0, _environment.getEnv)(),
83 cwd = ".",
84 root: rootDir = ".",
85 rootMode = "root",
86 caller,
87 cloneInputAst = true
88 } = args;
89
90 const absoluteCwd = _path().resolve(cwd);
91
92 const absoluteRootDir = resolveRootMode(_path().resolve(absoluteCwd, rootDir), rootMode);
93 const filename = typeof args.filename === "string" ? _path().resolve(cwd, args.filename) : undefined;
94 const showConfigPath = yield* (0, _files.resolveShowConfigPath)(absoluteCwd);
95 const context = {
96 filename,
97 cwd: absoluteCwd,
98 root: absoluteRootDir,
99 envName,
100 caller,
101 showConfig: showConfigPath === filename
102 };
103 const configChain = yield* (0, _configChain.buildRootChain)(args, context);
104 if (!configChain) return null;
105 const merged = {
106 assumptions: {}
107 };
108 configChain.options.forEach(opts => {
109 (0, _util.mergeOptions)(merged, opts);
110 });
111 const options = Object.assign({}, merged, {
112 targets: (0, _resolveTargets.resolveTargets)(merged, absoluteRootDir),
113 cloneInputAst,
114 babelrc: false,
115 configFile: false,
116 browserslistConfigFile: false,
117 passPerPreset: false,
118 envName: context.envName,
119 cwd: context.cwd,
120 root: context.root,
121 rootMode: "root",
122 filename: typeof context.filename === "string" ? context.filename : undefined,
123 plugins: configChain.plugins.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor)),
124 presets: configChain.presets.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor))
125 });
126 return {
127 options,
128 context,
129 fileHandling: configChain.fileHandling,
130 ignore: configChain.ignore,
131 babelrc: configChain.babelrc,
132 config: configChain.config,
133 files: configChain.files
134 };
135}
136
137const loadPartialConfig = _gensync()(function* (opts) {
138 let showIgnoredFiles = false;
139
140 if (typeof opts === "object" && opts !== null && !Array.isArray(opts)) {
141 var _opts = opts;
142 ({
143 showIgnoredFiles
144 } = _opts);
145 opts = _objectWithoutPropertiesLoose(_opts, _excluded);
146 _opts;
147 }
148
149 const result = yield* loadPrivatePartialConfig(opts);
150 if (!result) return null;
151 const {
152 options,
153 babelrc,
154 ignore,
155 config,
156 fileHandling,
157 files
158 } = result;
159
160 if (fileHandling === "ignored" && !showIgnoredFiles) {
161 return null;
162 }
163
164 (options.plugins || []).forEach(item => {
165 if (item.value instanceof _plugin.default) {
166 throw new Error("Passing cached plugin instances is not supported in " + "babel.loadPartialConfig()");
167 }
168 });
169 return new PartialConfig(options, babelrc ? babelrc.filepath : undefined, ignore ? ignore.filepath : undefined, config ? config.filepath : undefined, fileHandling, files);
170});
171
172exports.loadPartialConfig = loadPartialConfig;
173
174class PartialConfig {
175 constructor(options, babelrc, ignore, config, fileHandling, files) {
176 this.options = void 0;
177 this.babelrc = void 0;
178 this.babelignore = void 0;
179 this.config = void 0;
180 this.fileHandling = void 0;
181 this.files = void 0;
182 this.options = options;
183 this.babelignore = ignore;
184 this.babelrc = babelrc;
185 this.config = config;
186 this.fileHandling = fileHandling;
187 this.files = files;
188 Object.freeze(this);
189 }
190
191 hasFilesystemConfig() {
192 return this.babelrc !== undefined || this.config !== undefined;
193 }
194
195}
196
197Object.freeze(PartialConfig.prototype);
\No newline at end of file