vite.config.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { defineConfig, ConfigEnv, UserConfig } from 'vite'
  2. import path from 'path'
  3. // vite.config.ts中无法使用import.meta.env 所以需要引入
  4. import vue from '@vitejs/plugin-vue'
  5. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  6. // 增加 vue文件 script name值
  7. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  8. // 生产gz文件
  9. import viteCompression from 'vite-plugin-compression'
  10. // 按需加载
  11. // import AutoImport from 'unplugin-auto-import/vite'
  12. // import Components from 'unplugin-vue-components/vite'
  13. //import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  14. function resolve(dir: string) {
  15. return path.join(__dirname, '.', dir)
  16. }
  17. // https://vitejs.dev/config/
  18. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  19. return {
  20. plugins: [
  21. vue(),
  22. vueSetupExtend(),
  23. // AutoImport({
  24. // resolvers: [ElementPlusResolver()],
  25. // }),
  26. // Components({
  27. // resolvers: [ElementPlusResolver()],
  28. // }),
  29. // * 使用 svg 图标
  30. createSvgIconsPlugin({
  31. // 指定需要缓存的图标文件夹
  32. iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
  33. // 指定symbolId格式
  34. symbolId: 'icon-[dir]-[name]',
  35. }),
  36. // gzip压缩 生产环境生成 .gz 文件
  37. mode === 'production' &&
  38. viteCompression({
  39. verbose: true,
  40. disable: false,
  41. threshold: 10240,
  42. algorithm: 'gzip',
  43. ext: '.gz',
  44. }),
  45. ],
  46. css: {
  47. preprocessorOptions: {
  48. scss: {
  49. additionalData: `@use "./src/styles/index.scss" as *;`,
  50. },
  51. },
  52. },
  53. // 配置别名
  54. resolve: {
  55. alias: {
  56. '@': resolve('src'),
  57. static: resolve('public/static'),
  58. },
  59. // 忽略后缀名的配置选项, 添加 .vue 选项时要记得原本默认忽略的选项也要手动写入
  60. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  61. },
  62. //启动服务配置
  63. server: {
  64. // 服务器主机名,如果允许外部访问,可设置为 "0.0.0.0" 也可设置成你的ip地址
  65. host: '0.0.0.0',
  66. port: 8080,
  67. open: true,
  68. https: false,
  69. cors: true,
  70. // 代理跨域(模拟示例)
  71. proxy: {
  72. // "/api": {
  73. // target: "", // easymock
  74. // changeOrigin: true,
  75. // rewrite: path => path.replace(/^\/api/, "")
  76. // }
  77. },
  78. },
  79. // 生产环境打包配置
  80. esbuild: {
  81. pure: mode === 'production' ? ['console.log', 'debugger'] : [],
  82. },
  83. build: {
  84. terserOptions: {
  85. compress: {
  86. drop_console: true,
  87. drop_debugger: true,
  88. },
  89. },
  90. },
  91. }
  92. })