76 lines
3.0 KiB
TypeScript
76 lines
3.0 KiB
TypeScript
import { defineConfig } from 'vite'
|
||
import vue from '@vitejs/plugin-vue'
|
||
import { resolve } from 'path'
|
||
|
||
export default defineConfig({
|
||
plugins: [vue()],
|
||
root: __dirname,
|
||
base: '',
|
||
build: {
|
||
outDir: 'build',
|
||
emptyOutDir: true,
|
||
minify: 'esbuild',
|
||
// 注意:不 drop console/debugger,保留错误信息便于排查
|
||
esbuild: {
|
||
// drop: ['console', 'debugger'], ← 生产环境可开启
|
||
},
|
||
rollupOptions: {
|
||
output: {
|
||
// 入口文件带 hash,避免与动态导入的 chunk 产生命名/导出冲突
|
||
entryFileNames: (chunkInfo) => {
|
||
// 入口 chunk 统一命名为 index.js,确保 PHP 端稳定引用
|
||
return 'index.js'
|
||
},
|
||
chunkFileNames: (chunkInfo) => {
|
||
// vendor chunk 用稳定前缀+hash;其他 chunk(含被提取的 index 共享模块)也用固定名
|
||
if (chunkInfo.name.includes('node_modules')) return 'vendor-[hash].js'
|
||
// 防止 Rollup 提取的共享 chunk 名为 "index" 时与入口冲突
|
||
if (chunkInfo.name === 'index') return 'shared-api.[hash].js'
|
||
return '[name].[hash].js'
|
||
},
|
||
assetFileNames: (assetInfo) => {
|
||
// CSS 和其他资源保持固定名
|
||
if (assetInfo.name && assetInfo.name.endsWith('.css')) return '[name][extname]'
|
||
return '[name].[hash][extname]'
|
||
},
|
||
format: 'es',
|
||
// 手动拆分:将框架依赖独立为 vendor chunk
|
||
// Naive UI 单包 1.3MB+,按内部子目录粗拆,提升首屏并行下载速度。
|
||
manualChunks(id) {
|
||
if (!id.includes('node_modules')) return undefined
|
||
if (id.includes('node_modules/vue/')
|
||
|| id.includes('node_modules/@vue/')
|
||
|| id.includes('node_modules/vue-router/')
|
||
|| id.includes('node_modules/pinia/')) {
|
||
return 'vendor-vue'
|
||
}
|
||
if (id.includes('node_modules/@vicons/')) {
|
||
return 'vendor-icons'
|
||
}
|
||
if (id.includes('node_modules/vuedraggable/') || id.includes('node_modules/sortablejs/')) {
|
||
return 'vendor-draggable'
|
||
}
|
||
// Naive UI:内部子目录之间存在大量交叉依赖(form ⇄ core 等),
|
||
// 按子目录拆分会产生 ESM 循环依赖,导致 "Cannot access 'X' before initialization"。
|
||
// 因此整体打成单个 chunk,避免 TDZ 运行时错误。
|
||
if (id.includes('node_modules/naive-ui/')) {
|
||
return 'vendor-naive'
|
||
}
|
||
// 其它依赖归为通用 vendor
|
||
return 'vendor-misc'
|
||
},
|
||
},
|
||
},
|
||
cssCodeSplit: true,
|
||
// Naive UI 整体打成单个 vendor-naive chunk(避免子目录间循环依赖导致的 TDZ 错误)。
|
||
// 组件库本身约 1.16MB(gzip 后 ~304KB),属于该 UI 库的合理体积,
|
||
// 不再尝试进一步拆分,故将告警阈值上调以避免误导。
|
||
chunkSizeWarningLimit: 1500,
|
||
},
|
||
resolve: {
|
||
alias: {
|
||
'@': resolve(__dirname, 'src'),
|
||
},
|
||
},
|
||
})
|