vite.config.ts 4.1 KB

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