當我們開發一個庫的時候,不可能每次都去發布到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:
yarn link (in package you want to link)
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.
yarn link [package...]
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/
