UNPKG

10.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.findConfigUpwards = findConfigUpwards;
7exports.findRelativeConfig = findRelativeConfig;
8exports.findRootConfig = findRootConfig;
9exports.loadConfig = loadConfig;
10exports.resolveShowConfigPath = resolveShowConfigPath;
11exports.ROOT_CONFIG_FILENAMES = void 0;
12
13var _caching = require("../caching");
14
15var _configApi = require("../helpers/config-api");
16
17var _utils = require("./utils");
18
19var _moduleTypes = _interopRequireDefault(require("./module-types"));
20
21var _patternToRegex = _interopRequireDefault(require("../pattern-to-regex"));
22
23var fs = _interopRequireWildcard(require("../../gensync-utils/fs"));
24
25function _module() {
26 const data = require("module");
27
28 _module = function () {
29 return data;
30 };
31
32 return data;
33}
34
35function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
36
37function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
41const buildDebug = require("debug");
42
43const path = require("path");
44
45const json5 = require("json5");
46
47const gensync = require("gensync");
48
49const debug = buildDebug("babel:config:loading:files:configuration");
50const ROOT_CONFIG_FILENAMES = ["babel.config.js", "babel.config.cjs", "babel.config.mjs", "babel.config.json"];
51exports.ROOT_CONFIG_FILENAMES = ROOT_CONFIG_FILENAMES;
52const RELATIVE_CONFIG_FILENAMES = [".babelrc", ".babelrc.js", ".babelrc.cjs", ".babelrc.mjs", ".babelrc.json"];
53const BABELIGNORE_FILENAME = ".babelignore";
54
55function* findConfigUpwards(rootDir) {
56 let dirname = rootDir;
57
58 while (true) {
59 for (const filename of ROOT_CONFIG_FILENAMES) {
60 if (yield* fs.exists(path.join(dirname, filename))) {
61 return dirname;
62 }
63 }
64
65 const nextDir = path.dirname(dirname);
66 if (dirname === nextDir) break;
67 dirname = nextDir;
68 }
69
70 return null;
71}
72
73function* findRelativeConfig(packageData, envName, caller) {
74 let config = null;
75 let ignore = null;
76 const dirname = path.dirname(packageData.filepath);
77
78 for (const loc of packageData.directories) {
79 if (!config) {
80 var _packageData$pkg;
81
82 config = yield* loadOneConfig(RELATIVE_CONFIG_FILENAMES, loc, envName, caller, ((_packageData$pkg = packageData.pkg) == null ? void 0 : _packageData$pkg.dirname) === loc ? packageToBabelConfig(packageData.pkg) : null);
83 }
84
85 if (!ignore) {
86 const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
87 ignore = yield* readIgnoreConfig(ignoreLoc);
88
89 if (ignore) {
90 debug("Found ignore %o from %o.", ignore.filepath, dirname);
91 }
92 }
93 }
94
95 return {
96 config,
97 ignore
98 };
99}
100
101function findRootConfig(dirname, envName, caller) {
102 return loadOneConfig(ROOT_CONFIG_FILENAMES, dirname, envName, caller);
103}
104
105function* loadOneConfig(names, dirname, envName, caller, previousConfig = null) {
106 const configs = yield* gensync.all(names.map(filename => readConfig(path.join(dirname, filename), envName, caller)));
107 const config = configs.reduce((previousConfig, config) => {
108 if (config && previousConfig) {
109 throw new Error(`Multiple configuration files found. Please remove one:\n` + ` - ${path.basename(previousConfig.filepath)}\n` + ` - ${config.filepath}\n` + `from ${dirname}`);
110 }
111
112 return config || previousConfig;
113 }, previousConfig);
114
115 if (config) {
116 debug("Found configuration %o from %o.", config.filepath, dirname);
117 }
118
119 return config;
120}
121
122function* loadConfig(name, dirname, envName, caller) {
123 const filepath = (((v, w) => (v = v.split("."), w = w.split("."), +v[0] > +w[0] || v[0] == w[0] && +v[1] >= +w[1]))(process.versions.node, "8.9") ? require.resolve : (r, {
124 paths: [b]
125 }, M = require("module")) => {
126 let f = M._findPath(r, M._nodeModulePaths(b).concat(b));
127
128 if (f) return f;
129 f = new Error(`Cannot resolve module '${r}'`);
130 f.code = "MODULE_NOT_FOUND";
131 throw f;
132 })(name, {
133 paths: [dirname]
134 });
135 const conf = yield* readConfig(filepath, envName, caller);
136
137 if (!conf) {
138 throw new Error(`Config file ${filepath} contains no configuration data`);
139 }
140
141 debug("Loaded config %o from %o.", name, dirname);
142 return conf;
143}
144
145function readConfig(filepath, envName, caller) {
146 const ext = path.extname(filepath);
147 return ext === ".js" || ext === ".cjs" || ext === ".mjs" ? readConfigJS(filepath, {
148 envName,
149 caller
150 }) : readConfigJSON5(filepath);
151}
152
153const LOADING_CONFIGS = new Set();
154const readConfigJS = (0, _caching.makeStrongCache)(function* readConfigJS(filepath, cache) {
155 if (!fs.exists.sync(filepath)) {
156 cache.never();
157 return null;
158 }
159
160 if (LOADING_CONFIGS.has(filepath)) {
161 cache.never();
162 debug("Auto-ignoring usage of config %o.", filepath);
163 return {
164 filepath,
165 dirname: path.dirname(filepath),
166 options: {}
167 };
168 }
169
170 let options;
171
172 try {
173 LOADING_CONFIGS.add(filepath);
174 options = yield* (0, _moduleTypes.default)(filepath, "You appear to be using a native ECMAScript module configuration " + "file, which is only supported when running Babel asynchronously.");
175 } catch (err) {
176 err.message = `${filepath}: Error while loading config - ${err.message}`;
177 throw err;
178 } finally {
179 LOADING_CONFIGS.delete(filepath);
180 }
181
182 let assertCache = false;
183
184 if (typeof options === "function") {
185 yield* [];
186 options = options((0, _configApi.makeConfigAPI)(cache));
187 assertCache = true;
188 }
189
190 if (!options || typeof options !== "object" || Array.isArray(options)) {
191 throw new Error(`${filepath}: Configuration should be an exported JavaScript object.`);
192 }
193
194 if (typeof options.then === "function") {
195 throw new Error(`You appear to be using an async configuration, ` + `which your current version of Babel does not support. ` + `We may add support for this in the future, ` + `but if you're on the most recent version of @babel/core and still ` + `seeing this error, then you'll need to synchronously return your config.`);
196 }
197
198 if (assertCache && !cache.configured()) throwConfigError();
199 return {
200 filepath,
201 dirname: path.dirname(filepath),
202 options
203 };
204});
205const packageToBabelConfig = (0, _caching.makeWeakCacheSync)(file => {
206 const babel = file.options["babel"];
207 if (typeof babel === "undefined") return null;
208
209 if (typeof babel !== "object" || Array.isArray(babel) || babel === null) {
210 throw new Error(`${file.filepath}: .babel property must be an object`);
211 }
212
213 return {
214 filepath: file.filepath,
215 dirname: file.dirname,
216 options: babel
217 };
218});
219const readConfigJSON5 = (0, _utils.makeStaticFileCache)((filepath, content) => {
220 let options;
221
222 try {
223 options = json5.parse(content);
224 } catch (err) {
225 err.message = `${filepath}: Error while parsing config - ${err.message}`;
226 throw err;
227 }
228
229 if (!options) throw new Error(`${filepath}: No config detected`);
230
231 if (typeof options !== "object") {
232 throw new Error(`${filepath}: Config returned typeof ${typeof options}`);
233 }
234
235 if (Array.isArray(options)) {
236 throw new Error(`${filepath}: Expected config object but found array`);
237 }
238
239 return {
240 filepath,
241 dirname: path.dirname(filepath),
242 options
243 };
244});
245const readIgnoreConfig = (0, _utils.makeStaticFileCache)((filepath, content) => {
246 const ignoreDir = path.dirname(filepath);
247 const ignorePatterns = content.split("\n").map(line => line.replace(/#(.*?)$/, "").trim()).filter(line => !!line);
248
249 for (const pattern of ignorePatterns) {
250 if (pattern[0] === "!") {
251 throw new Error(`Negation of file paths is not supported.`);
252 }
253 }
254
255 return {
256 filepath,
257 dirname: path.dirname(filepath),
258 ignore: ignorePatterns.map(pattern => (0, _patternToRegex.default)(pattern, ignoreDir))
259 };
260});
261
262function* resolveShowConfigPath(dirname) {
263 const targetPath = process.env.BABEL_SHOW_CONFIG_FOR;
264
265 if (targetPath != null) {
266 const absolutePath = path.resolve(dirname, targetPath);
267 const stats = yield* fs.stat(absolutePath);
268
269 if (!stats.isFile()) {
270 throw new Error(`${absolutePath}: BABEL_SHOW_CONFIG_FOR must refer to a regular file, directories are not supported.`);
271 }
272
273 return absolutePath;
274 }
275
276 return null;
277}
278
279function throwConfigError() {
280 throw new Error(`\
281Caching was left unconfigured. Babel's plugins, presets, and .babelrc.js files can be configured
282for various types of caching, using the first param of their handler functions:
283
284module.exports = function(api) {
285 // The API exposes the following:
286
287 // Cache the returned value forever and don't call this function again.
288 api.cache(true);
289
290 // Don't cache at all. Not recommended because it will be very slow.
291 api.cache(false);
292
293 // Cached based on the value of some function. If this function returns a value different from
294 // a previously-encountered value, the plugins will re-evaluate.
295 var env = api.cache(() => process.env.NODE_ENV);
296
297 // If testing for a specific env, we recommend specifics to avoid instantiating a plugin for
298 // any possible NODE_ENV value that might come up during plugin execution.
299 var isProd = api.cache(() => process.env.NODE_ENV === "production");
300
301 // .cache(fn) will perform a linear search though instances to find the matching plugin based
302 // based on previous instantiated plugins. If you want to recreate the plugin and discard the
303 // previous instance whenever something changes, you may use:
304 var isProd = api.cache.invalidate(() => process.env.NODE_ENV === "production");
305
306 // Note, we also expose the following more-verbose versions of the above examples:
307 api.cache.forever(); // api.cache(true)
308 api.cache.never(); // api.cache(false)
309 api.cache.using(fn); // api.cache(fn)
310
311 // Return the value that will be cached.
312 return { };
313};`);
314}
\No newline at end of file