From 597f1a12cfb2f300195aa87063cd2f7f70395be1 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Thu, 5 Oct 2017 21:32:54 -0400 Subject: [PATCH] Refactor loaded descriptors to allow mutation of options. --- .../babel-core/src/config/option-manager.js | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index e628ac665d..9fd98346f4 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -222,7 +222,10 @@ type BasicDescriptor = { type LoadedDescriptor = { value: {}, - descriptor: BasicDescriptor, + options: ?{}, + dirname: string, + alias: string, + loc: string, }; /** @@ -289,7 +292,7 @@ const loadConfig = makeWeakCache((config): { */ const loadDescriptor = makeWeakCache( (descriptor: BasicDescriptor, cache): LoadedDescriptor => { - const { value, options } = descriptor; + const { value, options, dirname, alias, loc } = descriptor; let item = value; if (typeof value === "function") { @@ -314,7 +317,7 @@ const loadDescriptor = makeWeakCache( throw new Error("Plugin/Preset did not return an object."); } - return { value: item, descriptor }; + return { value: item, options, descriptor, dirname, alias, loc }; }, ); @@ -336,11 +339,14 @@ function loadPluginDescriptor(descriptor: BasicDescriptor): Plugin { } const instantiatePlugin = makeWeakCache( - ({ value: pluginObj, descriptor }: LoadedDescriptor, cache): Plugin => { + ( + { value: pluginObj, options, dirname, alias, loc }: LoadedDescriptor, + cache, + ): Plugin => { Object.keys(pluginObj).forEach(key => { if (!ALLOWED_PLUGIN_KEYS.has(key)) { throw new Error( - `Plugin ${descriptor.alias} provided an invalid property of ${key}`, + `Plugin ${alias} provided an invalid property of ${key}`, ); } }); @@ -364,11 +370,11 @@ const instantiatePlugin = makeWeakCache( let inherits; if (plugin.inherits) { inheritsDescriptor = { - alias: `${descriptor.loc}$inherits`, - loc: descriptor.loc, + alias: `${loc}$inherits`, + loc, value: plugin.inherits, - options: descriptor.options, - dirname: descriptor.dirname, + options, + dirname, }; // If the inherited plugin changes, reinstantiate this plugin. @@ -388,7 +394,7 @@ const instantiatePlugin = makeWeakCache( ]); } - return new Plugin(plugin, descriptor.options, descriptor.alias); + return new Plugin(plugin, options, alias); }, ); @@ -400,13 +406,13 @@ const loadPresetDescriptor = (descriptor: BasicDescriptor): MergeOptions => { }; const instantiatePreset = makeWeakCache( - ({ value, descriptor }: LoadedDescriptor): MergeOptions => { + ({ value, dirname, alias, loc }: LoadedDescriptor): MergeOptions => { return { type: "preset", options: value, - alias: descriptor.alias, - loc: descriptor.loc, - dirname: descriptor.dirname, + alias, + loc, + dirname, }; }, );