Yarn4.x版本的安装依赖逻辑和pnpm类似,同时,安装速度也与pnpm不相上下,喜欢使用yarn的可以升级一下
与1.22.xx的区别
配置项使用.yarnrc.yml
如果您依赖 .npmrc or .yarnrc 文件,可能会导致问题。
Yarn Modern 现在使用 .yarnrc.yml .现在将忽略任何其他文件 - 这包括 .npmrc
大多数配置键也已重命名,以便更加一致。可用设置的完整列表可以在 .yarnrc.yml 专用文档中找到,但以下是一些重要的设置:
- Custom registries are now configured via
npmRegistryServer.
自定义注册表现在通过npmRegistryServer进行配置。 - Registry authentication tokens are now configured via
npmAuthToken.
注册表身份验证令牌现在通过npmAuthToken进行配置。
使用 yarn dlx 代替 yarn global
Cli命令更改
详细见链接 yarnpkg.com/migration/g…
为什么 npm 上的 yarn 包还在 1.x 上?
Modern releases of Yarn haven't been distributed on npm since 2019.
自 2019 年以来,Yarn 的现代版本就没有在 npm 上分发过。
The reason is simple: because Yarn wasn't distributed alongside Node.js, many people relied on something like npm install -g yarn as part of their image building. It meant that any breaking change would make their way on everyone using this pattern, and break their deployments.
原因很简单:因为 Yarn 没有与 Node.js 一起分发,所以许多人依赖类似 npm install -g yarn 的东西作为他们形象建设的一部分。这意味着任何重大更改都会影响到使用此模式的每个人,并破坏他们的部署。
As a result, we decided to retire the yarn npm package and only use it for the few 1.x maintenance releases needed. Yarn is now installed directly from our website, via either Corepack or yarn set version.
因此,我们决定停用 yarn npm 包,仅将其用于所需的少数 1.x 维护版本。Yarn 现在可以通过 Corepack 或 yarn set version
哪些文件应该被 gitignored?
If you're using Zero-Installs:
如果您使用的是零安装:
.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
If you're not using Zero-Installs:
如果您未使用零安装:
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
If you're interested to know more about each of these files:
如果您有兴趣了解有关这些文件的详细信息:
.yarn/cacheand.pnp.*may be safely ignored, but you'll need to runyarn installto regenerate them between each branch switch - which would be optional otherwise, cf Zero-Installs.
.yarn/cache并且.pnp.*可能会被安全地忽略,但您需要运行yarn install以在每个分支交换机之间重新生成它们 - 否则这将是可选的,参见零安装。.yarn/install-state.gzis an optimization file that you shouldn't ever have to commit. It simply stores the exact state of your project so that the next commands can boot without having to resolve your workspaces all over again.
.yarn/install-state.gz是一个优化文件,你永远不必提交。它只是存储项目的确切状态,以便下一个命令可以启动,而无需重新解析工作区。.yarn/patchescontain the patchfiles you've been generating with theyarnpatch-commitcommand. You always want them in your repository, since they are necessary to install your dependencies.
.yarn/patches包含使用该yarn patch-commit命令生成的补丁文件。您始终希望它们位于存储库中,因为它们是安装依赖项所必需的。.yarn/pluginsand.yarn/releasescontain the Yarn releases used in the current repository (as defined byyarnset version). You will want to keep them versioned (this prevents potential issues if, say, two engineers use different Yarn versions with different features).
.yarn/plugins并.yarn/releases包含当前存储库中使用的 Yarn 版本(定义为yarn set version)。您需要对它们进行版本控制(例如,如果两个工程师使用具有不同功能的不同 Yarn 版本,这样可以防止出现潜在问题)。.yarn/sdkscontains the editor SDKs generated by@yarnpkg/sdks. Whether to keep it in your repository or not is up to you; if you don't, you'll need to follow the editor procedure again on new clones. See Editor SDKs for more details.
.yarn/sdks包含 生成@yarnpkg/sdks的编辑器 SDK。是否将其保留在存储库中取决于您;否则,则需要在新克隆上再次执行编辑器过程。有关详细信息,请参阅编辑器 SDK。.yarn/unpluggedshould likely always be ignored since they typically hold machine-specific build artifacts. Ignoring it might however prevent Zero-Installs from working (to prevent this, setenableScriptstofalse).
.yarn/unplugged可能总是被忽略,因为它们通常包含特定于计算机的生成工件。但是,忽略它可能会阻止零安装工作(要防止这种情况,请设置为enableScriptsfalse)。.yarn/versionsis used by the version plugin to store the package release definitions. You will want to keep it within your repository.
.yarn/versions由版本插件用于存储包发布定义。您需要将其保留在存储库中。yarn.lockshould always be stored within your repository (even if you develop a library).
yarn.lock应该始终存储在您的存储库中(即使您开发了一个库)。.yarnrc.yml(and its older counterpart,.yarnrc) are configuration files. They should always be stored in your project.
.yarnrc.yml(及其较旧的对应项.yarnrc)是配置文件。它们应始终存储在项目中。
Tip: You can also add a
.gitattributesfile to identify the release and plugin bundles as binary content. This way Git won't bother showing massive diffs when each time you subsequently add or update them:
提示:您还可以添加文件.gitattributes以将版本和插件捆绑包标识为二进制内容。这样一来,Git 就不会在每次添加或更新它们时显示大量差异:/.yarn/releases/** binary /.yarn/plugins/** binary
包管理器的管理器corepack
Environment Variables 环境变量
COREPACK_NPM_REGISTRYsets the registry base url used when retrieving package managers from npm. Default value ishttps://registry.npmjs.org
COREPACK_NPM_REGISTRY设置从 NPM 检索包管理器时使用的注册表基 URL。默认值为https://registry.npmjs.org
Offline Workflow 离线工作流程
The utility commands detailed in the next section.
下一节中详细介绍了实用程序命令。
-
Either you can use the network while building your container image, in which case you'll simply run
corepack packto make sure that your image includes the Last Known Good release for the specified package manager.
您可以在构建容器映像时使用网络,在这种情况下,只需运行corepack pack以确保映像包含指定包管理器的“上次已知良好”版本即可。 -
Or you're publishing your project to a system where the network is unavailable, in which case you'll preemptively generate a package manager archive from your local computer (using
corepack pack -o) before storing it somewhere your container will be able to access (for example within your repository). After that it'll just be a matter of runningcorepack install -g --cache-only <path/to/corepack.tgz>to setup the cache.
或者,你正在将项目发布到网络不可用的系统,在这种情况下,你将抢先从本地计算机(使用corepack pack -o)生成包管理器存档,然后再将其存储在容器能够访问的位置(例如,在存储库中)。之后,只需运行corepack install -g --cache-only <path/to/corepack.tgz>即可设置缓存。corepack enable
在终端中运行下列指令安装最新版的yarn。
$ corepack prepare yarn@stable --activate
# 安装完成后检查版本
$ yarn -v
接下来,我们使用yarn2去管理项目和依赖:
-
创建新项目:
yarn init -2 -
迁移现有项目:
yarn set version stable,然后运行yarn install更新依赖树-
在现有项目中启用Pnp(这可能导致兼容性问题)
- 先运行
yarn dlx @yarnpkg/doctor来检测当前已经安装的依赖是否会产生兼容性问题
- 先运行
-
需要注意的是,默认情况下yarn init仍是1.x的模式,除非你指定yarn版本为2(如 yarn init -2)才会启用Zero-Installation特性。
前端工具链之包管理器 - yarn-berry与PnP
package.json中添加
"packageManager": "yarn@4.1.1"
.yarnrc.yml
yarnPath: .yarn/releases/yarn-4.1.1.cjs
遗留问题,如何在内网情况下使用yarn4版本
测试打包构建出了 yarn.js,使用node yarn.js 貌似能使用yarn命令,但不确定还有没有更好的办法,如果有大佬知道的话,可以评论区留言。