aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/core/lib/config/config-chain.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@babel/core/lib/config/config-chain.js')
-rw-r--r--node_modules/@babel/core/lib/config/config-chain.js487
1 files changed, 487 insertions, 0 deletions
diff --git a/node_modules/@babel/core/lib/config/config-chain.js b/node_modules/@babel/core/lib/config/config-chain.js
new file mode 100644
index 00000000..22bca2bd
--- /dev/null
+++ b/node_modules/@babel/core/lib/config/config-chain.js
@@ -0,0 +1,487 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.buildRootChain = buildRootChain;
+exports.buildPresetChain = void 0;
+
+function _path() {
+ const data = _interopRequireDefault(require("path"));
+
+ _path = function _path() {
+ return data;
+ };
+
+ return data;
+}
+
+function _micromatch() {
+ const data = _interopRequireDefault(require("micromatch"));
+
+ _micromatch = function _micromatch() {
+ return data;
+ };
+
+ return data;
+}
+
+function _debug() {
+ const data = _interopRequireDefault(require("debug"));
+
+ _debug = function _debug() {
+ return data;
+ };
+
+ return data;
+}
+
+var _options = require("./validation/options");
+
+var _files = require("./files");
+
+var _caching = require("./caching");
+
+var _configDescriptors = require("./config-descriptors");
+
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
+
+const debug = (0, _debug().default)("babel:config:config-chain");
+const buildPresetChain = makeChainWalker({
+ init: arg => arg,
+ root: preset => loadPresetDescriptors(preset),
+ env: (preset, envName) => loadPresetEnvDescriptors(preset)(envName),
+ overrides: (preset, index) => loadPresetOverridesDescriptors(preset)(index),
+ overridesEnv: (preset, index, envName) => loadPresetOverridesEnvDescriptors(preset)(index)(envName)
+});
+exports.buildPresetChain = buildPresetChain;
+const loadPresetDescriptors = (0, _caching.makeWeakCache)(preset => buildRootDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors));
+const loadPresetEnvDescriptors = (0, _caching.makeWeakCache)(preset => (0, _caching.makeStrongCache)(envName => buildEnvDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors, envName)));
+const loadPresetOverridesDescriptors = (0, _caching.makeWeakCache)(preset => (0, _caching.makeStrongCache)(index => buildOverrideDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors, index)));
+const loadPresetOverridesEnvDescriptors = (0, _caching.makeWeakCache)(preset => (0, _caching.makeStrongCache)(index => (0, _caching.makeStrongCache)(envName => buildOverrideEnvDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors, index, envName))));
+
+function buildRootChain(opts, context) {
+ const programmaticChain = loadProgrammaticChain({
+ options: opts,
+ dirname: context.cwd
+ }, context);
+ if (!programmaticChain) return null;
+ const _opts$root = opts.root,
+ rootDir = _opts$root === void 0 ? "." : _opts$root,
+ _opts$configFile = opts.configFile,
+ configFileName = _opts$configFile === void 0 ? true : _opts$configFile;
+ let babelrc = opts.babelrc,
+ babelrcRoots = opts.babelrcRoots;
+
+ const absoluteRoot = _path().default.resolve(context.cwd, rootDir);
+
+ let configFile;
+
+ if (typeof configFileName === "string") {
+ configFile = (0, _files.loadConfig)(configFileName, context.cwd, context.envName);
+ } else if (configFileName === true) {
+ configFile = (0, _files.findRootConfig)(absoluteRoot, context.envName);
+ }
+
+ const configFileChain = emptyChain();
+
+ if (configFile) {
+ const validatedFile = validateConfigFile(configFile);
+ const result = loadFileChain(validatedFile, context);
+ if (!result) return null;
+
+ if (babelrc === undefined) {
+ babelrc = validatedFile.options.babelrc;
+ }
+
+ if (babelrcRoots === undefined) {
+ babelrcRoots = validatedFile.options.babelrcRoots;
+ }
+
+ mergeChain(configFileChain, result);
+ }
+
+ const pkgData = typeof context.filename === "string" ? (0, _files.findPackageData)(context.filename) : null;
+ let ignoreFile, babelrcFile;
+ const fileChain = emptyChain();
+
+ if ((babelrc === true || babelrc === undefined) && pkgData && babelrcLoadEnabled(context, pkgData, babelrcRoots, absoluteRoot)) {
+ var _findRelativeConfig = (0, _files.findRelativeConfig)(pkgData, context.envName);
+
+ ignoreFile = _findRelativeConfig.ignore;
+ babelrcFile = _findRelativeConfig.config;
+
+ if (ignoreFile && shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname)) {
+ return null;
+ }
+
+ if (babelrcFile) {
+ const result = loadFileChain(validateBabelrcFile(babelrcFile), context);
+ if (!result) return null;
+ mergeChain(fileChain, result);
+ }
+ }
+
+ const chain = mergeChain(mergeChain(mergeChain(emptyChain(), configFileChain), fileChain), programmaticChain);
+ return {
+ plugins: dedupDescriptors(chain.plugins),
+ presets: dedupDescriptors(chain.presets),
+ options: chain.options.map(o => normalizeOptions(o)),
+ ignore: ignoreFile || undefined,
+ babelrc: babelrcFile || undefined,
+ config: configFile || undefined
+ };
+}
+
+function babelrcLoadEnabled(context, pkgData, babelrcRoots, absoluteRoot) {
+ if (typeof babelrcRoots === "boolean") return babelrcRoots;
+
+ if (babelrcRoots === undefined) {
+ return pkgData.directories.indexOf(absoluteRoot) !== -1;
+ }
+
+ let babelrcPatterns = babelrcRoots;
+ if (!Array.isArray(babelrcPatterns)) babelrcPatterns = [babelrcPatterns];
+ babelrcPatterns = babelrcPatterns.map(pat => _path().default.resolve(context.cwd, pat));
+
+ if (babelrcPatterns.length === 1 && babelrcPatterns[0] === absoluteRoot) {
+ return pkgData.directories.indexOf(absoluteRoot) !== -1;
+ }
+
+ return (0, _micromatch().default)(pkgData.directories, babelrcPatterns).length > 0;
+}
+
+const validateConfigFile = (0, _caching.makeWeakCache)(file => ({
+ filepath: file.filepath,
+ dirname: file.dirname,
+ options: (0, _options.validate)("configfile", file.options)
+}));
+const validateBabelrcFile = (0, _caching.makeWeakCache)(file => ({
+ filepath: file.filepath,
+ dirname: file.dirname,
+ options: (0, _options.validate)("babelrcfile", file.options)
+}));
+const validateExtendFile = (0, _caching.makeWeakCache)(file => ({
+ filepath: file.filepath,
+ dirname: file.dirname,
+ options: (0, _options.validate)("extendsfile", file.options)
+}));
+const loadProgrammaticChain = makeChainWalker({
+ root: input => buildRootDescriptors(input, "base", _configDescriptors.createCachedDescriptors),
+ env: (input, envName) => buildEnvDescriptors(input, "base", _configDescriptors.createCachedDescriptors, envName),
+ overrides: (input, index) => buildOverrideDescriptors(input, "base", _configDescriptors.createCachedDescriptors, index),
+ overridesEnv: (input, index, envName) => buildOverrideEnvDescriptors(input, "base", _configDescriptors.createCachedDescriptors, index, envName)
+});
+const loadFileChain = makeChainWalker({
+ root: file => loadFileDescriptors(file),
+ env: (file, envName) => loadFileEnvDescriptors(file)(envName),
+ overrides: (file, index) => loadFileOverridesDescriptors(file)(index),
+ overridesEnv: (file, index, envName) => loadFileOverridesEnvDescriptors(file)(index)(envName)
+});
+const loadFileDescriptors = (0, _caching.makeWeakCache)(file => buildRootDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors));
+const loadFileEnvDescriptors = (0, _caching.makeWeakCache)(file => (0, _caching.makeStrongCache)(envName => buildEnvDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors, envName)));
+const loadFileOverridesDescriptors = (0, _caching.makeWeakCache)(file => (0, _caching.makeStrongCache)(index => buildOverrideDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors, index)));
+const loadFileOverridesEnvDescriptors = (0, _caching.makeWeakCache)(file => (0, _caching.makeStrongCache)(index => (0, _caching.makeStrongCache)(envName => buildOverrideEnvDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors, index, envName))));
+
+function buildRootDescriptors({
+ dirname,
+ options
+}, alias, descriptors) {
+ return descriptors(dirname, options, alias);
+}
+
+function buildEnvDescriptors({
+ dirname,
+ options
+}, alias, descriptors, envName) {
+ const opts = options.env && options.env[envName];
+ return opts ? descriptors(dirname, opts, `${alias}.env["${envName}"]`) : null;
+}
+
+function buildOverrideDescriptors({
+ dirname,
+ options
+}, alias, descriptors, index) {
+ const opts = options.overrides && options.overrides[index];
+ if (!opts) throw new Error("Assertion failure - missing override");
+ return descriptors(dirname, opts, `${alias}.overrides[${index}]`);
+}
+
+function buildOverrideEnvDescriptors({
+ dirname,
+ options
+}, alias, descriptors, index, envName) {
+ const override = options.overrides && options.overrides[index];
+ if (!override) throw new Error("Assertion failure - missing override");
+ const opts = override.env && override.env[envName];
+ return opts ? descriptors(dirname, opts, `${alias}.overrides[${index}].env["${envName}"]`) : null;
+}
+
+function makeChainWalker({
+ root,
+ env,
+ overrides,
+ overridesEnv
+}) {
+ return (input, context, files = new Set()) => {
+ const dirname = input.dirname;
+ const flattenedConfigs = [];
+ const rootOpts = root(input);
+
+ if (configIsApplicable(rootOpts, dirname, context)) {
+ flattenedConfigs.push(rootOpts);
+ const envOpts = env(input, context.envName);
+
+ if (envOpts && configIsApplicable(envOpts, dirname, context)) {
+ flattenedConfigs.push(envOpts);
+ }
+
+ (rootOpts.options.overrides || []).forEach((_, index) => {
+ const overrideOps = overrides(input, index);
+
+ if (configIsApplicable(overrideOps, dirname, context)) {
+ flattenedConfigs.push(overrideOps);
+ const overrideEnvOpts = overridesEnv(input, index, context.envName);
+
+ if (overrideEnvOpts && configIsApplicable(overrideEnvOpts, dirname, context)) {
+ flattenedConfigs.push(overrideEnvOpts);
+ }
+ }
+ });
+ }
+
+ if (flattenedConfigs.some(({
+ options: {
+ ignore,
+ only
+ }
+ }) => shouldIgnore(context, ignore, only, dirname))) {
+ return null;
+ }
+
+ const chain = emptyChain();
+
+ for (var _i = 0; _i < flattenedConfigs.length; _i++) {
+ const op = flattenedConfigs[_i];
+
+ if (!mergeExtendsChain(chain, op.options, dirname, context, files)) {
+ return null;
+ }
+
+ mergeChainOpts(chain, op);
+ }
+
+ return chain;
+ };
+}
+
+function mergeExtendsChain(chain, opts, dirname, context, files) {
+ if (opts.extends === undefined) return true;
+ const file = (0, _files.loadConfig)(opts.extends, dirname, context.envName);
+
+ if (files.has(file)) {
+ throw new Error(`Configuration cycle detected loading ${file.filepath}.\n` + `File already loaded following the config chain:\n` + Array.from(files, file => ` - ${file.filepath}`).join("\n"));
+ }
+
+ files.add(file);
+ const fileChain = loadFileChain(validateExtendFile(file), context, files);
+ files.delete(file);
+ if (!fileChain) return false;
+ mergeChain(chain, fileChain);
+ return true;
+}
+
+function mergeChain(target, source) {
+ target.options.push(...source.options);
+ target.plugins.push(...source.plugins);
+ target.presets.push(...source.presets);
+ return target;
+}
+
+function mergeChainOpts(target, {
+ options,
+ plugins,
+ presets
+}) {
+ target.options.push(options);
+ target.plugins.push(...plugins());
+ target.presets.push(...presets());
+ return target;
+}
+
+function emptyChain() {
+ return {
+ options: [],
+ presets: [],
+ plugins: []
+ };
+}
+
+function normalizeOptions(opts) {
+ const options = Object.assign({}, opts);
+ delete options.extends;
+ delete options.env;
+ delete options.plugins;
+ delete options.presets;
+ delete options.passPerPreset;
+ delete options.ignore;
+ delete options.only;
+
+ if (options.sourceMap) {
+ options.sourceMaps = options.sourceMap;
+ delete options.sourceMap;
+ }
+
+ return options;
+}
+
+function dedupDescriptors(items) {
+ const map = new Map();
+ const descriptors = [];
+
+ for (var _iterator = items, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
+ var _ref;
+
+ if (_isArray) {
+ if (_i2 >= _iterator.length) break;
+ _ref = _iterator[_i2++];
+ } else {
+ _i2 = _iterator.next();
+ if (_i2.done) break;
+ _ref = _i2.value;
+ }
+
+ const item = _ref;
+
+ if (typeof item.value === "function") {
+ const fnKey = item.value;
+ let nameMap = map.get(fnKey);
+
+ if (!nameMap) {
+ nameMap = new Map();
+ map.set(fnKey, nameMap);
+ }
+
+ let desc = nameMap.get(item.name);
+
+ if (!desc) {
+ desc = {
+ value: null
+ };
+ descriptors.push(desc);
+ if (!item.ownPass) nameMap.set(item.name, desc);
+ }
+
+ if (item.options === false) {
+ desc.value = null;
+ } else {
+ desc.value = item;
+ }
+ } else {
+ descriptors.push({
+ value: item
+ });
+ }
+ }
+
+ return descriptors.reduce((acc, desc) => {
+ if (desc.value) acc.push(desc.value);
+ return acc;
+ }, []);
+}
+
+function configIsApplicable({
+ options
+}, dirname, context) {
+ return (options.test === undefined || configFieldIsApplicable(context, options.test, dirname)) && (options.include === undefined || configFieldIsApplicable(context, options.include, dirname)) && (options.exclude === undefined || !configFieldIsApplicable(context, options.exclude, dirname));
+}
+
+function configFieldIsApplicable(context, test, dirname) {
+ if (context.filename === null) {
+ throw new Error(`Configuration contains explicit test/include/exclude checks, but no filename was passed to Babel`);
+ }
+
+ const ctx = context;
+ const patterns = Array.isArray(test) ? test : [test];
+ return matchesPatterns(ctx, patterns, dirname, false);
+}
+
+function shouldIgnore(context, ignore, only, dirname) {
+ if (ignore) {
+ if (context.filename === null) {
+ throw new Error(`Configuration contains ignore checks, but no filename was passed to Babel`);
+ }
+
+ const ctx = context;
+
+ if (matchesPatterns(ctx, ignore, dirname)) {
+ debug("Ignored %o because it matched one of %O from %o", context.filename, ignore, dirname);
+ return true;
+ }
+ }
+
+ if (only) {
+ if (context.filename === null) {
+ throw new Error(`Configuration contains ignore checks, but no filename was passed to Babel`);
+ }
+
+ const ctx = context;
+
+ if (!matchesPatterns(ctx, only, dirname)) {
+ debug("Ignored %o because it failed to match one of %O from %o", context.filename, only, dirname);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+function matchesPatterns(context, patterns, dirname, allowNegation = true) {
+ const res = [];
+ const strings = [];
+ const fns = [];
+ patterns.forEach(pattern => {
+ if (typeof pattern === "string") strings.push(pattern);else if (typeof pattern === "function") fns.push(pattern);else res.push(pattern);
+ });
+ const filename = context.filename;
+ if (res.some(re => re.test(context.filename))) return true;
+ if (fns.some(fn => fn(filename))) return true;
+
+ if (strings.length > 0) {
+ const possibleDirs = getPossibleDirs(context);
+ const absolutePatterns = strings.map(pattern => {
+ const negate = pattern[0] === "!";
+
+ if (negate && !allowNegation) {
+ throw new Error(`Negation of file paths is not supported.`);
+ }
+
+ if (negate) pattern = pattern.slice(1);
+ return (negate ? "!" : "") + _path().default.resolve(dirname, pattern);
+ });
+
+ if ((0, _micromatch().default)(possibleDirs, absolutePatterns, {
+ nocase: true,
+ nonegate: !allowNegation
+ }).length > 0) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+const getPossibleDirs = (0, _caching.makeWeakCache)(context => {
+ let current = context.filename;
+ if (current === null) return [];
+ const possibleDirs = [current];
+
+ while (true) {
+ const previous = current;
+ current = _path().default.dirname(current);
+ if (previous === current) break;
+ possibleDirs.push(current);
+ }
+
+ return possibleDirs;
+}); \ No newline at end of file