From 8be488652f8b555e8a58336894ab4ef47a0394bc Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 22 Nov 2017 18:26:12 -0800 Subject: [PATCH] Allow plugins and presets to have explicit names. --- .../src/config/option-assertions.js | 25 +++++++++++++++---- .../babel-core/src/config/option-manager.js | 11 +++++++- packages/babel-core/src/config/options.js | 7 +++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/babel-core/src/config/option-assertions.js b/packages/babel-core/src/config/option-assertions.js index 693b4fea92..8cdb7a5000 100644 --- a/packages/babel-core/src/config/option-assertions.js +++ b/packages/babel-core/src/config/option-assertions.js @@ -146,16 +146,31 @@ function assertPluginItem( if (value.length === 0) { throw new Error(`.${key}[${index}] must include an object`); } - if (value.length > 2) { - throw new Error(`.${key}[${index}] may only be a two-tuple`); + + if (value.length > 3) { + throw new Error( + `.${key}[${index}] may only be a two-tuple or three-tuple`, + ); } assertPluginTarget(key, index, true, value[0]); - if (value.length === 2) { + if (value.length > 1) { const opts = value[1]; - if (opts != null && (typeof opts !== "object" || Array.isArray(opts))) { - throw new Error(`.${key}[${index}][1] must be an object, or undefined`); + if ( + opts !== undefined && + opts !== false && + (typeof opts !== "object" || Array.isArray(opts)) + ) { + throw new Error( + `.${key}[${index}][1] must be an object, false, or undefined`, + ); + } + } + if (value.length === 3) { + const name = value[2]; + if (name !== undefined && typeof name !== "string") { + throw new Error(`.${key}[${index}][2] must be a string, or undefined`); } } } else { diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index 355e1057d2..b026725926 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -141,6 +141,7 @@ class OptionManager { } type BasicDescriptor = { + name: string | void, value: {} | Function, options: {} | void, dirname: string, @@ -262,6 +263,7 @@ const instantiatePlugin = makeWeakCache( if (plugin.inherits) { const inheritsDescriptor = { + name: undefined, alias: `${alias}$inherits`, value: plugin.inherits, options, @@ -327,10 +329,16 @@ function createDescriptor( ownPass?: boolean, }, ): BasicDescriptor { + let name; let options; let value = pair; if (Array.isArray(value)) { - [value, options] = value; + if (value.length === 3) { + // $FlowIgnore - Flow doesn't like the multiple tuple types. + [value, options, name] = value; + } else { + [value, options] = value; + } } let filepath = null; @@ -366,6 +374,7 @@ function createDescriptor( } return { + name, alias: filepath || `${alias}$${index}`, value, options, diff --git a/packages/babel-core/src/config/options.js b/packages/babel-core/src/config/options.js index 9aec95dfa8..3e5b36688a 100644 --- a/packages/babel-core/src/config/options.js +++ b/packages/babel-core/src/config/options.js @@ -187,8 +187,13 @@ export type EnvSet = { export type IgnoreItem = string | Function | RegExp; export type IgnoreList = $ReadOnlyArray; +export type PluginOptions = {} | void; export type PluginTarget = string | {} | Function; -export type PluginItem = PluginTarget | [PluginTarget, {} | void]; +export type PluginItem = + | Plugin + | PluginTarget + | [PluginTarget, PluginOptions] + | [PluginTarget, PluginOptions, string]; export type PluginList = $ReadOnlyArray; export type SourceMapsOption = boolean | "inline" | "both";