vite.config.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. import { viteMockServe } from 'vite-plugin-mock'
  5. import viteSvgIcons from 'vite-plugin-svg-icons'
  6. import viteESLint from '@ehutch79/vite-eslint'
  7. // https://vitejs.dev/config/
  8. export default env => {
  9. // console.log(111, env);
  10. return defineConfig({
  11. // base: '/vue3-element-admin-site/',
  12. plugins: [
  13. vue(),
  14. viteMockServe({
  15. ignore: /^_/, // 忽略以下划线`_`开头的文件
  16. mockPath: 'mock', // 指定mock目录中的文件全部是mock接口
  17. supportTs: false, // mockPath目录中的文件是否支持ts文件,现在我们不使用ts,所以设为false
  18. localEnabled: env.mode === 'mock', // 开发环境是否开启mock功能(可以在package.json的启动命令中指定mode为mock)
  19. prodEnabled: env.mode === 'mock', // 生产环境是否开启mock功能
  20. injectCode: `
  21. import { setupProdMockServer } from '../mock/_createProductionServer';
  22. setupProdMockServer();
  23. `,
  24. }),
  25. viteSvgIcons({
  26. // 指定需要缓存的图标文件夹
  27. iconDirs: [path.resolve(__dirname, 'src/assets/svg')],
  28. // 指定symbolId格式
  29. symbolId: 'icon-[dir]-[name]',
  30. }),
  31. viteESLint({
  32. include: ['src/**/*.vue', 'src/**/*.js'],
  33. }),
  34. ],
  35. css: {
  36. preprocessorOptions: {
  37. scss: {
  38. // 全局变量
  39. // additionalData: '@import "./src/assets/style/global-variables.scss";',
  40. // element-plus升级到v2需要改成以下写法
  41. additionalData: `@use "./src/assets/style/global-variables.scss" as *;`,
  42. },
  43. },
  44. },
  45. resolve: {
  46. alias: {
  47. '@': path.resolve(__dirname, 'src'),
  48. },
  49. },
  50. server: {
  51. port: 3001,
  52. open: true,
  53. proxy: {
  54. //https://9yeeydhohza8.hk1.xiaomiqiu123.top
  55. '/api': {
  56. target: 'http://192.168.18.22:8501', // 后端接口的域名
  57. changeOrigin: true,
  58. rewrite: path => path.replace(/^\/api/, ''),
  59. },
  60. },
  61. },
  62. esbuild: false,
  63. build: {
  64. terserOptions: {
  65. compress: {
  66. keep_infinity: true,
  67. // 删除console
  68. drop_console: true,
  69. },
  70. },
  71. // 禁用该功能可能会提高大型项目的构建性能
  72. brotliSize: false,
  73. rollupOptions: {
  74. output: {
  75. // 拆分单独模块
  76. manualChunks: {
  77. 'element-plus': ['element-plus'],
  78. mockjs: ['mockjs'],
  79. },
  80. },
  81. },
  82. },
  83. })
  84. }