vite.config.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import vue from '@vitejs/plugin-vue'
  2. import vueJsx from '@vitejs/plugin-vue-jsx'
  3. import { UserConfig, ConfigEnv, loadEnv, defineConfig } from 'vite'
  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 Icons from 'unplugin-icons/vite'
  8. import IconsResolver from 'unplugin-icons/resolver'
  9. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  10. import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
  11. import UnoCSS from 'unocss/vite'
  12. import { resolve } from 'path'
  13. import { name, version, engines, dependencies, devDependencies } from './package.json'
  14. /** 平台的名称、版本、运行所需的`node`版本、依赖、构建时间的类型提示 */
  15. const __APP_INFO__ = {
  16. pkg: { name, version, engines, dependencies, devDependencies },
  17. buildTimestamp: Date.now()
  18. }
  19. const pathSrc = resolve(__dirname, 'src')
  20. // https://cn.vitejs.dev/config
  21. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  22. const env = loadEnv(mode, process.cwd())
  23. return {
  24. resolve: {
  25. alias: {
  26. '@': pathSrc
  27. }
  28. },
  29. css: {
  30. // CSS 预处理器
  31. preprocessorOptions: {
  32. // 定义全局 SCSS 变量
  33. scss: {
  34. javascriptEnabled: true,
  35. additionalData: `
  36. @use "@/styles/variables.scss" as *;
  37. `
  38. }
  39. }
  40. },
  41. server: {
  42. // 允许IP访问
  43. host: '0.0.0.0',
  44. // 应用端口 (默认:3000)
  45. port: Number(env.VITE_APP_PORT),
  46. // 运行是否自动打开浏览器
  47. open: true,
  48. proxy: {
  49. /** 代理前缀为 /dev-api 的请求 */
  50. [env.VITE_APP_BASE_API]: {
  51. changeOrigin: true,
  52. // 接口地址
  53. target: env.VITE_APP_API_URL,
  54. rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
  55. }
  56. }
  57. },
  58. plugins: [
  59. vue(),
  60. // jsx、tsx语法支持
  61. vueJsx(),
  62. // MOCK 服务
  63. env.VITE_MOCK_DEV_SERVER === 'true' ? mockDevServerPlugin() : null,
  64. UnoCSS({
  65. hmrTopLevelAwait: false
  66. }),
  67. // 自动导入参考: https://github.com/sxzz/element-plus-best-practices/blob/main/vite.config.ts
  68. AutoImport({
  69. // 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
  70. imports: ['vue', '@vueuse/core', 'pinia', 'vue-router', 'vue-i18n'],
  71. resolvers: [
  72. // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
  73. ElementPlusResolver(),
  74. // 自动导入图标组件
  75. IconsResolver({})
  76. ],
  77. eslintrc: {
  78. // 是否自动生成 eslint 规则,建议生成之后设置 false
  79. enabled: false,
  80. // 指定自动导入函数 eslint 规则的文件
  81. filepath: './.eslintrc-auto-import.json',
  82. globalsPropValue: true
  83. },
  84. // 是否在 vue 模板中自动导入
  85. vueTemplate: true,
  86. // 指定自动导入函数TS类型声明文件路径 (false:关闭自动生成)
  87. // dts: false
  88. dts: 'src/typings/auto-imports.d.ts'
  89. }),
  90. Components({
  91. resolvers: [
  92. // 自动导入 Element Plus 组件
  93. ElementPlusResolver(),
  94. // 自动注册图标组件
  95. IconsResolver({
  96. // element-plus图标库,其他图标库 https://icon-sets.iconify.design/
  97. enabledCollections: ['ep']
  98. })
  99. ],
  100. // 指定自定义组件位置(默认:src/components)
  101. dirs: ['src/components', 'src/**/components'],
  102. // 指定自动导入组件TS类型声明文件路径 (false:关闭自动生成)
  103. // dts: false
  104. dts: 'src/typings/components.d.ts'
  105. }),
  106. Icons({
  107. // 自动安装图标库
  108. autoInstall: true
  109. }),
  110. createSvgIconsPlugin({
  111. // 指定需要缓存的图标文件夹
  112. iconDirs: [resolve(pathSrc, 'assets/icons')],
  113. // 指定symbolId格式
  114. symbolId: 'icon-[dir]-[name]'
  115. })
  116. ],
  117. // 预加载项目必需的组件
  118. optimizeDeps: {
  119. include: [
  120. 'vue',
  121. 'vue-router',
  122. 'pinia',
  123. 'axios',
  124. '@vueuse/core',
  125. 'sortablejs',
  126. 'path-to-regexp',
  127. 'echarts',
  128. '@wangeditor/editor',
  129. '@wangeditor/editor-for-vue',
  130. 'vue-i18n',
  131. 'path-browserify',
  132. 'element-plus/es/components/form/style/css',
  133. 'element-plus/es/components/form-item/style/css',
  134. 'element-plus/es/components/button/style/css',
  135. 'element-plus/es/components/input/style/css',
  136. 'element-plus/es/components/input-number/style/css',
  137. 'element-plus/es/components/switch/style/css',
  138. 'element-plus/es/components/upload/style/css',
  139. 'element-plus/es/components/menu/style/css',
  140. 'element-plus/es/components/col/style/css',
  141. 'element-plus/es/components/icon/style/css',
  142. 'element-plus/es/components/row/style/css',
  143. 'element-plus/es/components/tag/style/css',
  144. 'element-plus/es/components/dialog/style/css',
  145. 'element-plus/es/components/loading/style/css',
  146. 'element-plus/es/components/radio/style/css',
  147. 'element-plus/es/components/radio-group/style/css',
  148. 'element-plus/es/components/popover/style/css',
  149. 'element-plus/es/components/scrollbar/style/css',
  150. 'element-plus/es/components/tooltip/style/css',
  151. 'element-plus/es/components/dropdown/style/css',
  152. 'element-plus/es/components/dropdown-menu/style/css',
  153. 'element-plus/es/components/dropdown-item/style/css',
  154. 'element-plus/es/components/sub-menu/style/css',
  155. 'element-plus/es/components/menu-item/style/css',
  156. 'element-plus/es/components/divider/style/css',
  157. 'element-plus/es/components/card/style/css',
  158. 'element-plus/es/components/link/style/css',
  159. 'element-plus/es/components/breadcrumb/style/css',
  160. 'element-plus/es/components/breadcrumb-item/style/css',
  161. 'element-plus/es/components/table/style/css',
  162. 'element-plus/es/components/tree-select/style/css',
  163. 'element-plus/es/components/table-column/style/css',
  164. 'element-plus/es/components/select/style/css',
  165. 'element-plus/es/components/option/style/css',
  166. 'element-plus/es/components/pagination/style/css',
  167. 'element-plus/es/components/tree/style/css',
  168. 'element-plus/es/components/alert/style/css',
  169. 'element-plus/es/components/radio-button/style/css',
  170. 'element-plus/es/components/checkbox-group/style/css',
  171. 'element-plus/es/components/checkbox/style/css',
  172. 'element-plus/es/components/tabs/style/css',
  173. 'element-plus/es/components/tab-pane/style/css',
  174. 'element-plus/es/components/rate/style/css',
  175. 'element-plus/es/components/date-picker/style/css',
  176. 'element-plus/es/components/notification/style/css',
  177. 'element-plus/es/components/image/style/css',
  178. 'element-plus/es/components/statistic/style/css',
  179. 'element-plus/es/components/watermark/style/css',
  180. 'element-plus/es/components/config-provider/style/css',
  181. 'element-plus/es/components/text/style/css',
  182. 'element-plus/es/components/drawer/style/css',
  183. 'element-plus/es/components/color-picker/style/css'
  184. ]
  185. },
  186. // 构建配置
  187. build: {
  188. chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
  189. minify: 'terser', // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
  190. terserOptions: {
  191. compress: {
  192. keep_infinity: true, // 防止 Infinity 被压缩成 1/0,这可能会导致 Chrome 上的性能问题
  193. drop_console: true, // 生产环境去除 console
  194. drop_debugger: true // 生产环境去除 debugger
  195. },
  196. format: {
  197. comments: false // 删除注释
  198. }
  199. },
  200. rollupOptions: {
  201. output: {
  202. // manualChunks: {
  203. // "vue-i18n": ["vue-i18n"],
  204. // },
  205. // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
  206. entryFileNames: 'js/[name].[hash].js',
  207. // 用于命名代码拆分时创建的共享块的输出命名
  208. chunkFileNames: 'js/[name].[hash].js',
  209. // 用于输出静态资源的命名,[ext]表示文件扩展名
  210. assetFileNames: (assetInfo: any) => {
  211. const info = assetInfo.name.split('.')
  212. let extType = info[info.length - 1]
  213. // console.log('文件信息', assetInfo.name)
  214. if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
  215. extType = 'media'
  216. } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
  217. extType = 'img'
  218. } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
  219. extType = 'fonts'
  220. }
  221. return `${extType}/[name].[hash].[ext]`
  222. }
  223. }
  224. }
  225. },
  226. define: {
  227. __APP_INFO__: JSON.stringify(__APP_INFO__)
  228. }
  229. }
  230. })