我是要提交 yarn 的 `.pnp.js` 文件吗?

Am I meant to commit yarn's `.pnp.js` file?

Yarn 包含一个可选的 "Plug'n'Play" feature,它将 node_modules 移出项目目录。这样做时,它会创建一个 .pnp.js 文件,其中引用了硬盘驱动器上的各种依赖路径。

该文件是自动生成的,有几千行长,似乎引用了 特定于机器 运行 yarn install 的路径。

.pnp.js 是否要与其余代码一起提交?尽管该文件似乎无法提交,但我似乎找不到有关此的任何信息。什么是最佳实践?为什么?

我发现 this comment by Arcanis(Yarn 的主要维护者)在询问 .gitignore 中包含什么的问题。它可能会回答您的问题:

  • .yarn/plugins and .yarn/releases contain the Yarn releases used in the current repository (as defined by yarn set version). You will want to keep them versioned (this prevents potential issues if, say, two engineers use different Yarn versions with different features).

  • .yarn/unplugged should likely always be ignored, since it may contain native builds

  • .yarn/build-state.yml should likely be ignored as well, as it contains the build infos

    • If for some reason you version unplugged, it may make sense to keep build-state as well
  • .yarn/cache may be ignored, but you'll need to run yarn install to regenerate it

    • Versioning it unlocks what we call Zero-Installs - it's optional, though
  • .pnp.js (and potentially .pnp.data.json) are in the same boat as the cache. Add it to your repository if you keep your cache in your repository, and ignore it otherwise.

  • yarn.lock should always be stored within your repository (even if you develop a library)

So to summarize:

If you're using Zero-Installs:

.yarn/unplugged
.yarn/build-state.yml

If you're not using Zero-Installs:

.yarn/*
!.yarn/releases
!.yarn/plugins
.pnp.*

正如其中一位评论者所说,the official .gitignore recommendations are here,以及出色的评论。我唯一的批评是他们在那里展示它的方式,我认为他们应该将常见的方式合并为一个,然后将零安装与非零安装的特殊情况分开。这就是我的方式。但同样,请注意该文章,因为其中一些(例如 .yarn/sdk)可以选择添加或不添加。

# ------- yarn -------
# see excellent notes at: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
# Also: `yarn.lock` and `.yarnrc.yml` (or it's older counterpart .yarnrc) "should always be stored in your repo"
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

## --> ADD if using zero-install, otherwise do NOT:
#!.yarn/cache

## --> ELSE ADD if NOT using yarn's zero-install:
.pnp.*

# ------- end yarn -------