Gary Gu
5 days ago e7c05e5d214a149bdaba12f2e84ad0e8136f8726
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * @Author: gjj Ganjj@probim.com.cn
 * @Date: 2025-02-14 14:32:19
 * @LastEditors: gjj Ganjj@probim.com.cn
 * @LastEditTime: 2025-03-17 14:42:01
 * @FilePath: \北京交通大学\vite.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/* eslint-disable no-unused-vars */
import path from "path";
import { defineConfig, splitVendorChunkPlugin } from "vite";
// splitVendorChunk, isCSSRequest
 
import PluginForViteVue2 from "@vitejs/plugin-vue2";
// 屏幕适配
const px2rem = require("postcss-px2rem");
// 配置基本大小
const postcss = px2rem({
  //配rem基准值 基准大小 baseSize
  remUnit: 192, // 设稿尺1920/10
});
 
export default defineConfig(({ mode }) => {
  return {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@css": path.resolve("./src/assets/css"),
        "worker-loader": require.resolve("worker-loader"),
      },
    },
    worker: {
      // 例如,设置一个worker的入口文件
      // 注意:这是一个假设的配置,具体取决于worker-loader是否支持这样的配置
      // entry: '/path/to/worker/entry.js',
      options: { inline: true, name: "workerName.[hash].js" },
    },
    plugins: [PluginForViteVue2(), splitVendorChunkPlugin()],
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: "@import '@css/sass.scss';",
        },
      },
      loaderOptions: {
        postcss: {
          plugins: [postcss],
        },
      },
    },
    server: {
      proxy: {
        "^/api/": {
          // target: "http://47.117.124.20:2002/",
          target: "http://101.201.155.10:4011/",
          rewrite: (path) => path.replace(/^\/api\//, ""),
          changeOrigin: true, //通过浏览器查看像是"未生效",实际发送给后端的是更改过的Host(与target的host相同)
        },
      },
      port: 8081,
      host: "0.0.0.0",
      cors: true,
      open: true,
    },
    esbuild: {
      treeShaking: true,
      drop: mode === "production" ? ["console", "debugger"] : [],
    },
    chainWebpack(config) {
      config.module
        .rule("css")
        .test(/\.css$/)
        .oneOf("vue")
        .use("px2rem-loader")
        .loader("px2rem-loader")
        .options({
          remUnit: 192,
        })
        .end();
    },
    build: {
      minify: "esbuild",
      chunkSizeWarningLimit: 500, //kbs
      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/")) {
          //     const name = id.split("/node_modules/")[1].split("/")[0];
          //     for (const key of Object.keys(ChunksMap)) {
          //       if (ChunksMap[key].includes(name)) {
          //         return key;
          //       }
          //     }
          //     return "vendor";
          //   }
          // },
 
          // eslint-disable-next-line no-unused-vars
          // manualChunks(id, { getModuleInfo, getModuleIds }) {
          //   const getShouldBeVendor = splitVendorChunk();
          //   const chunkName = getShouldBeVendor(id, { getModuleInfo });
          //   if (chunkName === "vendor") {
          //     //1 在node_modules文件夹下
          //     //2 不是css类请求
          //     //3 是被静态引入的
          //     return chunkName;
          //   } else if (id.includes("src")) {
          //     const moduleInfo = getModuleInfo(id);
          //     // console.log(id, "importers", moduleInfo.importers);
          //     // console.log(id, "dynamicImporters", moduleInfo.dynamicImporters);
          //     if (moduleInfo.importers.length + moduleInfo.dynamicImporters.length > 1) {
          //       return "manifest";
          //     }
          //   }
          // },
 
          // eslint-disable-next-line no-unused-vars
          manualChunks(id) {
            if (id.includes("/node_modules/")) {
              return id.split("/node_modules/")[1].split("/")[0];
            }
          },
        },
      },
    },
  };
});