diff --git a/packages/babel-core/src/config/build-config-chain.js b/packages/babel-core/src/config/build-config-chain.js index c5f738cc11..5b4b8cee24 100644 --- a/packages/babel-core/src/config/build-config-chain.js +++ b/packages/babel-core/src/config/build-config-chain.js @@ -69,6 +69,7 @@ export default function buildConfigChain( class ConfigChainBuilder { file: LoadedFile | null; configs: Array = []; + seenFiles: Set = new Set(); constructor(file: LoadedFile | null) { this.file = file; @@ -85,9 +86,17 @@ class ConfigChainBuilder { } mergeConfigFile(file: ConfigFile, envKey: string) { - flattenFileOptionsParts(file)(envKey).forEach(part => - this._processConfigPart(part, envKey), - ); + if (this.seenFiles.has(file)) { + throw new Error( + `Cycle detected in Babel configuration file through "${file.filepath}".`, + ); + } + + const parts = flattenFileOptionsParts(file)(envKey); + + this.seenFiles.add(file); + parts.forEach(part => this._processConfigPart(part, envKey)); + this.seenFiles.delete(file); } _processConfigPart(part: ConfigPart, envKey: string) { @@ -107,14 +116,7 @@ class ConfigChainBuilder { this.configs.push(part.config); } else { - const extendsConfig = loadConfig(part.path, part.dirname); - - const existingConfig = this.configs.some(config => { - return config.alias === extendsConfig.filepath; - }); - if (!existingConfig) { - this.mergeConfigFile(extendsConfig, envKey); - } + this.mergeConfigFile(loadConfig(part.path, part.dirname), envKey); } } }