feat: 新增 node-frontend-vite5 示例(Vite 5 + Vue 3,Tengine 托管)

This commit is contained in:
2026-11-15 11:53:02 +08:00
parent 92a57d99a0
commit 90c3334368
15 changed files with 432 additions and 0 deletions
@@ -0,0 +1,4 @@
dist/
node_modules/
.git/
.DS_Store
+3
View File
@@ -0,0 +1,3 @@
dist/
node_modules/
npm-debug.log*
+23
View File
@@ -0,0 +1,23 @@
# 编译阶段:compile-node22(Vite 5 本身支持 Node 18+,这里统一用 node22 镜像)
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22 AS build
WORKDIR /src
COPY package.json package-lock.json* ./
# npm 源由镜像的 /usr/local/node/etc/npmrc 指向 npmmirror,无需额外换源
RUN npm install --no-audit --no-fund
COPY . ./
RUN npm run build && ls -lh dist/ dist/assets/
# 运行阶段:Tengine 3.1.0(自建 proxy-tengine3 镜像)托管静态文件
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3 AS runtime
LABEL MAINTAINER="tianjun@odboy.cn"
ENV TimeZone=Asia/Shanghai
# 站点配置与构建产物都放进镜像:conf.d/*.conf 由主配置 include,html/ 为站点根目录
COPY nginx/node-frontend-vite5.conf /usr/local/nginx/conf/conf.d/node-frontend-vite5.conf
COPY --from=build /src/dist/ /usr/local/nginx/html/
# 构建期自检:配置语法错误直接让镜像构建失败;纯静态站点没有上游主机名,nginx -t 可直接通过
RUN /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
EXPOSE 80
STOPSIGNAL SIGQUIT
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
+69
View File
@@ -0,0 +1,69 @@
# node-frontend-vite5 示例
**Vite 5** + Vue 3 的前端项目,用来和 `node-frontend-vue3`(Vite 7)对照:同一套两阶段构建,
只把构建工具降一个大版本。**构建阶段**用 `compile-node22`,**运行阶段**以自建 `proxy-tengine3` 托管静态文件。
| 阶段 | 镜像 | 说明 |
| --- | --- | --- |
| 编译 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22` | Node 22 + npm(源为 npmmirror);Vite 5 本身支持 Node 18+ |
| 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3` | Tengine 3.1.0,托管 `dist/` |
纯前端项目:页面数据全部在浏览器内产生,不调用任何后端接口,部署只需要一个 Tengine 容器。
> 关于名字里的 “vite3”:Vite 3 已于 2022 年停止维护,且与当前 Node 22 组合缺少经过验证的插件版本;
> 这里按 **Vite 5** 实现(5.4.21),目录名保留 `vite5`。若必须用 Vite 3,请明确,我再降到对应依赖组合。
### 示例内容
```
node-frontend-vite5/
├── package.json # vue 3.4.38 / vue-router 4.5.1;vite 5.4.21 + plugin-vue 5.1.4
├── vite.config.js # base: './';vue 单独拆包
├── index.html
├── src/
│ ├── main.js # createApp + mount
│ ├── App.vue # 单页:构建信息、计数器、时钟、import.meta.env 展示
│ └── styles.css # 手写样式,不引 UI 库
├── nginx/
│ └── node-frontend-vite5.conf # Tengine 站点配置:静态托管 + /healthz
├── Dockerfile # 两阶段:compile-node22 -> proxy-tengine3
├── build.sh # 构建镜像(PUSH=1 时同时推送)
├── docker-compose.yml # 只部署前端(端口 8092)
├── startup.sh / shutdown.sh
└── .dockerignore / .gitignore
```
### 构建与部署
```shell
sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vite5 .
PUSH=1 sh build.sh # 构建并推送
sh startup.sh # docker-compose -f docker-compose.yml -p node-frontend-vite5-demo up -d
```
访问 http://127.0.0.1:8092/ ;`curl http://127.0.0.1:8092/healthz` 返回 `ok`。
### 本地开发(不用 docker)
```shell
npm install
npm run dev # http://127.0.0.1:5473
```
### 关键点
**import.meta.env**:Vite 在构建时把 `import.meta.env.MODE/PROD/BASE_URL` 静态替换成字面量,
页面直接展示出来便于确认构建模式;生产包里不会带上其它环境变量。
**与 Vite 7 的差异**:配置项写法基本一致(`plugins` / `base` / `build.rollupOptions`),
主要区别是 Vite 5 对 Node 版本要求更低、以及 Vite 6+ 才有的 Environment API 等新特性在本示例中没用到。
**站点配置**:配置放在 `nginx/node-frontend-vite5.conf`,构建时 `COPY` 到 `/usr/local/nginx/conf/conf.d/`;
compose 把宿主机 `./nginx` 挂到同一个 `conf.d`,改完 `nginx -t && nginx -s reload` 生效。
挂目录而不是单文件:单文件挂载编辑会换 inode,容器内看不到改动。
### 停止
```shell
sh shutdown.sh
```
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# 在示例目录下构建示例镜像(编译 + 运行两阶段均在本机 docker 内完成)
set -euo pipefail
cd "$(dirname "$0")"
IMAGE=${IMAGE:-registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vite5}
docker build -t "${IMAGE}" .
if [ "${PUSH:-0}" = "1" ]; then
docker push "${IMAGE}"
fi
@@ -0,0 +1,32 @@
version: '3.8'
# 网段避开其它示例(172.31.0/24 ~ 172.31.5/24 已被 go / rust / node-backend / vue2 / vue3 / react 占用)
networks:
node-frontend-vite5-demo:
ipam:
driver: default
config:
- subnet: "172.31.6.0/24"
services:
frontend:
image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vite5 # 镜像由 build.sh 构建
container_name: node-frontend-vite5-demo
restart: unless-stopped
ports: # 映射端口:前端页面
- "8092:80"
volumes: # 只挂载站点配置目录,改完 reload 即可,不必重建镜像
# 挂目录而不是单文件:单文件挂载在宿主机上编辑会换 inode,容器内看不到改动
- "./nginx:/usr/local/nginx/conf/conf.d"
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
healthcheck:
# 运行镜像自带 net-tools,直接用端口探测
test: ["CMD-SHELL", "netstat -tln | grep -q ':80 '"]
interval: 30s
timeout: 5s
retries: 3
start_period: 5s
networks:
node-frontend-vite5-demo:
ipv4_address: 172.31.6.20
+12
View File
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>node-frontend-vite5 | Vite 5 + Vue 3</title>
</head>
<body>
<div id="app"></div>
<script src="/src/main.js" type="module"></script>
</body>
</html>
@@ -0,0 +1,29 @@
# node-frontend-vite5 站点:Tengine 3.1.0 托管 Vite 5 + Vue 3 构建产物(纯静态,无后端依赖)
server {
listen 80;
server_name _;
access_log /dev/stdout main;
root /usr/local/nginx/html;
index index.html;
# 静态资源带 hash,长缓存;index.html 每次重新验证,保证发版后立即生效
location /assets/ {
expires 30d;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
# 前端路由兜底:单页示例用不到,保留是为了以后切 history 模式时不用改配置
location / {
try_files $uri $uri/ /index.html;
}
# 健康检查:直接读 index.html
location = /healthz {
access_log off;
default_type text/plain;
return 200 "ok\n";
}
}
+23
View File
@@ -0,0 +1,23 @@
{
"name": "node-frontend-vite5",
"version": "1.0.0",
"private": true,
"description": "Vite 5 + Vue 3 前端示例(对比 Vite 7),构建产物由 Tengine 托管",
"type": "module",
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 4173"
},
"dependencies": {
"vue": "3.4.38",
"vue-router": "4.5.1"
},
"devDependencies": {
"@vitejs/plugin-vue": "5.1.4",
"vite": "5.4.21"
}
}
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
docker-compose -f docker-compose.yml -p node-frontend-vite5-demo down
+74
View File
@@ -0,0 +1,74 @@
<template>
<div class="app">
<header class="header">
<span class="logo">node-frontend-vite5</span>
<span class="tag">Vite 5 + Vue 3</span>
</header>
<main class="main">
<div class="grid">
<section class="card">
<h3 class="card-title">构建信息</h3>
<table class="kv">
<tbody>
<tr><th>Vite</th><td>{{ viteVersion }}</td></tr>
<tr><th>Vue</th><td>{{ vueVersion }}</td></tr>
<tr><th>Node 要求</th><td>Vite 5 支持 Node 18+(镜像用 22)</td></tr>
</tbody>
</table>
</section>
<section class="card">
<h3 class="card-title">计数器</h3>
<p class="big">{{ count }}</p>
<button class="btn" @click="count -= 1">-1</button>
<button class="btn primary" @click="count += 1">+1</button>
<button class="btn" @click="count = 0">重置</button>
</section>
<section class="card">
<h3 class="card-title">当前时间</h3>
<p class="big small">{{ clock }}</p>
<span class="hint">前端本地时间,不请求后端</span>
</section>
</div>
<section class="card">
<h3 class="card-title">构建环境变量(import.meta.env)</h3>
<table class="kv">
<tbody>
<tr><th>MODE</th><td>{{ env.MODE }}</td></tr>
<tr><th>PROD</th><td>{{ env.PROD }}</td></tr>
<tr><th>BASE_URL</th><td>{{ env.BASE_URL }}</td></tr>
</tbody>
</table>
</section>
</main>
</div>
</template>
<script setup>
import { onBeforeUnmount, onMounted, ref } from 'vue';
import { version as vueVersionValue } from 'vue';
// Vite 在构建时把 import.meta.env 静态替换成字面量,生产包里不会泄露其它环境变量
const env = import.meta.env;
const viteVersion = `5.4.21`;
const vueVersion = vueVersionValue;
const count = ref(0);
const clock = ref(format(new Date()));
let timer = null;
function format(d) {
const p = (n) => String(n).padStart(2, '0');
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}
onMounted(() => {
timer = setInterval(() => {
clock.value = format(new Date());
}, 1000);
});
onBeforeUnmount(() => clearInterval(timer));
</script>
+5
View File
@@ -0,0 +1,5 @@
import { createApp } from 'vue';
import App from './App.vue';
import './styles.css';
createApp(App).mount('#app');
+115
View File
@@ -0,0 +1,115 @@
* {
box-sizing: border-box;
}
html,
body,
#app {
height: 100%;
margin: 0;
}
body {
font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
color: #303133;
background: #f5f7fa;
}
.header {
display: flex;
align-items: center;
gap: 12px;
height: 56px;
padding: 0 20px;
background: #fff;
border-bottom: 1px solid #ebeef5;
}
.logo {
font-size: 18px;
font-weight: 600;
color: #42b883;
}
.tag {
padding: 2px 8px;
border-radius: 10px;
background: #f0f9eb;
color: #67c23a;
font-size: 12px;
}
.main {
padding: 20px;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 16px;
}
.card {
padding: 20px;
background: #fff;
border: 1px solid #ebeef5;
border-radius: 4px;
}
.card + .card {
margin-top: 16px;
}
.card-title {
margin: 0 0 16px;
font-size: 16px;
font-weight: 600;
}
.kv {
width: 100%;
border-collapse: collapse;
}
.kv th,
.kv td {
padding: 6px 12px;
border: 1px solid #ebeef5;
text-align: left;
}
.kv th {
width: 40%;
background: #fafafa;
}
.big {
margin: 0 0 12px;
font-size: 28px;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.big.small {
font-size: 20px;
}
.btn {
margin-right: 8px;
padding: 6px 14px;
border: 1px solid #dcdfe6;
border-radius: 4px;
background: #fff;
cursor: pointer;
}
.btn.primary {
color: #fff;
border-color: #42b883;
background: #42b883;
}
.hint {
color: #909399;
font-size: 12px;
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
docker-compose -f docker-compose.yml -p node-frontend-vite5-demo up -d
docker-compose -f docker-compose.yml -p node-frontend-vite5-demo ps
@@ -0,0 +1,23 @@
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// 纯静态前端:产物由 Tengine 托管,base 用相对路径,部署到任意子路径都能跑
export default defineConfig({
plugins: [vue()],
base: './',
server: {
host: '0.0.0.0',
port: 5473,
},
build: {
outDir: 'dist',
sourcemap: false,
rollupOptions: {
output: {
manualChunks: {
vue: ['vue', 'vue-router'],
},
},
},
},
});