vite.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 DefineOptions from 'unplugin-vue-define-options/vite'
  13. import { createHtmlPlugin } from 'vite-plugin-html'
  14. // 当前执行node命令时文件夹的地址(工作目录)
  15. const root = process.cwd()
  16. // 路径查找
  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 = {} 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. root: root,
  32. // 服务端渲染
  33. server: {
  34. // 是否开启 https
  35. https: false,
  36. // 端口号
  37. port: env.VITE_PORT,
  38. host: "0.0.0.0",
  39. open: env.VITE_OPEN,
  40. // 本地跨域代理
  41. proxy: {
  42. ['/dev-api']: {
  43. target: env.VITE_BASE_URL,
  44. ws: false,
  45. changeOrigin: true,
  46. rewrite: (path) => path.replace(new RegExp(`^/dev-api`), ''),
  47. },
  48. },
  49. },
  50. plugins: [
  51. Vue(),
  52. VueJsx(),
  53. WindiCSS(),
  54. styleImport({
  55. resolves: [ElementPlusResolve()],
  56. libs: [{
  57. libraryName: 'element-plus',
  58. esModule: true,
  59. resolveStyle: (name) => {
  60. return `element-plus/es/components/${name.substring(3)}/style/css`
  61. }
  62. }]
  63. }),
  64. EslintPlugin({
  65. cache: false,
  66. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  67. }),
  68. VueI18n({
  69. runtimeOnly: true,
  70. compositionOnly: true,
  71. include: [resolve(__dirname, 'src/locales/**')]
  72. }),
  73. createSvgIconsPlugin({
  74. iconDirs: [pathResolve('src/assets/svgs')],
  75. symbolId: 'icon-[dir]-[name]',
  76. svgoOptions: true
  77. }),
  78. PurgeIcons(),
  79. DefineOptions(),
  80. createHtmlPlugin({
  81. inject: {
  82. data: {
  83. title: env.VITE_APP_TITLE,
  84. injectScript: `<script src="./inject.js"></script>`,
  85. }
  86. }
  87. })
  88. ],
  89. css: {
  90. preprocessorOptions: {
  91. less: {
  92. additionalData: '@import "./src/styles/variables.module.less";',
  93. javascriptEnabled: true
  94. }
  95. }
  96. },
  97. resolve: {
  98. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  99. alias: [
  100. {
  101. find: 'vue-i18n',
  102. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  103. },
  104. {
  105. find: /\@\//,
  106. replacement: `${pathResolve('src')}/`
  107. }
  108. ]
  109. },
  110. build: {
  111. minify: 'terser',
  112. outDir: env.VITE_OUT_DIR || 'dist',
  113. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  114. // brotliSize: false,
  115. terserOptions: {
  116. compress: {
  117. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  118. drop_console: env.VITE_DROP_CONSOLE === 'true'
  119. }
  120. }
  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. }