当前位置:网站首页>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
-F
Equivalent 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 .
边栏推荐
- QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
- Learning C language from scratch -- installation and configuration of 01 MinGW
- Jenkins learning (I) -- Jenkins installation
- Hudi 快速体验使用(含操作详细步骤及截图)
- ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
- Redis learning (I)
- LeetCode每日一题(1996. The Number of Weak Characters in the Game)
- Django operates Excel files through openpyxl to import data into the database in batches.
- Crawler career from scratch (IV): climb the bullet curtain of station B through API
- Idea uses the MVN command to package and report an error, which is not available
猜你喜欢
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Spark 结构化流写入Hudi 实践
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
2022-2-14 learning xiangniuke project - Session Management
Flink学习笔记(十一)Table API 和 SQL
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
Navicat, MySQL export Er graph, er graph
随机推荐
Powerdesign reverse wizard such as SQL and generates name and comment
Windows安装Redis详细步骤
LeetCode每日一题(2109. Adding Spaces to a String)
ERROR: certificate common name “*.” doesn’t match requested ho
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Win10安装ELK
LeetCode每日一题(1024. Video Stitching)
Go language - Reflection
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
Using Hudi in idea
Apply for domain name binding IP to open port 80 record
Run flash demo on ECS
Basic knowledge of database design
小王叔叔的博客目录【持续更新中】
图像修复方法研究综述----论文笔记
The server denied password root remote connection access
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Overview of image restoration methods -- paper notes
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
Usage of pandas to obtain MySQL data