代码拉取完成,页面将自动刷新
import { fileURLToPath } from "node:url";
import { defineConfig, loadEnv, PluginOption } from "vite";
import vue from "@vitejs/plugin-vue";
import eslintPlugin from "vite-plugin-eslint";
import viteCompression from "vite-plugin-compression";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { visualizer } from "rollup-plugin-visualizer";
import { viteMockServe } from "vite-plugin-mock";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { VueAmapResolver } from "@vuemap/unplugin-resolver";
// @see: https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
const root = process.cwd();
const env = loadEnv(mode, root);
const {
VITE_APP_PORT,
VITE_APP_BASE_URL,
VITE_APP_MOCK,
VITE_APP_REPORT,
VITE_APP_BUILD_GZIP,
VITE_APP_OPEN,
VITE_APP_API_URL_PROXY,
VITE_APP_API_URL,
VITE_APP_Auto_Import,
VITE_APP_Components,
} = env;
const isBuild = command === "build";
// vite 插件
const vitePlugins: (PluginOption | PluginOption[])[] = [
vue(),
// 使用 svg 图标
createSvgIconsPlugin({
iconDirs: [fileURLToPath(new URL("./src/assets/iconsvg", import.meta.url))],
symbolId: "icon-[name]",
}),
// EsLint 报错信息显示在浏览器界面上
eslintPlugin(),
];
// vite-plugin-compression gzip compress
if (VITE_APP_BUILD_GZIP === "true") {
vitePlugins.push(
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: "gzip",
ext: ".gz",
}),
);
}
// rollup-plugin-visualizer 是否生成包预览(分析依赖包大小,方便做优化处理)
if (VITE_APP_REPORT === "true") {
vitePlugins.push(visualizer());
}
// vite-plugin-mock
if (VITE_APP_MOCK === "true") {
vitePlugins.push(
viteMockServe({
mockPath: "mock",
supportTs: true,
watchFiles: true,
localEnabled: !isBuild,
prodEnabled: isBuild,
logger: true,
}),
);
}
if (VITE_APP_Auto_Import === "true") {
vitePlugins.push(
AutoImport({
imports: ["vue", "vue-router", "pinia"],
dts: fileURLToPath(new URL("./src/@types/auto-import.d.ts", import.meta.url)),
resolvers: [ElementPlusResolver({ exclude: /^ElAmap[A-Z]*/ }), VueAmapResolver()],
}),
);
}
if (VITE_APP_Components === "true") {
vitePlugins.push(
Components({
dts: fileURLToPath(new URL("./src/@types/components.d.ts", import.meta.url)),
resolvers: [ElementPlusResolver({ exclude: /^ElAmap[A-Z]*/ }), VueAmapResolver()],
}),
);
}
// proxy
const proxy = {};
if (!isBuild) {
// 不是生产环境
if (VITE_APP_API_URL_PROXY && VITE_APP_API_URL !== "") {
// VITE_APP_API_URL_PROXY存在,启用;如果VITE_APP_MOCK启用且mock中有相同url,则mock优先
proxy[VITE_APP_API_URL] = {
target: VITE_APP_API_URL_PROXY,
rewrite: (path) => path.replace(VITE_APP_API_URL, VITE_APP_API_URL),
changeOrigin: true,
};
}
}
return {
root,
base: VITE_APP_BASE_URL + "/",
server: {
host: "0.0.0.0",
port: Number(VITE_APP_PORT || 3001),
open: VITE_APP_OPEN === "true",
cors: true,
proxy,
},
resolve: {
alias: [
{
find: /^~/,
replacement: fileURLToPath(new URL("./node_modules/", import.meta.url)),
},
{
find: /@\//,
replacement: fileURLToPath(new URL("./src/", import.meta.url)),
},
],
},
plugins: vitePlugins,
css: {
preprocessorOptions: {
scss: {
api: "modern-compiler",
silenceDeprecations: ["legacy-js-api"],
},
less: {
javascriptEnabled: true,
},
},
},
build: {
rollupOptions: {
output: {
chunkFileNames: "static/js/[name]-[hash].js",
entryFileNames: "static/js/[name]-[hash].js",
assetFileNames: "static/[ext]/[name]-[hash].[ext]",
manualChunks: {
chart: ["echarts"],
vue: ["vue", "vue-router", "pinia"],
},
},
},
chunkSizeWarningLimit: 1500,
minify: "terser",
terserOptions: {
compress: {
/* drop_console: true,
drop_debugger: true, */
},
},
},
};
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。