1. 程式人生 > 其它 >yarnlink的用法與作用

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 onreactand would like to use your local version to debug a problem inreact-relay, simply runyarn linkinside of thereactproject.

Useyarn link [package]

to link another package that you’d like to test into your current project. To follow the above example, in thereact-relayproject, you’d runyarn link reactto use your local version ofreactthat you previously linked.

Complete example, assuming two project foldersreactandreact-relaynext 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 namedreact-relay/node_modules/reactthat links to your local copy of thereactproject.

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