| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { defineConfig, loadEnv } from 'vite'
- import vue from '@vitejs/plugin-vue'
- import { fileURLToPath, URL } from 'node:url'
- import Components from 'unplugin-vue-components/vite'
- import { VantResolver } from '@vant/auto-import-resolver'
- export default defineConfig(({ mode }) => {
- // 加载环境变量
- const env = loadEnv(mode, process.cwd())
- return {
- plugins: [
- vue(),
- // Vant 组件自动导入
- Components({
- // resolvers: [VantResolver({ importStyle: false })],
- // dts: 'src/components.d.ts'
- })
- ],
- resolve: {
- alias: {
- '@': fileURLToPath(new URL('./src', import.meta.url))
- },
- extensions: ['.js', '.json', '.vue', '.mjs', '.ts', '.tsx']
- },
- server: {
- port: 3000,
- host: '0.0.0.0', // 允许局域网访问,方便手机调试
- open: false, // 不自动打开浏览器
- cors: true, // 启用 CORS
- hmr: {
- overlay: true // 显示错误覆盖层
- }
- },
- build: {
- outDir: 'dist',
- assetsDir: 'assets',
- sourcemap: false,
- // 压缩选项 - 使用 esbuild(更快)
- minify: 'esbuild',
- // esbuild 压缩配置
- esbuild: mode === 'production' ? {
- drop: ['console', 'debugger'], // 生产环境移除 console 和 debugger
- pure: ['console.log'] // 标记为纯函数,方便 tree-shaking
- } : {},
- // 设置打包后的文件大小警告限制(kb)
- chunkSizeWarningLimit: 1000,
- // 资源内联限制
- assetsInlineLimit: 4096,
- rollupOptions: {
- output: {
- // 分包策略
- manualChunks: {
- 'vue-vendor': ['vue', 'vue-router', 'pinia'],
- 'vant-vendor': ['vant'],
- 'liff-vendor': ['@line/liff'],
- 'utils': ['axios', 'dayjs']
- },
- // 静态资源文件命名
- chunkFileNames: 'js/[name]-[hash].js',
- entryFileNames: 'js/[name]-[hash].js',
- assetFileNames: '[ext]/[name]-[hash].[ext]'
- }
- },
- // CSS 代码分割
- cssCodeSplit: true
- },
- // 依赖优化
- optimizeDeps: {
- include: [
- 'vue',
- 'vue-router',
- 'pinia',
- 'axios',
- '@line/liff',
- 'vant'
- ]
- },
- // CSS 配置
- css: {
- preprocessorOptions: {
- scss: {
- additionalData: '', // 可以在这里添加全局 scss 变量
- api: 'modern-compiler' // 使用现代编译器 API
- }
- }
- }
- }
- })
|