vite.config.ts 4.1 KB

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