From ebd4694aa69a44e1f51fc3eae19e2cfa4d853821 Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Wed, 17 Sep 2025 12:12:50 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E6=9B=B4=E6=96=B0=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=92=8CDockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将Node.js最低版本要求从22降至20以兼容更多环境 - 在vite配置中添加外部依赖解决构建问题 - 重构Dockerfile,优化构建流程并修复依赖安装问题 - 更新nginx配置和容器启动命令 --- Dockerfile | 38 +++++++++++++++++++++++++++----------- package.json | 2 +- vite.config.ts | 5 +++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0cf05c7..07a7497 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,39 @@ -# 前端打包 -FROM node:24 AS build +# 使用官方 Node.js 作为基础镜像;不适用 alpine 版本,这样可以自带git +FROM node:latest AS builder + +# 在容器中创建目录 WORKDIR /app + # 安装pnpm -RUN npm install -g pnpm +RUN npm install -g pnpm@10.10.0 # 设置pnpm镜像源 RUN pnpm config set registry https://registry.npmmirror.com # 复制依赖文件 COPY package.json pnpm-lock.yaml ./ -# 安装依赖(类似npm ci的严格模式) -RUN pnpm install --frozen-lockfile -# 复制源代码 +# 先复制scripts目录,因为prepare脚本需要用到其中的文件 +COPY scripts ./scripts +# 创建src目录,确保create-base-files.js脚本能正常写入文件 +RUN mkdir -p src +# 安装依赖 +RUN pnpm install +# 复制其余源代码 COPY . . # 构建项目 RUN pnpm run build -# 内容组装 -FROM nginx:1.29.1 AS final -COPY --from=build /app/dist/build/h5 /usr/share/nginx/html -COPY ./nginx.conf /etc/nginx/nginx.conf +# 使用nginx作为服务 +FROM nginx:1.29.1-alpine3.22 AS production-stage -CMD nginx -g "daemon off;" +# 将构建好的项目复制到nginx下 +COPY --from=builder /app/dist/build/h5 /usr/share/nginx/html + +# 将默认的nginx配置文件替换为我们自定义的nginx配置文件 +COPY nginx.conf /etc/nginx/conf.d/default.conf + +# 暴露端口 +EXPOSE 80 +EXPOSE 443 + +# 启动nginx +CMD ["nginx", "-g", "daemon off;"] diff --git a/package.json b/package.json index c7fb157..9680441 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "url-old": "https://github.com/codercup/unibest/issues" }, "engines": { - "node": ">=22", + "node": ">=20", "pnpm": ">=9" }, "scripts": { diff --git a/vite.config.ts b/vite.config.ts index 0218120..4275b14 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -184,6 +184,11 @@ export default defineConfig(({ command, mode }) => { target: 'es6', // 开发环境不用压缩 minify: mode === 'development' ? false : 'esbuild', + // 添加外部依赖,解决Docker构建中的依赖解析问题 + rollupOptions: { + // 前4个由 @tanstack/vue-query 引起的,std-env 由 @uni-helper/uni-env 引起的 + external: ['@tanstack/query-core', 'vue-demi', '@vue/devtools-api', '@tanstack/match-sorter-utils', 'std-env'], + }, }, }) })