• 日常搜索
  • 百度一下
  • Google
  • 在线工具
  • 搜转载

使Yarn成为最佳JavaScript包管理器的6大功能

Yarn 是 Facebook 开发的开源 npm 客户端,在标准 npm 客户端的许多方面进行了改进。在本教程中,我将重点介绍使 Yarn 很棒的6大功能:

  1. 速度

  2. 强大的安装

  3. 许可证检查

  4. 与 npm 和 Bower 的兼容性

  5. 多个注册表

  6. 表情符号

1.速度

与标准 npm 客户端相比,Yarn 声名鹊起的原因之一是它的速度。但它有多快?在最近的基准测试中,Yarn 比 npm 快两到三倍。该基准测试了reactangular 2 和 Ember 的安装时间。这对于包管理器来说是一个很好的测试,因为这些框架中的每一个都提取了一堆依赖项,并代表了现实世界 Web 应用程序依赖项的主要份额。  

让我们添加另一个数据点并通过使用 yarn 和 npm 安装 create-react-app 来检查自己。这是使用纱线的安装:

$ yarn global add create-react-app --prefix /usr/local
yarn global v0.27.5
warning package.json: No license field
warning No license field
[1/4] Resolving packages...
[2/4] fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "create-react-app@1.4.0" with binaries:
      - create-react-app
warning No license field
Done in 2.59s.

这是使用 npm 安装的:

$ npm install -g create-react-app
/usr/local/bin/create-react-app -> 
/usr/local/lib/node_modules/create-react-app/index.js
+ create-react-app@1.4.0
added 80 packages in 9.422s

是的。这无疑证实了其他关于纱线具有显着速度优势的报道。Yarn 安装时间为 2.59 秒,而 npm 安装时间为 9.422 秒。纱线快了 3.63 倍!

2.强大的安装

Yarn 还拥有比 npm 更强大的安装。是什么让安装变得不稳定?如果后续安装失败或产生不同的结果,则安装是不稳定的。有两个主要原因:

  1. 暂时的网络问题可能会导致从注册表中获取包失败。

  2. 软件包的新版本可能会导致不兼容和破坏性更改。

Yarn 解决了这两个问题。 

离线缓存

Yarn 使用全局离线缓存来存储您安装过的软件包,因此新安装使用缓存版本并避免由于间歇性网络故障而导致的不稳定。您可以通过键入以下内容找到纱线缓存的位置:

$ yarn cache dir
/Users/gigi.sayfan/Library/Caches/Yarn/v1

以下是我的离线缓存中的前五个包:

$ ls `yarn cache dir` | head -5
npm-@kadira
npm-@types
npm-base64-0.2.1-ba3a4230708e186705065e66babdd4c35cf60028
npm-JSONStream-0.8.4-91657dfe6ff857483066132b4618b62e8f4887bd
npm-abab-1.0.3-b81de5f7274ec4e756d797cd834f303642724e5d

Yarn 可以走得更远,拥有一个完整的离线镜像,可以在纱线本身的升级中工作。

yarn.lock 文件

每当您添加或升级版本时,都会更新 yarn.lock 文件。它本质上使用部分版本控制(例如,主要和次要)及其依赖项来确定可能在 package.json 中指定的每个包的确切版本。 

这是典型的 yarn.lock 文件的开头。您可以看到 package.json 中指定的版本,例如“abbrev@1”和固定版本“1.1.0”。

 cat yarn.lock | head -18
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


abab@^1.0.3:
  version "1.0.3"
  resolved "https://registry.yarnpkg.com/abab/-
  /abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d"

abbrev@1:
  version "1.1.0"
  resolved "https://registry.yarnpkg.com/abbrev/-
  /abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f"

accepts@~1.3.3:
  version "1.3.4"
  resolved "https://registry.yarnpkg.com/accepts/
  -/accepts-1.3.4.tgz#86246758c7dd6d21a6474ff084a4740ec05eb21f"
  dependencies:
    mime-types "~2.1.16"
    negotiator "0.6.1"

但为什么?

Yarn 还为您提供了yarn why解释为什么在您的项目中安装特定包的命令:

$ yarn why worker-farm
yarn why v0.27.5
[1/4] Why do we have the module "worker-farm"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
info This module exists because "react-scripts#jest#jest-cli" 
     depends on it.
info Disk size without dependencies: "132kB"
info Disk size with unique dependencies: "212kB"
info Disk size with transitive dependencies: "244kB"
info Number of shared dependencies: 2
Done in 1.38s.

3.许可证检查

