vite.config.ts 3.9 KB

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