vite.config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { defineConfig, loadEnv } from 'vite';
  2. import path from 'path';
  3. import vue from '@vitejs/plugin-vue';
  4. import { viteVConsole } from 'vite-plugin-vconsole';
  5. export default defineConfig(({ mode, command }) => {
  6. // 加载环境配置
  7. const env = loadEnv(mode, path.join(process.cwd(), 'env'));
  8. return {
  9. base: '/',// 基础路径
  10. envDir: './env',// 环境目录
  11. css: {
  12. preprocessorOptions: {
  13. scss: {// scss配置全局导入
  14. additionalData: '@import "./src/styles/variables";'
  15. },
  16. }
  17. },
  18. resolve: {
  19. // 别名
  20. alias: [
  21. {
  22. find: '/@',
  23. replacement: path.resolve(__dirname, './src'),
  24. }
  25. ],
  26. },
  27. server: {
  28. // 监听所有地址
  29. host: '0.0.0.0',
  30. // 自定义端口号
  31. port: 3010,
  32. // 自动打开浏览器
  33. open: true,
  34. // 开启热更新
  35. hmr: true,
  36. // 若端口被占用,自动尝试下一个可用端口
  37. strictPort: false,
  38. // 代理规则
  39. proxy: {
  40. '/api': {
  41. // 开启跨域
  42. changeOrigin: true,
  43. // 转发地址
  44. target: env.VITE_API_URL,
  45. // 路径重写
  46. rewrite: (path) => path.replace(/^\/api/, ''),
  47. }
  48. }
  49. },
  50. build: {
  51. outDir: 'dist',// 指定打包文件根目录
  52. sourcemap: false,// 构建后不生成源代码
  53. write: true,// 构建的文件写入磁盘
  54. chunkSizeWarningLimit: 10240,// 触发警告的chunk大小10M
  55. // 底层配置
  56. rollupOptions: {
  57. output: {
  58. entryFileNames: 'js/[name]-[hash].js',
  59. chunkFileNames: 'js/[name]-[hash].js',
  60. }
  61. },
  62. },
  63. esbuild: {
  64. drop: command === 'build' ? ['console', 'debugger'] : [],
  65. },
  66. plugins: [
  67. vue(),
  68. viteVConsole({
  69. entry: path.resolve(__dirname, './src/main.ts'),// 入口文件
  70. localEnabled: command === 'serve',// serve开发环境下
  71. enabled: command === 'serve',// 本地运行时开启
  72. config: {// 配置
  73. maxLogNumber: 1000,
  74. theme: 'light'
  75. }
  76. }),
  77. ],
  78. }
  79. })