diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index f160f8a253..e81ca81f74 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -527,7 +527,7 @@ function dedupDescriptors( ): Array { const map: Map< Function, - Map, + Map, > = new Map(); const descriptors = []; @@ -542,16 +542,12 @@ function dedupDescriptors( } let desc = nameMap.get(item.name); if (!desc) { - desc = { value: null }; + desc = { value: item }; descriptors.push(desc); // Treat passPerPreset presets as unique, skipping them // in the merge processing steps. if (!item.ownPass) nameMap.set(item.name, desc); - } - - if (item.options === false) { - desc.value = null; } else { desc.value = item; } @@ -561,7 +557,7 @@ function dedupDescriptors( } return descriptors.reduce((acc, desc) => { - if (desc.value) acc.push(desc.value); + acc.push(desc.value); return acc; }, []); } diff --git a/packages/babel-core/src/config/full.js b/packages/babel-core/src/config/full.js index 8892056f76..ffdb71b02d 100644 --- a/packages/babel-core/src/config/full.js +++ b/packages/babel-core/src/config/full.js @@ -68,15 +68,21 @@ export default function loadFullConfig( }, pass: Array, ) { - const plugins = config.plugins.map(descriptor => { - return loadPluginDescriptor(descriptor, context); - }); - const presets = config.presets.map(descriptor => { - return { - preset: loadPresetDescriptor(descriptor, context), - pass: descriptor.ownPass ? [] : pass, - }; - }); + const plugins = config.plugins.reduce((acc, descriptor) => { + if (descriptor.options !== false) { + acc.push(loadPluginDescriptor(descriptor, context)); + } + return acc; + }, []); + const presets = config.presets.reduce((acc, descriptor) => { + if (descriptor.options !== false) { + acc.push({ + preset: loadPresetDescriptor(descriptor, context), + pass: descriptor.ownPass ? [] : pass, + }); + } + return acc; + }, []); // resolve presets if (presets.length > 0) { diff --git a/packages/babel-core/src/config/item.js b/packages/babel-core/src/config/item.js index ab9b51207e..c847df029e 100644 --- a/packages/babel-core/src/config/item.js +++ b/packages/babel-core/src/config/item.js @@ -71,9 +71,11 @@ class ConfigItem { /** * The options, if any, that were passed to the item. - * Mutating this will lead to undefined behavior. If you need + * Mutating this will lead to undefined behavior. + * + * "false" means that this item has been disabled. */ - options: {} | void; + options: {} | void | false; /** * The directory that the options for this item are relative to. @@ -103,10 +105,6 @@ class ConfigItem { this._descriptor = descriptor; Object.defineProperty(this, "_descriptor", ({ enumerable: false }: any)); - if (this._descriptor.options === false) { - throw new Error("Assertion failure - unexpected false options"); - } - this.value = this._descriptor.value; this.options = this._descriptor.options; this.dirname = this._descriptor.dirname; diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index 65eb0c6034..d53b746b2a 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -1,4 +1,4 @@ -import loadConfig from "../lib/config"; +import loadConfig, { loadPartialConfig } from "../lib/config"; import path from "path"; describe("@babel/core config loading", () => { @@ -36,6 +36,46 @@ describe("@babel/core config loading", () => { }; } + describe("loadPartialConfig", () => { + it("should preserve disabled plugins in the partial config", () => { + const plugin = function() { + return {}; + }; + + const opts = loadPartialConfig({ + ...makeOpts(true), + babelrc: false, + configFile: false, + plugins: [[plugin, false]], + }); + + expect(opts.options.plugins.length).toBe(1); + const item = opts.options.plugins[0]; + + expect(item.value).toBe(plugin); + expect(item.options).toBe(false); + }); + + it("should preserve disabled presets in the partial config", () => { + const preset = function() { + return {}; + }; + + const opts = loadPartialConfig({ + ...makeOpts(true), + babelrc: false, + configFile: false, + presets: [[preset, false]], + }); + + expect(opts.options.presets.length).toBe(1); + const item = opts.options.presets[0]; + + expect(item.value).toBe(preset); + expect(item.options).toBe(false); + }); + }); + describe("config file", () => { it("should load and cache the config with plugins and presets", () => { const opts = makeOpts();