vite.config.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. javascriptEnabled: true,
  50. api: 'modern-compiler',
  51. additionalData: `@use "@/styles/index.scss" as *;`,
  52. },
  53. },
  54. },
  55. // 配置别名
  56. resolve: {
  57. alias: {
  58. '@': path.resolve(__dirname, './src'),
  59. static: resolve('public/static'),
  60. },
  61. // 忽略后缀名的配置选项, 添加 .vue 选项时要记得原本默认忽略的选项也要手动写入
  62. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  63. },
  64. //启动服务配置
  65. server: {
  66. // 服务器主机名,如果允许外部访问,可设置为 "0.0.0.0" 也可设置成你的ip地址
  67. host: '0.0.0.0',
  68. port: 8080,
  69. open: true,
  70. https: false,
  71. cors: true,
  72. // 代理跨域(模拟示例)
  73. proxy: {
  74. // "/api": {
  75. // target: "", // easymock
  76. // changeOrigin: true,
  77. // rewrite: path => path.replace(/^\/api/, "")
  78. // }
  79. },
  80. },
  81. // 生产环境打包配置
  82. esbuild: {
  83. pure: mode === 'production' ? ['console.log', 'debugger'] : [],
  84. },
  85. build: {
  86. terserOptions: {
  87. compress: {
  88. drop_console: true,
  89. drop_debugger: true,
  90. },
  91. },
  92. },
  93. }
  94. })