当前位置:网站首页>Starting from 0, use pnpm to build a demo managed by monorepo
Starting from 0, use pnpm to build a demo managed by monorepo
2022-07-03 09:32:00 【Technology stuff】
Write it at the front
Monorepo You should have heard this word more than once , image Vue3、Vite、ElementPlus And other excellent open source projects Monorepo Managing projects in a different way , And the projects mentioned here all adopt pnpm As a package management tool .
This article uses pnpm Build a simple one Monorepo Projects managed in a way .
What is? Monorepo? What is? pnpm?
Q: What is? Monorepo?
A:Monorepo It's a way of project management , Is to put multiple projects in one warehouse , You can refer to an article by the God three yuan big brother : Why is modern front-end engineering increasingly inseparable from Monorepo?, This article introduces Monorepo The concept of 、 Revenue and MulitRepo The disadvantages of .
Q: What is? pnpm?
A:pnpm It's a package management tool , Native support Monorepo, Than npm and yarn Faster , Others can refer to Official website And another article of God's three yuan boss : Why do I recommend pnpm instead of npm/yarn?
To get a Monorepo Of demo play
Now let's start using pnpm To build a Monorepo, Before the business begins , You need to make sure that your computer has Node.js, My version is 16.9.0.
First of all, you need to have pnpm This tool , You can find a way to install it from the official website , Or use it directly npm install , The order is as follows :
npm i pnpm -g
Now we start making things .
First step , Create a project root directory , It's called monorepo-demo, How to create , Here's the command :
mkdir monorepo-demo
The second step , initialization package.json, There's nothing to that , The order is as follows :
pnpm init
Here I made a little change to the content ,package.json Is as follows :
{
"name": "monorepo-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"keywords": [],
"author": "ywanzhou",
"license": "MIT"
}
Here I mainly add one type Field , Here I use ESModule Modular specification .
The third step , establish pnpm-workspace.yaml file , This file defines the root directory of the workspace , The contents are as follows :
packages:
- 'packages/ **'
Now we can packages Create multiple projects in , The directory structure is as follows :
monorepo-demo
├── package.json
├── packages
│ ├── components
│ │ ├── index.js
│ │ └── package.json
│ ├── core
│ │ ├── index.js
│ │ └── package.json
│ ├── utils
│ │ ├── index.js
│ │ └── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml
Step four , Write for each project package.json, In fact, the main thing is to write the name , Convenient for future use , Here I have the following :
{
"name": "@packages/components",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"type": "module",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.21",
"@packages/utils": "workspace:^1.0.0"
}
}
The remaining two names are @packages/core and @packages/utils.
In fact, the basic work will be ready , Now let's talk about how to install dependencies , How to be in packageA I quote packageB.
Installation dependency
Just this demo Come on , If we install dependencies in the root directory , This dependency can be found in all packages Use in , If we need a specific package How to install dependencies ?
cd To package The directory of the ? leak , Big leak , We can use the following command :
pnpm --filter <package_selector> <command>
For example, we need to be in @packages/components install lodash, The order is as follows :
pnpm -F @packages/components add lodash
-FEquivalent to--filter
Now we are going @packages/utils Install a dayjs, The order is as follows :
pnpm --filter @packages/utils add dayjs
packageA I quote packageB
Now let's do it package Mutual reference between , First of all we have @packages/utils/index.js Write the following in :
import dayjs from 'dayjs'
export function format(time, f = 'YYYY-MM-DD') {
return dayjs(time).format(f)
}
Then we execute the following command :
pnpm -F @packages/components add @packages/[email protected]*
This command is expressed in @packages/components install @packages/utils, Among them @* Indicates that the latest version is synchronized by default , Save the problem of having to synchronize the latest version every time .
After installation @packages/components/package.json The contents are as follows :
{
"name": "@packages/components",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"type": "module",
"license": "ISC",
"dependencies": {
"@packages/utils": "workspace: *",
"lodash": "^4.17.21"
}
}
And then we were in @packages/components/index.js Write the following :
import { format } from '@packages/utils'
console.log(format(new Date()))
Then we run the following command in the project root directory
node packages/components
You can print out the current date .
边栏推荐
- Go language - JSON processing
- Send mail using WP mail SMTP plug-in
- Long类型的相等判断
- Spark 概述
- Alibaba cloud notes for the first time
- PolyWorks script development learning notes (4) - data import and alignment using file import
- Spark 集群安装与部署
- PolyWorks script development learning notes (I) - script development environment
- Hudi 数据管理和存储概述
- 2022-2-13 learn the imitation Niuke project - Project debugging skills
猜你喜欢
![[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature](/img/66/2e7668cfed1ef4ddad26deed44a33a.png)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature

Crawler career from scratch (IV): climb the bullet curtain of station B through API

Beego learning - Tencent cloud upload pictures
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

Common software open source protocols

npm install安装依赖包报错解决方法

Spark cluster installation and deployment

Arduino handles JSON data, arduinojson assistant

Go language - Reflection
随机推荐
Utilisation de hudi dans idea
LeetCode每日一题(1024. Video Stitching)
LeetCode每日一题(1856. Maximum Subarray Min-Product)
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Go language - Reflection
Flask+supervisor installation realizes background process resident
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
C language programming specification
Numerical analysis notes (I): equation root
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
Simple use of MATLAB
There is no open in default browser option in the right click of the vscade editor
Redis learning (I)
unbuntu(debian)下TFTP服务器搭建及测试
Windows安装Redis详细步骤
Beego learning - JWT realizes user login and registration
Hudi quick experience (including detailed operation steps and screenshots)
Jenkins learning (I) -- Jenkins installation
2022-2-14 learning xiangniuke project - generate verification code