vite.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { defineConfig } 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. import legacy from '@vitejs/plugin-legacy'
  10. // eslint
  11. import eslintPlugin from 'vite-plugin-eslint'
  12. import esbuild from 'rollup-plugin-esbuild'
  13. // https://vitejs.dev/config/
  14. export default defineConfig(({ command }) => {
  15. return {
  16. plugins: [
  17. vue(),
  18. createSvgIconsPlugin({
  19. iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
  20. symbolId: 'icon-[dir]-[name]'
  21. }),
  22. // 自动引入内容
  23. AutoImport({
  24. imports: [
  25. 'vue',
  26. 'vue-router'
  27. ],
  28. dirs: [
  29. 'src/hooks/**',
  30. 'src/stores/**',
  31. 'src/utils/**'
  32. ],
  33. resolvers: command === 'build' ? [ElementPlusResolver()] : [],
  34. dts: 'src/auto-import/imports.d.ts',
  35. eslintrc: {
  36. enabled: true
  37. }
  38. }),
  39. // 自动引入组件
  40. Components({
  41. dirs: [
  42. 'src/components'
  43. ],
  44. resolvers: command === 'build' ? [ElementPlusResolver()] : [],
  45. dts: 'src/auto-import/components.d.ts'
  46. }),
  47. // 对大于 10k 的文件进行压缩
  48. viteCompression({
  49. threshold: 10240,
  50. })
  51. ]
  52. .concat(getDynamicPlugins(command))
  53. // eslint
  54. .concat(command !== 'build'? [eslintPlugin({include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']})] : []
  55. ),
  56. server: {
  57. host: true,
  58. port: 9527,
  59. open: true
  60. },
  61. resolve: {
  62. alias: {
  63. '@': path.resolve(__dirname, 'src'),
  64. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
  65. }
  66. },
  67. build: {
  68. base: './',
  69. rollupOptions: {
  70. // 静态资源分类打包
  71. output: {
  72. chunkFileNames: 'static/js/[name]-[hash].js',
  73. entryFileNames: 'static/js/[name]-[hash].js',
  74. assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
  75. // 静态资源分拆打包
  76. manualChunks (id) {
  77. if (id.includes('node_modules')) {
  78. if (id.toString().indexOf('.pnpm/') !== -1) {
  79. return id.toString().split('.pnpm/')[1].split('/')[0].toString();
  80. } else if (id.toString().indexOf('node_modules/') !== -1) {
  81. return id.toString().split('node_modules/')[1].split('/')[0].toString();
  82. }
  83. }
  84. }
  85. }
  86. },
  87. sourcemap: false,
  88. minify: 'terser',
  89. reportCompressedSize: false
  90. }
  91. }
  92. })
  93. /**
  94. * 根据运行环境加载动态插件
  95. * @param {String} command serve: 开发环境 || 预览环境;build: 构建环境
  96. */
  97. function getDynamicPlugins (command) {
  98. const dynamicPlugins = [
  99. command === 'serve' ?
  100. // esbuild 在开发环境兼容低版本浏览器
  101. {
  102. ...esbuild({
  103. target: 'chrome64',
  104. include: /\.vue|.ts|.js$/,
  105. loaders: {
  106. '.vue': 'js'
  107. }
  108. }),
  109. enforce: 'post'
  110. } :
  111. // 在构建时使用 legacy 处理兼容
  112. legacy({
  113. // 兼容浏览器列表
  114. targets: ['chrome >= 51', 'firefox >= 54', 'safari >= 12', 'Android >= 7']
  115. })
  116. ]
  117. return dynamicPlugins
  118. }