Explorar o código

build: 更新构建配置和Dockerfile

- 将Node.js最低版本要求从22降至20以兼容更多环境
- 在vite配置中添加外部依赖解决构建问题
- 重构Dockerfile,优化构建流程并修复依赖安装问题
- 更新nginx配置和容器启动命令
feige996 hai 7 meses
pai
achega
ebd4694aa6
Modificáronse 3 ficheiros con 33 adicións e 12 borrados
  1. 27 11
      Dockerfile
  2. 1 1
      package.json
  3. 5 0
      vite.config.ts

+ 27 - 11
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
+
+# 将构建好的项目复制到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
 
-CMD nginx -g  "daemon off;"
+# 启动nginx
+CMD ["nginx", "-g", "daemon off;"]

+ 1 - 1
package.json

@@ -22,7 +22,7 @@
     "url-old": "https://github.com/codercup/unibest/issues"
   },
   "engines": {
-    "node": ">=22",
+    "node": ">=20",
     "pnpm": ">=9"
   },
   "scripts": {

+ 5 - 0
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'],
+      },
     },
   })
 })