diff --git a/packages/babel-core/src/config/full.js b/packages/babel-core/src/config/full.js index 8a1a26c0e29e..eba908c2566e 100644 --- a/packages/babel-core/src/config/full.js +++ b/packages/babel-core/src/config/full.js @@ -18,6 +18,7 @@ import { validatePluginObject } from "./validation/plugins"; import makeAPI from "./helpers/config-api"; import loadPrivatePartialConfig from "./partial"; +import type { ValidatedOptions } from "./validation/options"; type LoadedDescriptor = { value: {}, @@ -278,6 +279,18 @@ const instantiatePlugin = makeWeakCache( }, ); +const validateIfOptionNeedsFilename = ( + options: ValidatedOptions, + descriptor: UnloadedDescriptor, +): void => { + if (options.test || options.include || options.exclude) { + throw new Error( + `Preset ${descriptor.name || + ""} requires filename, but it was not passed.`.replace(/\s{2}/, " "), + ); + } +}; + const validatePreset = ( preset: PresetInstance, context: ConfigContext, @@ -285,10 +298,10 @@ const validatePreset = ( ): void => { if (!context.filename) { const { options } = preset; - if (options.test || options.include || options.exclude) { - throw new Error( - `Preset ${descriptor.name || - ""} requires filename, but it was not passed.`.replace(/\s{2}/, " "), + validateIfOptionNeedsFilename(options, descriptor); + if (options.overrides) { + options.overrides.forEach(overrideOptions => + validateIfOptionNeedsFilename(overrideOptions, descriptor), ); } } diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index 84a816eab445..7aacbe62d1e9 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -1063,5 +1063,13 @@ describe("buildConfigChain", function() { }); }).toThrow(/Preset requires filename/); }); + + it("should throw when `preset.overrides` requires `filename` but it was not passed", () => { + expect(() => { + loadOptions({ + presets: [require("./fixtures/config-loading/preset5")], + }); + }).toThrow(/Preset requires filename/); + }); }); }); diff --git a/packages/babel-core/test/fixtures/config-loading/preset5.js b/packages/babel-core/test/fixtures/config-loading/preset5.js new file mode 100644 index 000000000000..65ecb47f93c8 --- /dev/null +++ b/packages/babel-core/test/fixtures/config-loading/preset5.js @@ -0,0 +1,8 @@ +module.exports = function() { + return { + overrides: [{ + test: /\.ts$/, + plugins: [] + }] + } +};