PnPify
由于 Yarn 支持开箱即用的 node_modules
安装,因此 PnPify 已基本弃用。
¥PnPify is mostly deprecated since Yarn supports node_modules
installs out of the box.
动机
¥Motivation
Plug'n'Play 在设计上与所有仅使用 require
API 的项目兼容 - 无论是 require
、require.resolve
还是 createRequire
。但是,一些罕见的项目更喜欢自己重新实现 Node 解析,因此默认情况下与我们的环境不兼容(除非它们将其解析器与 PnP API 集成)。
¥Plug'n'Play is, by design, compatible with all projects that only make use of the require
API - whether it's require
, require.resolve
, or createRequire
. However, some rare projects prefer to reimplement the Node resolution themselves and as such aren't compatible by default with our environment (unless they integrate their resolvers with the PnP API).
PnPify
PnPify 是一种旨在解决这些兼容性问题的工具。它并不完美 - 它带来了自己的一些注意事项,并且不允许你利用 PnP 提供的所有功能 - 但它通常足以解除你的阻碍,直到实现更好的解决方案。
¥PnPify is a tool designed to work around these compatibility issues. It's not perfect - it brings its own set of caveats and doesn't allow you to leverage all the features that PnP has to offer - but it's often good enough to unblock you until better solutions are implemented.
它的工作原理很简单:当不符合 PnP 规范的项目尝试访问 node_modules
目录(例如通过 readdir
或 readFile
)时,PnPify 会拦截这些调用并将其转换为对 PnP API 的调用。然后,根据结果,它模拟虚拟 node_modules
文件夹的存在,然后底层工具将使用该文件夹 - 仍然不知道文件是从虚拟文件系统中提取的。
¥How it works is simple: when a non-PnP-compliant project tries to access the node_modules
directories (for example through readdir
or readFile
), PnPify intercepts those calls and converts them into calls to the PnP API. Then, based on the result, it simulates the existence of a virtual node_modules
folder that the underlying tool will then consume - still unaware that the files are extracted from a virtual filesystem.
用法
¥Usage
-
将 PnPify 添加到你的依赖中:
¥Add PnPify to your dependencies:
yarn add @yarnpkg/pnpify
-
使用 pnpify 运行不兼容的工具:
¥Use pnpify to run the incompatible tool:
yarn pnpify tsc
有关运行命令的更多详细信息,请参阅其 专用页面。
¥More details about the run command can be found on its dedicated page.
警告
¥Caveat
-
由于 PnPify 模拟
node_modules
目录的方式,因此可能会出现一些问题,尤其是使用监视node_modules
内目录的工具时。¥Due to how PnPify emulates the
node_modules
directory, some problems are to be expected, especially with tools that watch directories insidenode_modules
. -
PnPify 并非设计为长期解决方案;它的目的纯粹是为了帮助项目过渡到更严格的 Plug'n'Play 模块解析方案。依赖 PnPify 并不能让你充分利用 Plug'n'Play 提供的一切,尤其是完美的扁平化和边界检查。
¥PnPify isn't designed to be a long-term solution; its purpose is purely to help projects during their transition to the stricter Plug'n'Play module resolution scheme. Relying on PnPify doesn't allow you to take full advantage of everything Plug'n'Play has to offer, in particular perfect flattening and boundary checks.
IDE 支持
¥IDE Support
当使用 Plug'n'Play 安装与你最喜欢的文本编辑器时,你可能希望继续使用你的扩展,如 ESLint 或 Prettier。为此,你可能需要使用 yarn sdks
。有关更多信息,请参阅 编辑器 SDK 部分中的详细文档。
¥When using Plug'n'Play installs with your favorite text editors you will probably want to keep using your extensions, like ESLint or Prettier. To do so, you may need to use yarn sdks
. For more information, consult the detailed documentation in the Editor SDKs section.