Monorepo学习笔记

Monorepo学习笔记

使用 pnpm 配置 monorepo

1、创建项目

shell 复制代码
mkdir stars-ui && cd stars-ui && pnpm init
mkdir packages docs

2、.gitignore

gitignore 复制代码
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

pnpm-lock.yaml

3、package.json

json 复制代码
{
  "name": "@xumeng03/monorepo",
  "private": "true",
  "description": "A Component Library for Vue 3",
  "scripts": {
    "preinstall": "npx only-allow pnpm",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "license": "ISC",
  "engines": {
    "node": ">=18"
  }
}

4、pnpm-workspace.yaml

yaml 复制代码
packages:
  - "packages/*"
  - "docs"
  - "play"

5、初始化

5.1、docs

shell 复制代码
pnpm init

docspackage.json如下

json 复制代码
{
  "name": "docs",
  "private": "true",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

5.2、packages

shell 复制代码
for i in components hooks stars-ui themes utils; do
    mkdir $i && cd $i
    pnpm init
    cd ..
done

componentshooksthemesutilspackage.json如下

json 复制代码
{
  "name": "@stars-ui/components",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

stars-uipackage.json如下

json 复制代码
{
  "name": "@xumeng03/stars-ui",
  "version": "1.0.0",
  "description": "A Component Library for Vue 3",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

5.3、play

shell 复制代码
# 根目录执行
pnpm create vite play -template vue-ts

playpackage.json如下

json 复制代码
{
  "name": "play",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc -b && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "vue": "^3.4.37"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^5.1.2",
    "typescript": "^5.5.3",
    "vite": "^5.4.1",
    "vue-tsc": "^2.0.29"
  }
}

6、依赖安装

6.1、根目录依赖

shell 复制代码
pnpm add -Dw vite
pnpm add -Dw typescript
shell 复制代码
pnpm add -w vue
pnpm add -w lodash-es

@xumeng03/stars-ui引入作为全局依赖

json 复制代码
{
  "name": "@xumeng03/monorepo",
  "private": "true",
  "description": "A Component Library for Vue 3",
  "scripts": {
    "preinstall": "npx only-allow pnpm",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "license": "ISC",
  "engines": {
    "node": ">=18"
  },
  "devDependencies": {
    "typescript": "^5.5.3",
    "vite": "^5.4.1"
  },
  "dependencies": {
    "lodash-es": "^4.17.21",
    "vue": "^3.4.37",
    "@xumeng03/stars-ui": "workspace:*"
  },
  "workspaces": [
    "packages/*",
    "play",
    "docs"
  ]
}

6.2、docs依赖

按需求添加,我这里目前没有依赖需要安装

6.3、package子包依赖安装

按需求添加,我这里只是在@xumeng03/stars-ui里面引入@stars-ui/components、@stars-ui/hooks、@stars-ui/themes、@stars-ui/utils

json 复制代码
{
  "name": "@xumeng03/stars-ui",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@stars-ui/components": "workspace:*",
    "@stars-ui/hooks": "workspace:*",
    "@stars-ui/themes": "workspace:*",
    "@stars-ui/utils": "workspace:*"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

6.4、play依赖

json 复制代码
{
  "name": "play",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc -b && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "vue": "^3.4.37"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^5.1.2",
    "typescript": "^5.5.3",
    "vite": "^5.4.1",
    "vue-tsc": "^2.0.29"
  }
}

6.5、安装依赖

shell 复制代码
pnpm install

6.6、测试Monorepo

shell 复制代码
cd play
pnpm run dev

7、发布

待补。。。

相关推荐
宁波阿成7 天前
pnpm install安装element-plus的版本跟package.json指定的版本不一样
json·pnpm·vue3·element-plus
乐闻x22 天前
如何使用 pnpm 进行打补丁patch操作?推荐两个方法
前端·javascript·pnpm·patch
艾小逗24 天前
pnpm报错 cannot find package xxx,有的电脑正常运行,只有这个的电脑报错
javascript·vue·pnpm
ziyue75751 个月前
pnpm install的时候失败提示python问题
开发语言·python·pnpm
潜心专研的小张同学1 个月前
pnpm依赖安装失败解决|pnpm项目从一个文件夹复制到另一个文件夹运行失败问题解决-以vbenAdmin项目为例
前端·javascript·vscode·npm·vue·pnpm
程序猿看视界1 个月前
npm、yarn、pnpm 最新国内镜像源设置和常见问题解决
npm·node.js·pnpm·yarn
程序猿看视界2 个月前
Halo 开发者指南——项目运行、构建
git·docker·node.js·pnpm·idea·openjdk·halo
唯之为之2 个月前
vue3项目部署到Github
vue·github·pnpm·vue3·vite
dralexsanderl2 个月前
通过 pnpm 安装依赖包会发生什么
npm·pnpm