build: 稳定文件名去hash + HTML压缩 + deploy脚本
- vite.config.js: 文件名去掉 [hash],输出到项目本地 dist/(emptyOutDir=true 避免旧 hash 残留堆积);新增 html-minifier-terser 插件压缩 index.html。 - package.json: 新增 deploy 脚本,build 后用 rsync 把 dist/ 的 mainPage 命名空间子目录同步到 Apache 站点根 /root/html,--delete 仅作用于 mainPage 子目录,不误伤既有旧文件(jquery/pdf.js 等)。 - 站点始终由 Apache 静态服务(DocumentRoot /var/www/html),无 Node 实时渲染。
This commit is contained in:
@@ -1,29 +1,54 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import { minify } from 'html-minifier-terser'
|
||||
|
||||
// 构建到项目本地 dist/(每次清空,避免旧产物残留),
|
||||
// 再由 `npm run deploy` 用 rsync 同步到 Apache 站点根 /root/html(软链到 /var/www/html)。
|
||||
// 文件名去掉内容 hash,保持稳定;JS 由 esbuild 压缩,HTML 由下方插件压缩。
|
||||
// 用 mainPage/ 命名空间子目录避免与 /root/html/js、/root/html/css 既有旧文件冲突。
|
||||
function minifyHtml() {
|
||||
return {
|
||||
name: 'minify-html',
|
||||
apply: 'build',
|
||||
transformIndexHtml: {
|
||||
order: 'post',
|
||||
handler(html) {
|
||||
return minify(html, {
|
||||
collapseWhitespace: true,
|
||||
removeComments: true,
|
||||
removeRedundantAttributes: true,
|
||||
removeScriptTypeAttributes: true,
|
||||
removeStyleLinkTypeAttributes: true,
|
||||
collapseBooleanAttributes: true,
|
||||
minifyCSS: true,
|
||||
minifyJS: true,
|
||||
})
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 构建产物输出到站点根 /root/html,但绝不清空该目录
|
||||
// (里面已有 448/449/js/css 等既有内容)。
|
||||
// 用 mainPage/ 命名空间子目录避免与 /root/html/js、/root/html/css 旧文件冲突。
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
plugins: [vue(), minifyHtml()],
|
||||
base: '/',
|
||||
build: {
|
||||
outDir: '/root/html',
|
||||
// ★ 安全项:不清空 outDir,保护既有文件
|
||||
emptyOutDir: false,
|
||||
outDir: 'dist',
|
||||
// ★ dist 在项目内,安全清空,杜绝旧 hash 文件堆积
|
||||
emptyOutDir: true,
|
||||
rollupOptions: {
|
||||
output: {
|
||||
entryFileNames: 'js/mainPage/[name].[hash].js',
|
||||
chunkFileNames: 'js/mainPage/[name].[hash].js',
|
||||
// 稳定文件名:去掉 [hash],便于部署与长效缓存策略
|
||||
entryFileNames: 'js/mainPage/[name].js',
|
||||
chunkFileNames: 'js/mainPage/[name].js',
|
||||
assetFileNames: (assetInfo) => {
|
||||
const name = assetInfo.name || ''
|
||||
if (name.endsWith('.css')) {
|
||||
return 'css/mainPage/[name].[hash][extname]'
|
||||
return 'css/mainPage/[name][extname]'
|
||||
}
|
||||
return 'assets/mainPage/[name].[hash][extname]'
|
||||
}
|
||||
}
|
||||
}
|
||||
return 'assets/mainPage/[name][extname]'
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
server: {
|
||||
// 开发服务器把站点根的静态资源代理到 /root/html,方便引用 /448 /449 等
|
||||
@@ -31,12 +56,12 @@ export default defineConfig({
|
||||
proxy: {
|
||||
'/448': {
|
||||
target: 'http://localhost:8080',
|
||||
changeOrigin: true
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/449': {
|
||||
target: 'http://localhost:8080',
|
||||
changeOrigin: true
|
||||
}
|
||||
}
|
||||
}
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user