使纱线成为最佳JavaScript软件包管理器的6件事

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

  1. 速度
  2. 坚固的安装
  3. 许可证检查
  4. 与npm和Bower的兼容性
  5. 多个注册表
  6. 表情符号

1.速度

Yarn的声望之一是与标准npm客户相比的速度。 但是速度有多快? 在最近的基准测试中 ,Yarn比npm快两到三倍。 基准测试确定了React,Angular 2和Ember的安装时间。 对于软件包管理器来说,这是一个相当不错的测试,因为这些框架中的每个框架都拉出了一堆依赖关系,并且代表了真实世界Web应用程序依赖关系的主要部分。

让我们添加另一个数据点,并通过同时使用yarn和npm安装一个create-react-app进行检查。 这是使用yarn的安装:

$ 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

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

2.坚固的安装

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

  1. 瞬态网络问题可能会导致从注册表获取程序包失败。
  2. 新版本的软件包可能会导致不兼容和重大更改。

纱线解决了这两个问题。

离线缓存

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.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 licenses ls命令可以使Yarn变得非常容易。 它会生成一个紧凑的报告,其中包括完全合格的程序包名称,其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 licenses generate-disclaimer为您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支持。

主要原因是Bower支持效果不佳,清空了bower_components目录,或者没有在新项目中获取任何Bower软件包。 但是另一个原因是,Yarn团队不想在软件包管理领域鼓励分散化,而是每个人都希望改用npm。

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

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

5.多个注册表

纱线可以使用多种注册表类型。 默认情况下,如果仅添加软件包,它将使用其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. Emojis 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兼容,但是速度要快得多。 它对于安装不稳定的大型项目解决了严重的问题,它支持多种类型的注册表,并且具有可启动的表情符号。

如您所见,JavaScript尽管并非没有学习曲线,但它拥有大量的库和框架,可让您忙碌。 如果您正在寻找其他资源来学习或在工作中使用,请查看我们在Envato市场中可用的资源

JavaScript社区总体上非常积极,并且Yarn背后有很多动力。 它已经解决了一些问题,例如冗余的Bower支持和默认表情符号。 从npm迁移到Yarn非常容易。 我强烈建议您尝试一下。

翻译自: https://code.tutsplus.com/tutorials/6-things-that-make-yarn-the-best-javascript-package-manager--cms-29465

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值