代码拉取完成,页面将自动刷新
import { fileURLToPath, URL } from "node:url";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
// import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import dayjs from "dayjs";
// import fs from "node:fs";
function createProxy(proxyArr) {
const proxy = {};
proxyArr.forEach(([context, target]) => {
proxy[context] = {
target,
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp(`^${context}`), ""),
};
});
return proxy;
}
export default defineConfig(({ mode: VITE_MODE }: { mode: string }) => {
const env = loadEnv(VITE_MODE, process.cwd());
console.log("VITE_MODE: ", VITE_MODE);
console.log("ENV: ", env);
let proxyConfig = [];
if (env.VITE_PROXY) {
try {
proxyConfig = JSON.parse(env.VITE_PROXY); //二维数组 [["/A","http://"],["/B","http://"]...]
} catch (error) {
console.error("解析 VITE_PROXY 失败:", error);
}
}
return {
plugins: [
vue(),
AutoImport({
imports: ["vue", "vue-router"],
// resolvers: [AntDesignVueResolver()],
dts: true, // 自动生成类型声明文件
}),
Components({
// resolvers: [AntDesignVueResolver()],
}),
],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
public: fileURLToPath(new URL("./public", import.meta.url)),
},
},
server: {
host: "0.0.0.0",
port: 3000,
open: false,
https: false,
cors: true,
proxy: createProxy(proxyConfig),
//移至.env.development文件中配置
// proxy: {
// "/api": {
// target: "http://127.0.0.1:9000",
// changeOrigin: true,
// rewrite(path) {
// return path.replace(/^\/api/, "");
// },
// },
// },
},
build: {
outDir: `prodist${dayjs().format("YYYYMMDD")}`,
target: "modules",
cssCodeSplit: true,
sourcemap: false,
minify: "terser",
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ["console.*"],
},
format: {
comments: false,
},
},
assetsInlineLimit: 4096, //小于4MB的图片会被转成base64
chunkSizeWarningLimit: 1800,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes("node_modules")) {
return "vendor";
}
},
chunkFileNames: "assets/chunkjs/[name]-[hash].js",
entryFileNames: "assets/entryjs/[name]-[hash].js",
assetFileNames(assetInfo) {
if (assetInfo.name.endsWith(".css")) {
return "assets/css/[name]-[hash].css";
}
const imgExts = [".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".ico"];
if (imgExts.some((ext) => assetInfo.name.endsWith(ext))) {
return "assets/img/[name]-[hash][extname]";
}
const fontExts = [".woff", ".woff2", ".ttf", ".eot", ".otf"];
if (fontExts.some((ext) => assetInfo.name.endsWith(ext))) {
return "assets/font/[name]-[hash][extname]";
}
return "assets/others/[name]-[hash][extname]";
},
},
},
},
};
});
// const imgPlugin = (limit = 1024) => {
// return {
// name: "img-plugin",
// async transform(code, id) {
// if (process.env.NODE_ENV !== "development") {
// return;
// }
// if (id.endsWith(".png")) {
// return;
// }
// const state = await fs.promises.stat(id);
// if (state.size > limit) {
// return;
// }
// const buffer = await fs.promises.readFile(id);
// const base64 = buffer.toString("base64");
// const dataurl = `data:images/png;base64,${base64}`;
// return {
// code: `export default "${dataurl}"`,
// };
// },
// };
// };
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。