Allow plugins and presets to have explicit names.

This commit is contained in:
Logan Smyth 2017-11-22 18:26:12 -08:00
parent cdf420d4d8
commit 8be488652f
3 changed files with 36 additions and 7 deletions

View File

@ -146,16 +146,31 @@ function assertPluginItem(
if (value.length === 0) { if (value.length === 0) {
throw new Error(`.${key}[${index}] must include an object`); 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]); assertPluginTarget(key, index, true, value[0]);
if (value.length === 2) { if (value.length > 1) {
const opts = value[1]; const opts = value[1];
if (opts != null && (typeof opts !== "object" || Array.isArray(opts))) { if (
throw new Error(`.${key}[${index}][1] must be an object, or undefined`); 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 { } else {

View File

@ -141,6 +141,7 @@ class OptionManager {
} }
type BasicDescriptor = { type BasicDescriptor = {
name: string | void,
value: {} | Function, value: {} | Function,
options: {} | void, options: {} | void,
dirname: string, dirname: string,
@ -262,6 +263,7 @@ const instantiatePlugin = makeWeakCache(
if (plugin.inherits) { if (plugin.inherits) {
const inheritsDescriptor = { const inheritsDescriptor = {
name: undefined,
alias: `${alias}$inherits`, alias: `${alias}$inherits`,
value: plugin.inherits, value: plugin.inherits,
options, options,
@ -327,10 +329,16 @@ function createDescriptor(
ownPass?: boolean, ownPass?: boolean,
}, },
): BasicDescriptor { ): BasicDescriptor {
let name;
let options; let options;
let value = pair; let value = pair;
if (Array.isArray(value)) { 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; let filepath = null;
@ -366,6 +374,7 @@ function createDescriptor(
} }
return { return {
name,
alias: filepath || `${alias}$${index}`, alias: filepath || `${alias}$${index}`,
value, value,
options, options,

View File

@ -187,8 +187,13 @@ export type EnvSet<T> = {
export type IgnoreItem = string | Function | RegExp; export type IgnoreItem = string | Function | RegExp;
export type IgnoreList = $ReadOnlyArray<IgnoreItem>; export type IgnoreList = $ReadOnlyArray<IgnoreItem>;
export type PluginOptions = {} | void;
export type PluginTarget = string | {} | Function; 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<PluginItem>; export type PluginList = $ReadOnlyArray<PluginItem>;
export type SourceMapsOption = boolean | "inline" | "both"; export type SourceMapsOption = boolean | "inline" | "both";