项目构建与部署
本文档详细说明 Widiazine SCRM 前端项目的构建和部署流程。
📋 目录
🚀 快速开始
项目使用 pnpm 作为包管理器,Vite 8 作为构建工具。
# 安装依赖
pnpm install
# 开发环境启动
pnpm dev
# 生产环境构建
pnpm build开发命令
基础开发命令
| 命令 | 说明 |
|---|---|
pnpm dev | 启动开发服务器 |
pnpm dev:turbo | 清除缓存后启动开发服务器 |
pnpm reinstall | 清除依赖后重新安装 |
开发服务器配置
开发服务器默认配置:
- 端口:由
VITE_PORT环境变量指定 - 代理:由
VITE_PROXY环境变量指定 - 主机:启用
host和allowedHosts,支持局域网访问
构建命令
生产构建
# 标准构建(推荐用于生产环境)
pnpm build说明:
- 使用
cross-env设置环境变量 NODE_ENV=production设置生产模式NODE_OPTIONS=--max-old-space-size=8192设置 Node.js 内存限制为 8GB- 使用
vp build(vite-plus 的构建命令)
快速构建
# 快速构建(适合本地测试)
pnpm build:fast说明:
- 直接使用
vp build --mode production - 不设置额外的环境变量
- 启动速度更快,但可能在大项目时内存不足
构建分析
# 构建并生成分析报告
pnpm build:analyze
# 或
pnpm build:report说明:
REPORT=true启用打包分析- 分析报告生成在
./node_modules/.cache/visualizer/stats.html - 自动在浏览器中打开报告
缓存清理
# 清理所有缓存
pnpm clean:cache
# 清理 Vite 缓存
pnpm clean:vite
# 清理 dist 目录
pnpm clean:dist
# 完全清理(包括 node_modules)
pnpm clean:all预览构建结果
使用项目命令预览(推荐)
# 构建并预览
pnpm preview
# 直接预览已有的 dist 目录
pnpm preview:dist说明:
preview会先执行构建,然后预览preview:dist直接预览dist目录- 使用
vp preview(vite-plus 的预览命令)
不能直接打开构建后的 html 文件
必须通过 HTTP 服务器访问,否则会出现资源加载错误。
使用 live-server 预览
# 1. 全局安装 live-server
pnpm add -g live-server
# 2. 进入打包目录
cd ./dist
# 3. 启动预览服务器
live-server # 默认端口 8080
live-server --port 9000 # 指定端口代码分析
打包体积分析
# 生成分析报告
pnpm build:report报告特性:
- 使用
rollup-plugin-visualizer生成可视化报告 - 自动在浏览器中打开
- 支持切换显示 gzip 或 brotli 压缩体积
分析报告包含:
- 模块依赖图
- 体积统计
- 压缩比例
- 重复代码检测
环境变量配置
环境文件说明
项目包含以下环境配置文件:
| 文件 | 说明 | 是否提交 Git |
|---|---|---|
.env | 本地环境变量(个人配置) | ❌ |
.env.development | 开发环境配置 | ❌ |
.env.proddev | 预发布环境配置 | ❌ |
.env.production | 生产环境配置 | ✅ |
环境变量分类
# ========================================
# 环境变量命名规范
# ========================================
# VITE_* : 构建时注入配置(敏感、稳定)
# VITE_GLOB_* : 运行时动态配置(不敏感、易变)生产环境配置示例
# .env.production
# ----------------------------------------
# 构建时配置(固定值)
# ----------------------------------------
VITE_PUBLIC_PATH=/admin
VITE_BUILD_COMPRESS='gzip'
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE=false
# ----------------------------------------
# 构建时配置(CNB 替换敏感信息)
# ----------------------------------------
VITE_BASIC_KEY=
VITE_TINYPNG_API_KEY=
# ----------------------------------------
# 运行时配置(K8s ConfigMap 注入)
# ----------------------------------------
VITE_GLOB_APP_TITLE=Widiazine SCRM
VITE_GLOB_APP_SHORT_NAME=widiazine_scrm
VITE_GLOB_API_URL=
VITE_GLOB_API_URL_PREFIX=
# ... 其他运行时配置部署路径配置
# 根据部署路径配置 PUBLIC_PATH
# 注意:需要以 / 开头和结尾
# 部署到根目录
VITE_PUBLIC_PATH=/
# 部署到子目录
VITE_PUBLIC_PATH=/admin/
VITE_PUBLIC_PATH=/xxx/配置路径说明
- 根目录部署:
VITE_PUBLIC_PATH=/ - 子目录部署:
VITE_PUBLIC_PATH=/admin/ - 修改后需要重新构建
Nginx 部署
基础部署配置
server {
listen 80;
server_name your-domain.com;
# 静态文件目录
root /srv/www/project;
index index.html index.htm;
location / {
# 不缓存 HTML,防止更新后缓存继续生效
if ( ~* .*\.(?:htm|html)$) {
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
access_log on;
}
# 用于配合 browserHistory 使用
try_files / /index.html;
}
}带 API 代理的部署
upstream backend {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name your-domain.com;
root /srv/www/project;
index index.html index.htm;
# 前端路由
location / {
try_files / /index.html;
}
# API 代理
location /api-cloud/ {
proxy_pass http://backend/;
proxy_redirect off;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
proxy_set_header X-Forwarded-Proto ;
}
}子目录部署
# 1. 修改 .env.production
VITE_PUBLIC_PATH=/admin/server {
listen 80;
server_name your-domain.com;
location /admin/ {
alias /srv/www/project/;
index index.html index.htm;
try_files / /admin/index.html;
}
}路由模式配置
Vue Router 模式
项目使用 vue-router,支持两种模式:
| 模式 | URL 格式 | 服务器配置 |
|---|---|---|
createWebHashHistory() | https://example.com/#/user | ❌ 不需要 |
createWebHistory() | https://example.com/user | ✅ 需要 |
在 src/router/index.ts 中配置:
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
const router = createRouter({
// Hash 模式(默认)
history: createWebHashHistory(),
// 或 History 模式
// history: createWebHistory(),
});History 模式服务器配置
部署到根目录:
server {
listen 80;
location / {
try_files / /index.html;
}
}部署到子目录:
# 1. 修改 .env.production
VITE_PUBLIC_PATH=/admin/server {
listen 80;
location /admin/ {
alias /srv/www/project/;
try_files / /admin/index.html;
}
}构建压缩
Gzip 压缩
# 在 .env.production 中配置
VITE_BUILD_COMPRESS='gzip'构建时会生成对应的 .gz 文件。
Brotli 压缩
# 在 .env.production 中配置
VITE_BUILD_COMPRESS='brotli'构建时会生成对应的 .br 文件。
同时启用 Gzip 和 Brotli
# 在 .env.production 中配置
VITE_BUILD_COMPRESS='gzip,brotli'同时生成 .gz 和 .br 文件。
Nginx 压缩配置
http {
# ===== Gzip 配置 =====
gzip on;
gzip_static on; # 使用预压缩的 .gz 文件
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain
application/javascript
application/x-javascript
text/css
application/xml
text/javascript
application/javascript
image/jpeg
image/gif
image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
# ===== Brotli 配置 =====
# 需要安装 ngx_brotli 模块
brotli on;
brotli_comp_level 6;
brotli_buffers 16 8k;
brotli_min_length 20;
brotli_types text/plain
text/css
application/json
application/x-javascript
text/xml
application/xml
application/xml+rss
text/javascript
application/javascript
image/svg+xml;
}构建产物
构建成功后会在根目录生成 dist 文件夹:
dist/
├── _app.config.js # 运行时配置文件
├── assets/ # 静态资源
│ ├── *.js # JavaScript 文件
│ ├── *.css # 样式文件
│ ├── *.png # 图片
│ └── ...
├── favicon.ico # 网站图标
└── index.html # 入口 HTML常见问题
Q: 构建时内存溢出
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory解决方案:
# 使用标准构建(已设置内存限制)
pnpm build
# 或手动设置内存
NODE_OPTIONS="--max-old-space-size=8192" pnpm build:fastQ: 资源路径 404
检查配置:
- 确认
VITE_PUBLIC_PATH配置正确 - 确认 Nginx
root或alias路径正确 - 检查是否使用了 History 模式但未配置
try_files
Q: API 请求跨域
解决方案:
- 开发环境:配置
VITE_PROXY环境变量 - 生产环境:使用 Nginx 反向代理
Q: 构建产物过大
排查步骤:
# 1. 生成分析报告
pnpm build:report
# 2. 检查是否有未使用的大型依赖
# 3. 考虑使用 CDN 加载大型库
# 4. 检查是否有重复打包的代码相关文档
页面信息›
别名构建部署打包部署