zhangwei
2 天以前 9488f2bcb346c32c64f2c4eb5814fece8726b363
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
105
106
107
108
109
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { defineConfig, loadEnv, ConfigEnv } from 'vite';
import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import compression from 'vite-plugin-compression2';
import { buildConfig } from './src/utils/build';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { CodeInspectorPlugin } from 'code-inspector-plugin';
import fs from 'fs';
import { visualizer } from 'rollup-plugin-visualizer';
import { webUpdateNotice } from '@plugin-web-update-notification/vite';
const pathResolve = (dir: string) => {
    return resolve(__dirname, '.', dir);
};
 
const alias: Record<string, string> = {
    '/@': pathResolve('./src/'),
    'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
};
 
const viteConfig = defineConfig((mode: ConfigEnv) => {
    const env = loadEnv(mode.mode, process.cwd());
    fs.writeFileSync('./public/config.js', `window.__env__ = ${JSON.stringify(env, null, 2)} `);
    return {
        plugins: [
            visualizer({ open: false }), // 开启可视化分析页面
            CodeInspectorPlugin({
                bundler: 'vite',
                hotKeys: ['shiftKey'],
            }),
            vue(),
            vueJsx(),
            webUpdateNotice({
                versionType: 'build_timestamp',
                notificationConfig: {
                    placement: 'topLeft',
                },
                notificationProps: {
                    title: '📢 系统更新',
                    description: '系统更新啦,请刷新页面!',
                    buttonText: '刷新',
                    dismissButtonText: '忽略',
                },
            }),
            vueSetupExtend(),
            compression({
                deleteOriginalAssets: false, // 是否删除源文件
                threshold: 5120, // 对大于 5KB 文件进行 gzip 压缩,单位Bytes
                skipIfLargerOrEqual: true, // 如果压缩后的文件大小等于或大于原始文件,则跳过压缩
                // algorithm: 'gzip', // 压缩算法,可选[‘gzip’,‘brotliCompress’,‘deflate’,‘deflateRaw’]
                // exclude: [/\.(br)$/, /\.(gz)$/], // 排除指定文件
            }),
            JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
        ],
        root: process.cwd(),
        resolve: { alias },
        base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
        optimizeDeps: { exclude: ['vue-demi'] },
        server: {
            host: '0.0.0.0',
            port: env.VITE_PORT as unknown as number,
            open: JSON.parse(env.VITE_OPEN),
            hmr: true,
            proxy: {
                '^/[Uu]pload': {
                    target: env.VITE_API_URL,
                    changeOrigin: true,
                },
            },
        },
        build: {
            outDir: 'dist',
            chunkSizeWarningLimit: 1500,
            assetsInlineLimit: 5000, // 小于此阈值的导入或引用资源将内联为 base64 编码
            sourcemap: false, // 构建后是否生成 source map 文件
            extractComments: false, // 移除注释
            minify: 'terser', // 启用后 terserOptions 配置才有效
            terserOptions: {
                compress: {
                    drop_console: true, // 生产环境时移除console
                    drop_debugger: true,
                },
            },
            rollupOptions: {
                output: {
                    chunkFileNames: 'assets/js/[name]-[hash].js', // 引入文件名的名称
                    entryFileNames: 'assets/js/[name]-[hash].js', // 包的入口文件名称
                    assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
                        }
                    },
                },
                ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
            },
        },
        css: { preprocessorOptions: { css: { charset: false }, scss: { silenceDeprecations: ['legacy-js-api', 'global-builtin', 'fs-importer-cwd', 'import'] } } },
        define: {
            __VUE_I18N_LEGACY_API__: JSON.stringify(false),
            __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
            __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
            __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
            __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
        },
    };
});
 
export default viteConfig;