yarnlink的用法与作用

当我们开发一个库的时候,不可能每次都去发布到npm或者私服,然后再去应用中安装。所以,无论npm或者 yarn都提供了link命令用以简化开发流程。

以yarn为例,首先进入到库的根目录中,执行yarn link,值得注意的时候,当前目录显然应该有package.json文件,且应包含发布的名称和入口文件。

此时相当于将当前包发布到了全局环境,这个时候,对应的应用就可以通过yarn link @scope/your-lib命令添加该包。其实仅仅是创建了一个软连接,即一个快捷方式。

至此就成功绕过了发包的流程,而且得益于仅仅是一个创建一个软连接,使得每次重新构建库之后,应用可以立即看到效果,从而实现快速的本地开发。

yarn官网原文 地址:https://yarn.bootcss.com/docs/cli/link/

Symlink a package folder during development.

For development, a package can be linked into another project. This is often useful to test out new features or when trying to debug an issue in a package that manifests itself in another project.

There are two commands to facilitate this workflow:

This command is run in the package folder you’d like to consume. For example if you are working on react and would like to use your local version to debug a problem in react-relay, simply run yarn link inside of the react project.

Use yarn link [package] to link another package that you’d like to test into your current project. To follow the above example, in the react-relay project, you’d run yarn link react to use your local version of react that you previously linked.

Complete example, assuming two project folders react and react-relay next to each other:

$ cd react
$ yarn link
yarn link vx.x.x
success Registered "react".
info You can now run `yarn link "react"` in the projects where you want to use this module and it will be used instead.
$ cd ../react-relay
$ yarn link react
yarn link vx.x.x
success Registered "react".

This will create a symlink named react-relay/node_modules/react that links to your local copy of the react project.

Links are registered in ~/.config/yarn/link. If you want to specify a different folder you can run the command with this syntax yarn link --link-folder path/to/dir/

posted @ 2022-03-01 15:08  deulist_p  阅读(930)  评论(0编辑  收藏  举报