huang
8 天以前 73aa66976e35252378be3f09be2474193ccd0bf6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import ReactivityTransform from '@vue-macros/reactivity-transform/vite'
import AutoImport from 'unplugin-auto-import/vite'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {fileURLToPath, URL} from 'node:url'
import { createHash } from 'node:crypto'
 
// 性能优化:简化 manualChunks 分组策略
const chunkGroups = {
    'vue-related': ['vue', '@vue/', 'vue-router', 'pinia'],
    'ui-library': ['element-plus', 'ant-design-vue', 'vant'],
    'utility': ['lodash', 'axios', 'dayjs']
}
 
 
export default defineConfig({
    plugins: [vue(), ReactivityTransform(),
        AutoImport({
            imports: [
                'vue-i18n',
            ],
            dts: './auto-imports.d.ts',
            eslintrc: {
                enabled: false, // 配置更新时临时设为true,
            },
        })],
    resolve: {
        alias: {
            'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },
    // 启用持久缓存
    cacheDir: './node_modules/.vite',
    build: {
        // 输出目录
        outDir: 'dist',
        // 禁用 brotli 压缩报告
        brotliSize: false,
        // 调整 chunk 大小警告限制
        chunkSizeWarningLimit: 1500,
        // 使用 esbuild 压缩以获得更快速度
        minify: 'esbuild',
        // 静态资源小于 8KB 内联
        assetsInlineLimit: 8192,
        // 自定义 Rollup 配置
        rollupOptions: {
            // 排除测试和 mock 文件
            external: [/^tests\//, /^mock\//],
            output: {
                // 简化哈希长度
                entryFileNames: 'assets/js/[name]-[hash:8].js',
                chunkFileNames: 'assets/js/[name]-[hash:8].js',
                // 优化资源输出路径
                assetFileNames: (assetInfo) => {
                    const extType = assetInfo.name.split('.').pop()
                    if (/png|jpe?g|gif|svg|webp|avif/i.test(extType)) {
                        return 'assets/images/[name]-[hash:8][extname]'
                    }
                    if (/woff2?|eot|ttf|otf/i.test(extType)) {
                        return 'assets/fonts/[name]-[hash:8][extname]'
                    }
                    return 'assets/[ext]/[name]-[hash:8][extname]'
                },
                // 优化的 manualChunks 策略
                manualChunks: (id) => {
                    if (id.includes('node_modules')) {
                        const libName = id.split('node_modules/')[1].split('/')[0]
                        for (const [group, deps] of Object.entries(chunkGroups)) {
                            if (deps.some(dep => libName.includes(dep))) {
                                return `vendor-${group}`
                            }
                        }
                        return 'vendor'
                    }
                },
                // 更快的哈希算法
                hashCharacters: 'hex',
                hashFunction: (input) => {
                    return createHash('sha1').update(input).digest('hex').substring(0, 8)
                }
            }
        }
    },
    // 开发服务器配置
    server: {
        port: 801,
        host: true,
        open: false,
        https: false,
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:88/',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, '/'),
            },
        },
    },
    // 预构建优化
    optimizeDeps: {
        include: ['vue', 'vue-router', 'pinia'],
        exclude: ['vue-demi']
    }
})