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) {
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 {

View File

@ -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,11 +329,17 @@ function createDescriptor(
ownPass?: boolean,
},
): BasicDescriptor {
let name;
let options;
let value = pair;
if (Array.isArray(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;
if (typeof value === "string") {
@ -366,6 +374,7 @@ function createDescriptor(
}
return {
name,
alias: filepath || `${alias}$${index}`,
value,
options,

View File

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