| 1 |
import type { Handler } from "gensync"; |
| 2 |
import type { PluginTarget, PluginOptions } from "./validation/options"; |
| 3 |
|
| 4 |
import path from "path"; |
| 5 |
import { createDescriptor } from "./config-descriptors"; |
| 6 |
|
| 7 |
import type { UnloadedDescriptor } from "./config-descriptors"; |
| 8 |
|
| 9 |
export function createItemFromDescriptor(desc: UnloadedDescriptor): ConfigItem { |
| 10 |
return new ConfigItem(desc); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Create a config item using the same value format used in Babel's config |
| 15 |
* files. Items returned from this function should be cached by the caller |
| 16 |
* ideally, as recreating the config item will mean re-resolving the item |
| 17 |
* and re-evaluating the plugin/preset function. |
| 18 |
*/ |
| 19 |
export function* createConfigItem( |
| 20 |
value: |
| 21 |
| PluginTarget |
| 22 |
| [PluginTarget, PluginOptions] |
| 23 |
| [PluginTarget, PluginOptions, string | void], |
| 24 |
{ |
| 25 |
dirname = ".", |
| 26 |
type, |
| 27 |
}: { |
| 28 |
dirname?: string; |
| 29 |
type?: "preset" | "plugin"; |
| 30 |
} = {}, |
| 31 |
): Handler<ConfigItem> { |
| 32 |
const descriptor = yield* createDescriptor(value, path.resolve(dirname), { |
| 33 |
type, |
| 34 |
alias: "programmatic item", |
| 35 |
}); |
| 36 |
|
| 37 |
return createItemFromDescriptor(descriptor); |
| 38 |
} |
| 39 |
|
| 40 |
const CONFIG_ITEM_BRAND = Symbol.for("@babel/core@7 - ConfigItem"); |
| 41 |
|
| 42 |
export function getItemDescriptor(item: unknown): UnloadedDescriptor | void { |
| 43 |
if ((item as any)?.[CONFIG_ITEM_BRAND]) { |
| 44 |
return (item as ConfigItem)._descriptor; |
| 45 |
} |
| 46 |
|
| 47 |
return undefined; |
| 48 |
} |
| 49 |
|
| 50 |
export type { ConfigItem }; |
| 51 |
|
| 52 |
/** |
| 53 |
* A public representation of a plugin/preset that will _eventually_ be load. |
| 54 |
* Users can use this to interact with the results of a loaded Babel |
| 55 |
* configuration. |
| 56 |
* |
| 57 |
* Any changes to public properties of this class should be considered a |
| 58 |
* breaking change to Babel's API. |
| 59 |
*/ |
| 60 |
class ConfigItem { |
| 61 |
/** |
| 62 |
* The private underlying descriptor that Babel actually cares about. |
| 63 |
* If you access this, you are a bad person. |
| 64 |
*/ |
| 65 |
_descriptor: UnloadedDescriptor; |
| 66 |
|
| 67 |
// TODO(Babel 8): Check if this symbol needs to be updated |
| 68 |
/** |
| 69 |
* Used to detect ConfigItem instances from other Babel instances. |
| 70 |
*/ |
| 71 |
[CONFIG_ITEM_BRAND] = true; |
| 72 |
|
| 73 |
/** |
| 74 |
* The resolved value of the item itself. |
| 75 |
*/ |
| 76 |
value: {} | Function; |
| 77 |
|
| 78 |
/** |
| 79 |
* The options, if any, that were passed to the item. |
| 80 |
* Mutating this will lead to undefined behavior. |
| 81 |
* |
| 82 |
* "false" means that this item has been disabled. |
| 83 |
*/ |
| 84 |
options: {} | void | false; |
| 85 |
|
| 86 |
/** |
| 87 |
* The directory that the options for this item are relative to. |
| 88 |
*/ |
| 89 |
dirname: string; |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the name of the plugin, if the user gave it one. |
| 93 |
*/ |
| 94 |
name: string | void; |
| 95 |
|
| 96 |
/** |
| 97 |
* Data about the file that the item was loaded from, if Babel knows it. |
| 98 |
*/ |
| 99 |
file: { |
| 100 |
// The requested path, e.g. "@babel/env". |
| 101 |
request: string; |
| 102 |
// The resolved absolute path of the file. |
| 103 |
resolved: string; |
| 104 |
} | void; |
| 105 |
|
| 106 |
constructor(descriptor: UnloadedDescriptor) { |
| 107 |
// Make people less likely to stumble onto this if they are exploring |
| 108 |
// programmatically, and also make sure that if people happen to |
| 109 |
// pass the item through JSON.stringify, it doesn't show up. |
| 110 |
this._descriptor = descriptor; |
| 111 |
Object.defineProperty(this, "_descriptor", { enumerable: false }); |
| 112 |
|
| 113 |
Object.defineProperty(this, CONFIG_ITEM_BRAND, { enumerable: false }); |
| 114 |
|
| 115 |
this.value = this._descriptor.value; |
| 116 |
this.options = this._descriptor.options; |
| 117 |
this.dirname = this._descriptor.dirname; |
| 118 |
this.name = this._descriptor.name; |
| 119 |
this.file = this._descriptor.file |
| 120 |
? { |
| 121 |
request: this._descriptor.file.request, |
| 122 |
resolved: this._descriptor.file.resolved, |
| 123 |
} |
| 124 |
: undefined; |
| 125 |
|
| 126 |
// Freeze the object to make it clear that people shouldn't expect mutating |
| 127 |
// this object to do anything. A new item should be created if they want |
| 128 |
// to change something. |
| 129 |
Object.freeze(this); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
Object.freeze(ConfigItem.prototype); |