vite.config.ts 4.0 KB

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