import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"
import svgLoader from "vite-svg-loader"
import UnoCSS from "unocss/vite"
const resolve = (dir) => path.join(__dirname, dir)
export default defineConfig(({ mode }) => {
	// 获取当前环境的配置
	const config = loadEnv(mode, './')
	return {
		base: './',
		build: {
			/** 消除打包大小超过 500kb 警告 */
			chunkSizeWarningLimit: 2000,
			/** Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效 */
			minify: "terser",
			/** 在打包代码时移除 console.log、debugger 和 注释 */
			terserOptions: {
				compress: {
					drop_console: false,
					drop_debugger: true,
					pure_funcs: ["console.log"]
				},
				format: {
					/** 删除注释 */
					comments: false
				}
			},
			/** 打包后静态资源目录 */
			assetsDir: "static",
			rollupOptions: {
				output: {
					assetFileNames: assetInfo => {
						var info = assetInfo.name.split('.')
						var extType = info[info.length - 1]
						if (
							/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)
						) {
							extType = 'media'
						} else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
							extType = 'img'
						} else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
							extType = 'fonts'
						}
						return `static/${extType}/[name]-[hash][extname]`
					},
					chunkFileNames: 'static/js/[name]-[hash].js',
					entryFileNames: 'static/js/[name]-[hash].js'
				}
			},
		},
		plugins: [
			vue(),
			/** 将 SVG 静态图转化为 Vue 组件 */
			svgLoader(),
			/** SVG */
			createSvgIconsPlugin({
				iconDirs: [path.resolve(process.cwd(), "src/icons/svg")],
				symbolId: "icon-[dir]-[name]"
			}),
			/** UnoCSS */
			UnoCSS()
			// styleImport({
			//   resolves: [VantResolve()],
			//   libs: [
			//     {
			//       libraryName: 'vant',
			//       esModule: true,
			//       resolveStyle: (name) => `../es/${name}/style`
			//     }
			//    ]
			// })

		],
		resolve: {
			alias: {
				'@': resolve('src'),
				comps: resolve('src/components'),
				apis: resolve('src/apis'),
				views: resolve('src/views'),
				utils: resolve('src/utils'),
				routes: resolve('src/routes'),
				styles: resolve('src/styles')
			}
		},
		server: {
			/** 是否开启 HTTPS */
			https: false,
			/** 设置 host: true 才可以使用 Network 的形式,以 IP 访问项目 */
			host: true, // host: "0.0.0.0"
			/** 端口号 */
			port: 3333,
			/** 是否自动打开浏览器 */
			open: false,
			/** 跨域设置允许 */
			cors: true,
			/** 端口被占用时,是否直接退出 */
			strictPort: false,
			proxy: {
				'/basice': {
					target: config.VITE_BASIC_URL,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/basice/, '')
				}
			}
		},
	}
})


Logo

前往低代码交流专区

更多推荐