1 Star 1 Fork 0

fangzhongwei/dts-vue3-vite

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vite.config.ts 4.87 KB
一键复制 编辑 原始数据 按行查看 历史
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}"`,
// };
// },
// };
// };
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/YaoYaosHusband/dts-vue3-vite.git
git@gitee.com:YaoYaosHusband/dts-vue3-vite.git
YaoYaosHusband
dts-vue3-vite
dts-vue3-vite
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385