zhgdlarge/vite.config.ts

167 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-07-12 09:56:31 +08:00
import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
import { createHtmlPlugin } from "vite-plugin-html";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import { wrapperEnv } from "./src/utils/getEnv";
import { visualizer } from "rollup-plugin-visualizer";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import viteCompression from "vite-plugin-compression";
import vueSetupExtend from "vite-plugin-vue-setup-extend-plus";
import eslintPlugin from "vite-plugin-eslint";
import vueJsx from "@vitejs/plugin-vue-jsx";
import importToCDN from "vite-plugin-cdn-import";
2024-06-04 19:16:37 +08:00
import optimizer from "vite-plugin-optimizer";
2023-07-12 09:56:31 +08:00
// import AutoImport from "unplugin-auto-import/vite";
// import Components from "unplugin-vue-components/vite";
// import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
2024-10-17 17:28:00 +08:00
import { vitePluginMars3d } from "vite-plugin-mars3d";
2023-07-12 09:56:31 +08:00
// @see: https://vitejs.dev/config/
// export const BASE_IMAGE_URL = import.meta.env.NODE_ENV === "development" ? "./src" : "";
2024-06-04 19:16:37 +08:00
//在vite.config.ts中增加
2023-07-12 09:56:31 +08:00
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
const env = loadEnv(mode, process.cwd());
const viteEnv = wrapperEnv(env);
return {
// publicPath:'./',
2023-07-12 09:56:31 +08:00
base: "./",
resolve: {
alias: {
"@": resolve(__dirname, "./src"),
"vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
}
},
css: {
preprocessorOptions: {
scss: {
// additionalData: `@import "@/styles/var.scss";`
}
}
},
2024-06-04 19:16:37 +08:00
define: {
"process.env": {}
},
2023-07-12 09:56:31 +08:00
server: {
// 服务器主机名,如果允许外部访问,可设置为 "0.0.0.0"
host: "0.0.0.0",
port: viteEnv.VITE_PORT,
open: viteEnv.VITE_OPEN,
cors: true,
// 跨域代理配置
proxy: {
"/api": {
target: viteEnv.VITE_API_URL, // easymock
// target: "https://www.fastmock.site/mock/f81e8333c1a9276214bcdbc170d9e0a0", // fastmock
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, "")
2024-10-17 17:28:00 +08:00
},
2023-07-12 09:56:31 +08:00
}
// open: true,
// host: "localhost",
// port: 8520,
// //这里的ip和端口是前端项目的;下面为需要跨域访问后端项目
// proxy: {
// "^/token": {
// target: "http://192.168.34.155:8520/", //这里填入你要请求的接口的前缀
// ws: true, //代理websocket
// changeOrigin: true, //虚拟的站点需要更管origin
// secure: true, //是否https接口
// rewrite: path => path.replace(/^\/api/, "")
// }
// }
},
plugins: [
vue(),
2024-10-17 17:28:00 +08:00
vitePluginMars3d(),
2023-07-12 09:56:31 +08:00
createHtmlPlugin({
inject: {
data: {
title: viteEnv.VITE_GLOB_APP_TITLE
}
}
}),
// * 使用 svg 图标
createSvgIconsPlugin({
iconDirs: [resolve(process.cwd(), "src/assets/icons")],
symbolId: "icon-[dir]-[name]"
}),
// * EsLint 报错信息显示在浏览器界面上
// eslintPlugin(),
// * vite 可以使用 jsx/tsx 语法
vueJsx(),
// * name 可以写在 script 标签上
vueSetupExtend(),
// * 是否生成包预览(分析依赖包大小,方便做优化处理)
viteEnv.VITE_REPORT && visualizer(),
// * gzip compress
viteEnv.VITE_BUILD_GZIP &&
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: "gzip",
ext: ".gz"
}),
// * cdn 引入vue按需引入会导致依赖vue的插件出现问题(列如:pinia/vuex)
importToCDN({
modules: [
// {
// name: "vue",
// var: "Vue",
// path: "https://unpkg.com/vue@next"
// },
// 使用cdn引入element-plus时,开发环境还是需要在main.js中引入element-plus,可以不用引入css
// {
// name: "element-plus",
// var: "ElementPlus",
// path: "https://unpkg.com/element-plus",
// css: "https://unpkg.com/element-plus/dist/index.css"
// }
]
2024-06-04 19:16:37 +08:00
}),
// mqtt与浏览器兼容
optimizer({
child_process: () => ({
find: /^(node:)?child_process$/,
code: `const child_process = import.meta.glob('child_process'); export { child_process as default }`
})
2023-07-12 09:56:31 +08:00
})
2024-06-04 19:16:37 +08:00
2023-07-12 09:56:31 +08:00
// * demand import element
// AutoImport({
// resolvers: [ElementPlusResolver()]
// }),
// Components({
// resolvers: [ElementPlusResolver()]
// }),
],
// * 打包去除 console.log && debugger
esbuild: {
pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
},
build: {
outDir: "dist",
minify: "esbuild",
// esbuild 打包更快,但是不能去除 console.logterser打包慢但能去除 console.log
// minify: "terser",
// terserOptions: {
// compress: {s
2023-07-12 09:56:31 +08:00
// drop_console: viteEnv.VITE_DROP_CONSOLE,
// drop_debugger: true
// }
// },
chunkSizeWarningLimit: 2500,
rollupOptions: {
output: {
// Static resource classification and packaging
chunkFileNames: "assets/js/[name]-[hash].js",
entryFileNames: "assets/js/[name]-[hash].js",
assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
}
}
}
};
});