有些项目需要遵守某些许可要求,或者只是为内部或外部目的生成报告。Yarn 使用该yarn licenses ls 命令非常容易。它会生成一个紧凑的报告,其中包括完全限定的包名称、其 URL 和许可证。这是一个例子:

$ yarn licenses ls | head -20
yarn licenses v0.27.5
├─ abab@1.0.3
│  ├─ License: ISC
│  └─ URL: git+https://github.com/jsdom/abab.git
├─ abbrev@1.1.0
│  ├─ License: ISC
│  └─ URL: https://github.com/isaacs/abbrev-js
├─ accepts@1.3.4
│  ├─ License: MIT
│  └─ URL: https://github.com/jshttp/accepts.git
├─ acorn-dynamic-import@2.0.2
│  ├─ License: MIT
│  └─ URL: https://github.com/kesne/acorn-dynamic-import
├─ acorn-globals@3.1.0
│  ├─ License: MIT
│  └─ URL: https://github.com/ForbesLindesay/acorn-globals.git
├─ acorn-jsx@3.0.1
│  ├─ License: MIT
│  └─ URL: https://github.com/RReverser/acorn-jsx

Yarn 甚至可以为您生成免责声明yarn licenses generate-disclaimer结果是一些带有免责声明消息的文本和应用程序中每个包的文本。以下是为我的测试项目生成的免责声明示例:

-----

The following software may be included in this product: 
utils-merge. A copy of the source code may be downloaded from 
git://github.com/jaredhanson/utils-merge.git. This software 
contains the following license and notice below:

(The MIT License)

Copyright (c) 2013 Jared Hanson

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without 
restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or 
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following 
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO event SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

-----

The following software may be included in this product: uuid. 
A copy of the source code may be downloaded from 
https://github.com/defunctzombie/node-uuid.git. This software 
contains the following license and notice below:

Copyright (c) 2010-2012 Robert Kieffer
MIT License - http://opensource.org/licenses/mit-license.php-----

4.与 npm 和 Bower 的兼容性

Yarn 与 npm 完全兼容,因为它只是一个与 npm 注册表一起使用的不同客户端。它很早就支持鲍尔,但在决定放弃鲍尔支持后不久。 

主要原因是 Bower 支持没有很好地工作并且清空了 bower_components 目录或者没有在新项目中获取任何 bower 包。但另一个原因是 Yarn 团队不想在包管理领域鼓励碎片化,而是希望每个人都切换到 npm。

如果你投资了 Bower 并且现在不想迁移,你仍然可以使用 Yarn,但将以下代码段添加到你的 package.json 文件中:

"scripts": {
    "postinstall": "bower install"
}

5.多个注册表

Yarn 可以与多种注册表类型一起使用。默认情况下,如果你只是添加一个包,它将使用它的 npm 注册表(它不是标准的 npm 注册表)。但它也可以从文件、远程 tarball 或远程 git 存储库中添加包。

要查看当前配置的 npm 注册表:

$ yarn config get registry
https://registry.yarnpkg.com

要设置不同的注册表类型: yarn config set registry <registry url>

要从不同位置添加包,请使用以下添加命令:

# Configured npm registry
yarn add <pkg-name>

# Local package
yarn add file:/<path to local package directory>

# Remote tarball
yarn add https://<path to compressed tarball>.tgz

# Remote git repo
yarn add <git remote-url>

6.表情符号FTW!

有些人喜欢表情符号,有些人不喜欢。Yarn 最初会自动显示表情符号,但仅在mac OS X 上。它在两个阵营中都引起了轰动:表情符号仇恨者对他们在 Mac OS X 上的控制台上堆满表情符号感到不满,而表情符号爱好者则对他们没有表情符号感到不满在 Windows 和 Linux 上。 

现在,表情符号默认不会在 macOS 上显示,您可以使用以下--emoji标志启用表情符号:

$ yarn install --emoji
yarn install v0.27.5
[1/4]   Resolving packages...
[2/4]   Fetching packages...
[3/4]   Linking dependencies...

结论

Yarn 是最好的javascript包管理器。它与 npm 兼容,但速度要快得多。它解决了安装不稳定的大型项目的严重问题,它支持多种类型的注册表,并且可以启动表情符号。 


文章目录
  • 1.速度
  • 2.强大的安装
    • 离线缓存
    • yarn.lock 文件
    • 但为什么?
  • 3.许可证检查
  • 4.与 npm 和 Bower 的兼容性
  • 5.多个注册表
  • 6.表情符号FTW!
  • 结论