From e7187faea64b64d65a17c57d6578903f1fff27d6 Mon Sep 17 00:00:00 2001 From: Dmitry Soshnikov Date: Wed, 20 Jan 2016 13:36:27 -0800 Subject: [PATCH] Pass per preset: merge nested preset sub-options into preset --- .../src/transformation/file/index.js | 2 +- .../src/transformation/file/options/config.js | 5 +-- .../file/options/option-manager.js | 32 +++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index 88dd4d82bf..c89545c5bc 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -66,7 +66,7 @@ export default class File extends Store { // If we are in the "pass per preset" mode, build // also plugins for each preset. - if (this.opts.passPerPresset) { + if (this.opts.passPerPreset) { // All the "per preset" options are inherited from the main options. this.perPresetOpts = []; this.opts.presets.forEach(presetOpts => { diff --git a/packages/babel-core/src/transformation/file/options/config.js b/packages/babel-core/src/transformation/file/options/config.js index 76d1dc9fe1..cef40bf136 100644 --- a/packages/babel-core/src/transformation/file/options/config.js +++ b/packages/babel-core/src/transformation/file/options/config.js @@ -185,9 +185,10 @@ module.exports = { type: "string" }, - passPerPresset: { + passPerPreset: { description: "Whether to spawn a traversal pass per a preset. By default all presets are merged.", type: "boolean", - default: false + default: false, + hidden: true, }, }; diff --git a/packages/babel-core/src/transformation/file/options/option-manager.js b/packages/babel-core/src/transformation/file/options/option-manager.js index 330631f219..c65108039c 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -157,22 +157,23 @@ export default class OptionManager { throw err; } - this.mergeOptions(opts, loc, null, path.dirname(loc)); + this.mergeOptions(opts, this.options, loc, null, path.dirname(loc)); this.resolvedConfigs.push(loc); return !!opts; } /** - * This is called when we want to merge the input `opts` into our - * base options. + * This is called when we want to merge the input `opts` into the + * base options (passed as the `extendingOpts`: at top-level it's the + * main options, at presets level it's presets options). * * - `alias` is used to output pretty traces back to the original source. * - `loc` is used to point to the original config. * - `dirname` is used to resolve plugins relative to it. */ - mergeOptions(rawOpts?: Object, alias: string = "foreign", loc?: string, dirname?: string) { + mergeOptions(rawOpts?: Object, extendingOpts?: Object, alias: string = "foreign", loc?: string, dirname?: string) { if (!rawOpts) return; // @@ -223,11 +224,9 @@ export default class OptionManager { if (opts.presets) { // If we're in the "pass per preset" mode, we resolve the presets // and keep them for further execution to calculate the options. - if (opts.passPerPresset) { + if (opts.passPerPreset) { opts.presets = this.resolvePresets(opts.presets, dirname, (preset, presetLoc) => { - if (preset.plugins) { - preset.plugins = OptionManager.normalisePlugins(presetLoc, dirname, preset.plugins); - } + this.mergeOptions(preset, preset, presetLoc, presetLoc, dirname); }); } else { // Otherwise, just merge presets options into the main options. @@ -244,11 +243,17 @@ export default class OptionManager { delete opts.env; } - // merge them into this current files options - merge(this.options, opts); + // Merge them into current extending options in case of top-level + // options. In case of presets, just re-assign options which are got + // normalized during the `mergeOptions`. + if (rawOpts !== extendingOpts) { + merge(extendingOpts, opts); + } else { + Object.assign(extendingOpts, opts); + } // merge in env options - this.mergeOptions(envOpts, `${alias}.env.${envKey}`, null, dirname); + this.mergeOptions(envOpts, extendingOpts, `${alias}.env.${envKey}`, null, dirname); } /** @@ -259,6 +264,7 @@ export default class OptionManager { this.resolvePresets(presets, dirname, (presetOpts, presetLoc) => { this.mergeOptions( presetOpts, + this.options, presetLoc, presetLoc, path.dirname(presetLoc) @@ -298,7 +304,7 @@ export default class OptionManager { .map((line) => line.replace(/#(.*?)$/, "").trim()) .filter((line) => !!line); - this.mergeOptions({ ignore: lines }, loc); + this.mergeOptions({ ignore: lines }, this.options, loc); } findConfigs(loc) { @@ -365,7 +371,7 @@ export default class OptionManager { } // merge in base options - this.mergeOptions(opts, "base", null, filename && path.dirname(filename)); + this.mergeOptions(opts, this.options, "base", null, filename && path.dirname(filename)); // normalise this.normaliseOptions(opts);