maven
7 天以前 4a469e847313acc136d19bc71e7b64c3922bc342
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
110
111
112
113
import { defineConfig, loadEnv } from "vite";
import path from "path";
import createVitePlugins from "./vite/plugins";
 
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV } = env;
  const baseUrl =
      env.VITE_APP_ENV === "development"
          ? "http://1.15.17.182:9003"
          : env.VITE_BASE_API;
  const javaUrl =
      env.VITE_APP_ENV === "development"
          ? "http://1.15.17.182:9002"
          : env.VITE_JAVA_API;
 
  return {
    define:{
      __BASE_API__: JSON.stringify(javaUrl)
    },
    base: VITE_APP_ENV === "production" ? "/" : "/",
    plugins: createVitePlugins(env, command === "build"),
    resolve: {
      alias: {
        "~": path.resolve(__dirname, "./"),
        "@": path.resolve(__dirname, "./src"),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
      dedupe: ['vue', 'axios'], // 去重重复依赖
    },
    // 全局开启构建缓存(核心)
    cacheDir: '/var/jenkins_home/workspace/客户-鹏创电子前端/node_modules/.vite',
    // 依赖预构建优化
    optimizeDeps: {
      include: ['vue', 'axios', 'element-plus', 'echarts'], // 根据项目依赖调整
      disabled: false,
      esbuildOptions: {
        target: 'es2020',
        worker: true, // 开启 esbuild 多线程
      }
    },
    // 打包配置(核心优化区)
    build: {
      sourcemap: false, // 彻底关闭生产环境sourcemap
      outDir: "dist",
      assetsDir: "assets",
      chunkSizeWarningLimit: 2000,
      minify: 'esbuild', // 使用 esbuild 压缩(无需额外依赖)
      reportCompressedSize: false, // 关闭产物体积报告,减少耗时
      commonjsOptions: {
        include: [/node_modules/, /\.commonjs$/],
      },
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
          // 分包策略(拆分大依赖)
          manualChunks: {
            vendor: ['vue', 'vue-router', 'pinia', 'axios'],
            ui: ['element-plus'], // 根据实际UI库调整
            charts: ['echarts'], // 有图表库则保留,无则删除
          },
        },
        cache: true,
      },
    },
    server: {
      port: 80,
      host: true,
      open: true,
      proxy: {
        "/dev-api": {
          target: baseUrl,
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, ""),
        },
        "^/v3/api-docs/(.*)": {
          target: baseUrl,
          changeOrigin: true,
        },
      },
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
      // CSS 预编译缓存
      preprocessorOptions: {
        scss: {
          cacheDirectory: path.resolve(__dirname, './node_modules/.vite/scss-cache'),
        },
      },
    },
    // esbuild 全局配置
    esbuild: {
      logOverride: { 'this-is-undefined-in-esm': 'silent' },
      target: 'es2020',
    },
  };
});