vite.config.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { resolve } from 'path'
  2. import { loadEnv } from 'vite'
  3. import type { UserConfig, ConfigEnv } from 'vite'
  4. import Vue from '@vitejs/plugin-vue'
  5. import VueJsx from '@vitejs/plugin-vue-jsx'
  6. import progress from 'vite-plugin-progress'
  7. import EslintPlugin from 'vite-plugin-eslint'
  8. import { ViteEjsPlugin } from "vite-plugin-ejs"
  9. import { viteMockServe } from 'vite-plugin-mock'
  10. import PurgeIcons from 'vite-plugin-purge-icons'
  11. import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"
  12. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  13. // @ts-expect-error
  14. import DefineOptions from "unplugin-vue-define-options/vite"
  15. import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import'
  16. import UnoCSS from 'unocss/vite'
  17. // https://vitejs.dev/config/
  18. const root = process.cwd()
  19. function pathResolve(dir: string) {
  20. return resolve(root, '.', dir)
  21. }
  22. export default ({ command, mode }: ConfigEnv): UserConfig => {
  23. let env = {} as any
  24. const isBuild = command === 'build'
  25. if (!isBuild) {
  26. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  27. } else {
  28. env = loadEnv(mode, root)
  29. }
  30. return {
  31. base: env.VITE_BASE_PATH,
  32. plugins: [
  33. Vue(),
  34. VueJsx(),
  35. // WindiCSS(),
  36. progress(),
  37. createStyleImportPlugin({
  38. resolves: [ElementPlusResolve()],
  39. libs: [{
  40. libraryName: 'element-plus',
  41. esModule: true,
  42. resolveStyle: (name) => {
  43. return `element-plus/es/components/${name.substring(3)}/style/css`
  44. }
  45. }]
  46. }),
  47. EslintPlugin({
  48. cache: false,
  49. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  50. }),
  51. VueI18nPlugin({
  52. runtimeOnly: true,
  53. compositionOnly: true,
  54. include: [resolve(__dirname, 'src/locales/**')]
  55. }),
  56. createSvgIconsPlugin({
  57. iconDirs: [pathResolve('src/assets/svgs')],
  58. symbolId: 'icon-[dir]-[name]',
  59. svgoOptions: true
  60. }),
  61. PurgeIcons(),
  62. viteMockServe({
  63. ignore: /^\_/,
  64. mockPath: 'mock',
  65. localEnabled: !isBuild,
  66. prodEnabled: isBuild,
  67. injectCode: `
  68. import { setupProdMockServer } from '../mock/_createProductionServer'
  69. setupProdMockServer()
  70. `
  71. }),
  72. DefineOptions(),
  73. ViteEjsPlugin({
  74. title: env.VITE_APP_TITLE
  75. }),
  76. UnoCSS(),
  77. // sveltekit(),
  78. ],
  79. css: {
  80. preprocessorOptions: {
  81. less: {
  82. additionalData: '@import "./src/styles/variables.module.less";',
  83. javascriptEnabled: true
  84. }
  85. }
  86. },
  87. resolve: {
  88. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  89. alias: [
  90. {
  91. find: 'vue-i18n',
  92. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  93. },
  94. {
  95. find: /\@\//,
  96. replacement: `${pathResolve('src')}/`
  97. }
  98. ]
  99. },
  100. build: {
  101. minify: 'terser',
  102. outDir: env.VITE_OUT_DIR || 'dist',
  103. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  104. // brotliSize: false,
  105. terserOptions: {
  106. compress: {
  107. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  108. drop_console: env.VITE_DROP_CONSOLE === 'true'
  109. }
  110. }
  111. },
  112. server: {
  113. port: 4000,
  114. proxy: {
  115. // 选项写法
  116. '/api': {
  117. target: 'http://127.0.0.1:8000',
  118. changeOrigin: true,
  119. rewrite: path => path.replace(/^\/api/, '')
  120. }
  121. },
  122. hmr: {
  123. overlay: false
  124. },
  125. host: '0.0.0.0'
  126. },
  127. optimizeDeps: {
  128. include: [
  129. 'vue',
  130. 'vue-router',
  131. 'vue-types',
  132. 'element-plus/es/locale/lang/zh-cn',
  133. 'element-plus/es/locale/lang/en',
  134. '@iconify/iconify',
  135. '@vueuse/core',
  136. 'axios',
  137. 'qs',
  138. 'echarts',
  139. 'echarts-wordcloud',
  140. 'intro.js',
  141. 'qrcode',
  142. '@wangeditor/editor',
  143. '@wangeditor/editor-for-vue'
  144. ]
  145. }
  146. }
  147. }