当前位置: 首页 > 文档资料 > Yarn 中文文档 >

pnp 概述

优质
小牛编辑
140浏览
2023-12-01

Plug’n’Play is an alternative installation strategy unveiled in September 2018. It presents interesting characteristics that make suitable for a large panel of projects, and is designed for compatibility with the current ecosystem.

The way regular installs work is simple: Yarn generates a node_modules directory that Node is then able to consume. In this context, Node doesn’t know the first thing about what a package is: it only reasons in terms of files. “Does this file exist here? No? Let’s look in the parent node_modules then. Does it exist here? Still no? Too bad… parent folder it is!” - and it does this until it matches something that matches one of the possibilities. That’s vastly inefficient.

When you think about it, Yarn knows everything about your dependency tree - it evens installs it! So why is Node tasked with locating your packages on the disk? Why don’t we simply query Yarn, and let it tell us where to look for a package X required by a package Y? That’s what Plug’n’Play (abbreviated PnP) is. Instead of generating a node_modules directory and leaving the resolution to Node, we now generate a single .pnp.js file and let Yarn tell us where to find our packages. Doing this provides a lot of benefits:

  • The node_modules directory contains a gargantuan amount of files. Generating it makes up for more than 70% of the time needed to run yarn install with a hot cache. Because the copy is I/O bound, it’s not like package managers can really optimize it either - we can use hardlinks or copy-on-write, but even then we still need to make a bunch of syscalls that slow us down dramatically.

  • Because Node has no concept of “package”, it doesn’t know whether a file is meant to be accessed, on top of being available. It’s entirely possible that code you wrote will work in development but break in production because you forgot to list one of your dependencies in your package.json - and you won’t know it until it becomes a problem and you lose a day investigating the issue.

  • Even at runtime, the Node resolution needs to make a bunch of stat and readdir calls in order to figure out where should a resolution end up. It’s extremely wasteful, and is part of the reason why booting a Node application takes so much time - before even starting executing it, Node has to spend its time querying the filesystem for information that Yarn could have given it already.

  • Finally, the very design of the node_modules folder is impractical in that it doesn’t allow to dedupe packages as efficiently as one would hope. Because two packages with the same name but different versions cannot coexist in the same directory, we can’t guarantee a perfect hoisting. Similarly, because the node_modules are deeply nested in a way that depend on the project dependencies, they cannot be shared from one project to the other.

All those problems and more are solved by Plug’n’Play.

最后更新:

类似资料

  • 概述 国际化和本地化的目的就是让一个网站应用能做到根据用户语种和指定格式的不同而提供不同的内容。 Django 对文本翻译, 日期、时间和数字的格式化,以及时区提供了完善的支持。 实际上,Django做了两件事: 由开发者和模板作者指定应用的哪些部分应该翻译,或是根据本地语种和文化进行相应的格式化。 根据用户的偏好设置,使用钩子将web应用本地化。 很显然,翻译取决于用户所选语言,而格式化通常取决

  • 虽然Django 满满的便捷性让Web 开发人员活得轻松一些,但是如果不能轻松地部署你的网站,这些工具还是没有什么用处。Django 起初,易于部署就是一个主要的目标。有许多优秀的方法可以轻松地来部署Django: 如何使用WSGI 部署 部署的检查清单 FastCGI 的支持已经废弃并将在Django 1.9 中删除。 如何使用FastCGI、SCGI 和AJP 部署Django 如果你是部署D

  • 本书概述 进程的概念大家都很熟悉,但你是否能准确说出僵尸进程的含义呢?还有 COW(Copy On Write)、Flock(File Lock)、Epoll 和 Namespace 的概念又是否了解过呢? 本书汇集了进程方方面面的基础知识,加上编程实例,保证阅读后能自如地回答以上问题,在项目开发中对进程的优化也有更深的理解。 本书架构 本书按循序渐进的方式介绍进程的基础概念和拓展知识,主要涵盖以

  • Docker在1.12.0和以上版本中增加了swarm模式。通过swarm模式管理的Docker Engines集群称之为“Swarm”。并可以通过Docker CLI来创建Swarm,在Swarm中部署应用服务,以及管理Swarm。 功能特点 在Docker Engines中集成了集群管理功能:通过Docker CLI可以创建一个用来发布应用services的swarm,而不需要安装其他任何额外

  • 这是 Elasticsearch 官方的 PHP 客户端。我们把 Elasticsearch-PHP 设计成低级客户端(低级设计模式),使用时不会偏离 REST API 的用法。 客户端所有方法几乎都与 REST API 对应,而且也与其他编程语言的客户端(如 ruby, python 等)方法结构相似。我们希望这种对应方式可以方便开发者更加容易上手客户端,且以最小的代价快速从一种编程语言转换到另

  • 特点 为什么用 Groovy?