vite.config.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  7. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  8. import viteCompression from 'vite-plugin-compression'
  9. // eslint
  10. import eslintPlugin from 'vite-plugin-eslint'
  11. // https://vitejs.dev/config/
  12. export default defineConfig(()=> {
  13. return {
  14. plugins: [
  15. vue(),
  16. createSvgIconsPlugin({
  17. iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
  18. symbolId: 'icon-[dir]-[name]'
  19. }),
  20. // 自动引入内容
  21. AutoImport({
  22. imports: [
  23. 'vue',
  24. 'vue-router'
  25. ],
  26. dirs: [
  27. 'src/hooks/**',
  28. 'src/stores/**',
  29. 'src/utils/**'
  30. ],
  31. resolvers: [
  32. ElementPlusResolver()
  33. ],
  34. dts: 'src/auto-import/imports.d.ts',
  35. eslintrc: {
  36. enabled: false
  37. }
  38. }),
  39. // 自动引入组件
  40. Components({
  41. dirs: [
  42. 'src/components'
  43. ],
  44. resolvers: [
  45. ElementPlusResolver()
  46. ],
  47. dts: 'src/auto-import/components.d.ts'
  48. }),
  49. // eslint
  50. eslintPlugin({
  51. include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
  52. }),
  53. // 对大于 1k 的文件进行压缩
  54. viteCompression({
  55. threshold: 1000,
  56. })
  57. ],
  58. server: {
  59. host: true,
  60. port: 9527,
  61. open: true
  62. },
  63. resolve: {
  64. alias: {
  65. '@': path.resolve(__dirname, 'src'),
  66. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
  67. }
  68. },
  69. build: {
  70. base: './',
  71. rollupOptions: {
  72. // 静态资源分类打包
  73. output: {
  74. chunkFileNames: 'static/js/[name]-[hash].js',
  75. entryFileNames: 'static/js/[name]-[hash].js',
  76. assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
  77. // 静态资源分拆打包
  78. manualChunks (id) {
  79. if (id.includes('node_modules')) {
  80. if (id.toString().indexOf('.pnpm/') !== -1) {
  81. return id.toString().split('.pnpm/')[1].split('/')[0].toString();
  82. } else if (id.toString().indexOf('node_modules/') !== -1) {
  83. return id.toString().split('node_modules/')[1].split('/')[0].toString();
  84. }
  85. }
  86. }
  87. }
  88. },
  89. sourcemap: false,
  90. target: 'es2015',
  91. reportCompressedSize: false
  92. }
  93. }
  94. })