Optimize admin production bundle

This commit is contained in:
zhx 2026-05-17 13:43:35 +08:00
parent 5ddd79d116
commit 3dcf7bbd3e
2 changed files with 44 additions and 13 deletions

View File

@ -14,19 +14,6 @@
<!-- 由 vite 注入 VITE_APP_TITLE 变量,在 .env 文件内配置 -->
<title><%= VITE_APP_TITLE %></title>
<link rel="icon" href="/favicon.ico" />
<script>
// 生产环境下注入百度统计
if (window._VBEN_ADMIN_PRO_APP_CONF_) {
var _hmt = _hmt || [];
(function () {
var hm = document.createElement('script');
hm.src =
'https://hm.baidu.com/hm.js?b38e689f40558f20a9a686d7f6f33edf';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(hm, s);
})();
}
</script>
</head>
<body>
<div id="app"></div>

View File

@ -8,6 +8,37 @@ const DEV_INTERNAL_GOLANG_TARGET = 'http://127.0.0.1:2900';
const DEV_PROD_GOLANG_TARGET = 'https://jvapi.haiyihy.com';
const DEV_PROD_TARGET = 'https://yumi-admin.haiyihy.com';
function getBundledAssetFileName(assetInfo: {
name?: string;
names?: string[];
}) {
const names = [
assetInfo.name,
...(Array.isArray(assetInfo.names) ? assetInfo.names : []),
]
.filter(Boolean)
.join(',');
if (names.endsWith('.css') || names.includes('.css,')) {
return 'css/app.css';
}
return '[ext]/[name]-[hash].[ext]';
}
function addBuildTimestampToBundledAssets() {
const buildTimestamp = Date.now().toString();
return {
name: 'chatapp-build-asset-timestamp',
transformIndexHtml(html: string) {
return html
.replaceAll('/js/app.js"', `/js/app.js?v=${buildTimestamp}"`)
.replaceAll('/css/app.css"', `/css/app.css?v=${buildTimestamp}"`);
},
};
}
function createPrefixRegExp(prefix: string) {
return new RegExp(`^${prefix.replaceAll('/', '\\/')}`);
}
@ -16,6 +47,19 @@ export default defineConfig(async () => {
return {
application: {},
vite: {
build: {
cssCodeSplit: false,
modulePreload: false,
rolldownOptions: {
output: {
assetFileNames: getBundledAssetFileName,
chunkFileNames: 'js/[name]-[hash].js',
codeSplitting: false,
entryFileNames: 'js/app.js',
},
},
},
plugins: [addBuildTimestampToBundledAssets()],
server: {
proxy: {
[`${DEV_API_INTERNAL_PREFIX}/go`]: {