diff --git a/packages/babel-core/src/config/build-config-chain.js b/packages/babel-core/src/config/build-config-chain.js index 605e32004c..68b97b1dda 100644 --- a/packages/babel-core/src/config/build-config-chain.js +++ b/packages/babel-core/src/config/build-config-chain.js @@ -74,35 +74,42 @@ class ConfigChainBuilder { else res.push(pattern); }); - // Lazy-init so we don't initialize this for files that have no glob patterns. - if (strings.length > 0 && !this.possibleDirs) { - this.possibleDirs = []; + if (res.some((re) => re.test(this.filename))) return true; + if (fns.some((fn) => fn(this.filename))) return true; - if (this.filename) { - this.possibleDirs.push(this.filename); + if (strings.length > 0) { + // Lazy-init so we don't initialize this for files that have no glob patterns. + if (!this.possibleDirs) { + this.possibleDirs = []; - let current = this.filename; - while (true) { - const previous = current; - current = path.dirname(current); - if (previous === current) break; + if (this.filename) { + this.possibleDirs.push(this.filename); - this.possibleDirs.push(current); + let current = this.filename; + while (true) { + const previous = current; + current = path.dirname(current); + if (previous === current) break; + + this.possibleDirs.push(current); + } } } - } - return res.some((re) => re.test(this.filename)) || - fns.some((fn) => fn(this.filename)) || - (strings.length > 0 && this.possibleDirs.some(micromatch.filter(strings.map((pattern) => { + const absolutePatterns = strings.map((pattern) => { // Preserve the "!" prefix so that micromatch can use it for negation. const negate = pattern[0] === "!"; if (negate) pattern = pattern.slice(1); return (negate ? "!" : "") + path.resolve(dirname, pattern); - }, { - nocase: true, - })))); + }); + + if (micromatch(this.possibleDirs, absolutePatterns, { nocase: true }).length > 0) { + return true; + } + } + + return false; } findConfigs(loc: string) { diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index 453e3e86e6..269e6053f7 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -31,7 +31,26 @@ describe("buildConfigChain", function () { process.env.NODE_ENV = oldNodeEnv; }); - // TODO: Tests for ignore and only + describe("ignore/only", () => { + // TODO: More tests for ignore and only + + it("should ignore files that match", () => { + const chain = buildConfigChain({ + filename: fixture("nonexistant-fake", "src.js"), + babelrc: false, + ignore: [ + fixture("nonexistant-fake", "src.js"), + + // We had a regression where multiple ignore patterns broke things, so + // we keep some extra random items in here. + fixture("nonexistant-fake", "other.js"), + fixture("nonexistant-fake", "misc.js"), + ], + }); + + assert.equal(chain, null); + }); + }); it("dir1", function () { const chain = buildConfigChain({