vite.config.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (name === 'click-outside') {
  44. return ''
  45. }
  46. return `element-plus/es/components/${name.substring(3)}/style/css`
  47. }
  48. }]
  49. }),
  50. // EslintPlugin({
  51. // cache: false,
  52. // include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  53. // }),
  54. VueI18nPlugin({
  55. runtimeOnly: true,
  56. compositionOnly: true,
  57. include: [resolve(__dirname, 'src/locales/**')]
  58. }),
  59. createSvgIconsPlugin({
  60. iconDirs: [pathResolve('src/assets/svgs')],
  61. symbolId: 'icon-[dir]-[name]',
  62. svgoOptions: true
  63. }),
  64. PurgeIcons(),
  65. viteMockServe({
  66. ignore: /^\_/,
  67. mockPath: 'mock',
  68. localEnabled: !isBuild,
  69. prodEnabled: isBuild,
  70. injectCode: `
  71. import { setupProdMockServer } from '../mock/_createProductionServer'
  72. setupProdMockServer()
  73. `
  74. }),
  75. DefineOptions(),
  76. ViteEjsPlugin({
  77. title: env.VITE_APP_TITLE
  78. }),
  79. UnoCSS(),
  80. // sveltekit(),
  81. ],
  82. css: {
  83. preprocessorOptions: {
  84. less: {
  85. additionalData: '@import "./src/styles/variables.module.less";',
  86. javascriptEnabled: true
  87. }
  88. }
  89. },
  90. resolve: {
  91. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  92. alias: [
  93. {
  94. find: 'vue-i18n',
  95. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  96. },
  97. {
  98. find: /\@\//,
  99. replacement: `${pathResolve('src')}/`
  100. }
  101. ]
  102. },
  103. build: {
  104. minify: 'terser',
  105. outDir: env.VITE_OUT_DIR || 'dist',
  106. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  107. // brotliSize: false,
  108. terserOptions: {
  109. compress: {
  110. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  111. drop_console: env.VITE_DROP_CONSOLE === 'true'
  112. }
  113. }
  114. },
  115. server: {
  116. port: 4000,
  117. proxy: {
  118. // 选项写法
  119. '/api': {
  120. target: 'http://127.0.0.1:8000',
  121. changeOrigin: true,
  122. rewrite: path => path.replace(/^\/api/, '')
  123. }
  124. },
  125. hmr: {
  126. overlay: false
  127. },
  128. host: '0.0.0.0'
  129. },
  130. optimizeDeps: {
  131. include: [
  132. 'vue',
  133. 'vue-router',
  134. 'vue-types',
  135. 'element-plus/es/locale/lang/zh-cn',
  136. 'element-plus/es/locale/lang/en',
  137. '@iconify/iconify',
  138. '@vueuse/core',
  139. 'axios',
  140. 'qs',
  141. 'echarts',
  142. 'echarts-wordcloud',
  143. 'intro.js',
  144. 'qrcode',
  145. '@wangeditor/editor',
  146. '@wangeditor/editor-for-vue'
  147. ]
  148. }
  149. }
  150. }