PnP API
概述
¥Overview
在 Plug'n'Play 运行时环境中运行的每个脚本都可以访问一个特殊的内置模块 (pnpapi
),该模块允许你在运行时自省依赖树。
¥Every script running within a Plug'n'Play runtime environment has access to a special builtin module (pnpapi
) that allows you to introspect the dependency tree at runtime.
数据结构
¥Data Structures
PackageLocator
export type PackageLocator = {
name: string,
reference: string,
};
包定位器是描述依赖树中包的一个唯一实例的对象。name
字段保证是包本身的名称,但 reference
字段应被视为不透明字符串,其值可能是 PnP 实现决定放在那里的任何值。
¥A package locator is an object describing one unique instance of a package in the dependency tree. The name
field is guaranteed to be the name of the package itself, but the reference
field should be considered an opaque string whose value may be whatever the PnP implementation decides to put there.
请注意,一个包定位器与其他包定位器不同:顶层定位器(可通过 pnp.topLevel
获得,参见下文)将 name
和 reference
都设置为 null
。此特殊定位器将始终镜像顶层包(通常是存储库的根,即使在使用工作区时也是如此)。
¥Note that one package locator is different from the others: the top-level locator (available through pnp.topLevel
, cf below) sets both name
and reference
to null
. This special locator will always mirror the top-level package (which is generally the root of the repository, even when working with workspaces).
PackageInformation
export type PackageInformation = {
packageLocation: string,
packageDependencies: Map<string, null | string | [string, string]>,
packagePeers: Set<string>,
linkType: 'HARD' | 'SOFT',
};
软件包信息集描述了可以在磁盘上找到软件包的位置,以及允许它需要的确切依赖集。packageDependencies
值应解释为:
¥The package information set describes the location where the package can be found on the disk, and the exact set of dependencies it is allowed to require. The packageDependencies
values are meant to be interpreted as such:
-
如果是字符串,则该值应用作定位器中的引用,其名称是依赖名称。
¥If a string, the value is meant to be used as a reference in a locator whose name is the dependency name.
-
如果是
[string, string]
元组,则该值应用作定位器,其名称是元组的第一个元素,引用是第二个元素。这通常发生在包别名(例如"foo": "npm:bar@1.2.3"
)中。¥If a
[string, string]
tuple, the value is meant to be used as a locator whose name is the first element of the tuple and reference is the second one. This typically occurs with package aliases (such as"foo": "npm:bar@1.2.3"
). -
如果是
null
,则指定的依赖根本不可用。这通常发生在依赖树中包的对等依赖未由其直接父级提供时。¥If
null
, the specified dependency isn't available at all. This typically occurs when a package's peer dependency didn't get provided by its direct parent in the dependency tree.
如果存在 packagePeers
字段,则表示哪些依赖具有强制契约,即使用与依赖它们的包完全相同的实例。此字段在纯 PnP 上下文中很少有用(因为我们的实例化保证比这更严格、更可预测),但需要从 PnP 映射正确生成 node_modules
目录。
¥The packagePeers
field, if present, indicates which dependencies have an enforced contract on using the exact same instance as the package that depends on them. This field is rarely useful in pure PnP context (because our instantiation guarantees are stricter and more predictable than this), but is required to properly generate a node_modules
directory from a PnP map.
linkType
字段仅在特定情况下有用 - 它描述了是否要求 PnP API 的制作者通过硬链接(在这种情况下,所有 packageLocation
字段都被认为归链接器所有)或软链接(在这种情况下,packageLocation
字段代表链接器影响范围之外的位置)提供软件包。
¥The linkType
field is only useful in specific cases - it describes whether the producer of the PnP API was asked to make the package available through a hard linkage (in which case all the packageLocation
field is reputed being owned by the linker) or a soft linkage (in which case the packageLocation
field represents a location outside of the sphere of influence of the linker).
运 行时常量
¥Runtime Constants
process.versions.pnp
在 PnP 环境下操作时,此值将设置为一个数字,表示正在使用的 PnP 标准的版本(与 require('pnpapi').VERSIONS.std
完全相同)。
¥When operating under PnP environments, this value will be set to a number indicating the version of the PnP standard in use (which is strictly identical to require('pnpapi').VERSIONS.std
).
此值是一种方便的方法,用于检查你是否在 Plug'n'Play 环境(你可以在其中 require('pnpapi')
)下操作:
¥This value is a convenient way to check whether you're operating under a Plug'n'Play environment (where you can require('pnpapi')
) or not:
if (process.versions.pnp) {
// do something with the PnP API ...
} else {
// fallback
}