zhangxiao
2024-08-21 406a398a94d54b854472063f219ae87e8fcf4a98
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
import { UserConfig, ConfigEnv, loadEnv } from "vite";
import path from "path";
import { createVitePlugins } from "./src/config/vite";
import { getOssConfig, enableUpload } from "./src/config/oss";
 
function resolvePath(paths: string): string {
    return path.resolve(__dirname, paths);
}
 
// https://vitejs.cn/config/
export default ({ mode }: ConfigEnv): UserConfig => {
    // 环境文件对象
    const envMap = loadEnv(mode, process.cwd());
    const ossConfig = getOssConfig(envMap);
    const now = new Date().getTime().toString();
    //设置资源路径
    let base = envMap.VITE_APP_BASE_ROUTE;
    if (envMap.VITE_APP_MODE !== "development" && enableUpload) {
        base = ossConfig.uploadPath;
    }
    return {
        base,
        resolve: {
            alias: {
                "@": resolvePath("src"),
                types: resolvePath("types")
            }
        },
        // 全局变量
        define: {
            __APP_UPLOAD__: enableUpload,
            __APP_VERSION__: now
        },
        // 插件加载
        plugins: createVitePlugins({
            envMap,
            uploadOption: enableUpload ? ossConfig.uploadOption : undefined,
            now
        }),
        // 全局样式引用
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: `@import "@/styles/utils/index.scss";`
                }
            }
        },
        // 开发环境
        server: {
            hmr: { overlay: false }, // 禁用或配置 HMR 连接 设置 server.hmr.overlay 为 false 可以禁用服务器错误遮罩层
            // 服务配置
            port: 14100, // 类型: number 指定服务器端口;
            open: false, // 类型: boolean | string在服务器启动时自动在浏览器中打开应用程序;
            cors: false, // 类型: boolean | CorsOptions 为开发服务器配置 CORS。默认启用并允许任何源
            host: "0.0.0.0", // IP配置,支持从IP启动
            proxy: {
                "/proxy_url": {
                    target: envMap.VITE_APP_PROXY_URL,
                    changeOrigin: true,
                    secure: false,
                    ws: true,
                    rewrite: (path): string => path.replace(/^\/proxy_url/, "")
                }
            }
        },
        // 生产构建
        build: {
            assetsInlineLimit: 1 * 1024,
            minify: "terser",
            sourcemap: envMap.VITE_APP_MODE === "development",
            outDir: envMap.VITE_APP_OUTPUT_DIR,
            target: "es2015",
            cssTarget: "chrome80",
            terserOptions: {
                compress: {
                    keep_infinity: true,
                    drop_console: false
                }
            },
            rollupOptions: {
                // 确保外部化处理那些你不想打包进库的依赖
                external: [],
                // https://rollupjs.org/guide/en/#big-list-of-options
                output: {
                    manualChunks: {
                        echart: ["echarts"],
                        ui: ["@arco-design/web-vue", "@arco-plugins/vite-vue"],
                        core: ["vue", "vue-router"]
                    }
                }
            },
            // 文件变化时重新构建,会保留构建进程
            // watch: {
            //     // https://rollupjs.org/guide/en/#watch-options
            // },
            // Turning off brotliSize display can slightly reduce packaging time
            chunkSizeWarningLimit: 2000
        }
    };
};