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、发布

待补。。。

相关推荐
昔人'3 天前
`corepack` 安装pnpm
前端·pnpm·node·corepack
LYFlied9 天前
【一句话概括】前端项目包管理器怎么选?
前端·npm·pnpm·yarn
Irene199114 天前
解决 pnpm 构建脚本被阻止(Ignored build scripts)的问题
pnpm
lkbhua莱克瓦2414 天前
项目知识——Monorepo(单体仓库)架构详解
架构·github·项目·monorepo
LYFlied15 天前
幽灵依赖详解
npm·pnpm·打包工具·yarn·工程化·包管理工具·幽灵依赖
LYFlied15 天前
前端项目包管理器怎么选?
前端·面试·npm·pnpm·yarn·工程化·包管理器
F2E_Zhangmo15 天前
pnpm如何对node_modules打补丁
webpack·npm·pnpm
宁雨桥23 天前
使用pnpm构建高效Monorepo:从零到一的完整指南
前端·pnpm·项目架构
这是个栗子2 个月前
【问题解决】用pnpm创建的 Vue3项目找不到 .eslintrc.js文件 及 后续的eslint配置的解决办法
javascript·vue.js·pnpm·eslint
gs801402 个月前
pnpm + webpack + vue 项目依赖缺失错误排查与解决
pnpm·1024程序员节