Skip to content

Commit 8fa3101

Browse files
authored
Merge pull request JefferyHcool#311 from JefferyHcool/feature/optimize-build
fix(docker): 优化 Vite 配置以支持 Docker 构建环境
2 parents 1f52185 + 499366d commit 8fa3101

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

BillNote_frontend/vite.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
88

99
// https://vitejs.dev/config/
1010
export default defineConfig(({ mode }) => {
11-
const env = loadEnv(mode, process.cwd() + '/../')
11+
// 在 Docker 环境中,父目录可能没有 .env 文件,使用当前目录
12+
const envDir = process.env.DOCKER_BUILD ? __dirname : path.resolve(__dirname, '../')
13+
const env = loadEnv(mode, envDir)
1214

1315
const apiBaseUrl = env.VITE_API_BASE_URL || 'http://127.0.0.1:8483'
1416
const port = parseInt(env.VITE_FRONTEND_PORT || '3015', 10)

Dockerfile.complete

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ WORKDIR /tmp/frontend
3636
COPY ./BillNote_frontend/package.json ./
3737
RUN pnpm install
3838

39-
# 复制 .env.example 到父目录,供 vite.config.ts 使用
40-
COPY ./.env.example /tmp/.env
41-
4239
COPY ./BillNote_frontend /tmp/frontend
40+
41+
# 设置环境变量,告诉 vite.config.ts 这是 Docker 构建
42+
ENV DOCKER_BUILD=1
4343
RUN pnpm run build
4444

4545
# === 阶段3:完整应用镜像 ===

0 commit comments

Comments
 (0)