vite.config.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 PurgeIcons from 'vite-plugin-purge-icons'
  10. import ServerUrlCopy from 'vite-plugin-url-copy'
  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. import { visualizer } from 'rollup-plugin-visualizer'
  16. import viteCompression from 'vite-plugin-compression'
  17. // 自动引入组件
  18. import AutoImport from 'unplugin-auto-import/vite'
  19. import Components from 'unplugin-vue-components/vite'
  20. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  21. const root = process.cwd()
  22. function pathResolve(dir: string) {
  23. return resolve(root, '.', dir)
  24. }
  25. export default ({ command, mode }: ConfigEnv): UserConfig => {
  26. let env = {} as any
  27. const isBuild = command === 'build'
  28. if (!isBuild) {
  29. env = loadEnv(process.argv[3] === '--mode' ? process.argv[4] : process.argv[3], root)
  30. } else {
  31. env = loadEnv(mode, root)
  32. }
  33. return {
  34. base: env.VITE_BASE_PATH,
  35. plugins: [
  36. viteCompression(),
  37. Vue({
  38. script: {
  39. // 开启defineModel
  40. defineModel: true
  41. }
  42. }),
  43. // VueJsx(),
  44. VueJsx({ transformOn: true }), // 启用 Vue JSX 支持
  45. ServerUrlCopy(),
  46. progress(),
  47. env.VITE_USE_ALL_ELEMENT_PLUS_STYLE === 'false'
  48. ? createStyleImportPlugin({
  49. resolves: [ElementPlusResolve()],
  50. libs: [
  51. {
  52. libraryName: 'element-plus',
  53. esModule: true,
  54. resolveStyle: (name) => {
  55. if (name === 'click-outside') {
  56. return ''
  57. }
  58. return `element-plus/es/components/${name.replace(/^el-/, '')}/style/css`
  59. }
  60. }
  61. ]
  62. })
  63. : undefined,
  64. EslintPlugin({
  65. cache: false,
  66. failOnWarning: false,
  67. failOnError: false,
  68. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  69. }),
  70. VueI18nPlugin({
  71. runtimeOnly: true,
  72. compositionOnly: true,
  73. include: [resolve(__dirname, 'src/locales/**')]
  74. }),
  75. createSvgIconsPlugin({
  76. // iconDirs: [pathResolve('src/assets/svgs')],
  77. iconDirs: [resolve(__dirname,'../../','common/src/assets/svgs')],
  78. symbolId: 'icon-[dir]-[name]',
  79. svgoOptions: true
  80. }),
  81. PurgeIcons(),
  82. ViteEjsPlugin({
  83. title: env.VITE_APP_TITLE
  84. }),
  85. UnoCSS(),
  86. // 自动element-plus导入组件
  87. AutoImport({
  88. resolvers: [ElementPlusResolver()],
  89. }),
  90. Components({
  91. resolvers: [ElementPlusResolver()],
  92. })
  93. ],
  94. css: {
  95. preprocessorOptions: {
  96. less: {
  97. additionalData: '@import "@common/src/styles/variables.module.less";',
  98. javascriptEnabled: true
  99. }
  100. }
  101. },
  102. resolve: {
  103. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  104. alias: [
  105. {
  106. find: 'vue-i18n',
  107. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  108. },
  109. {
  110. find: /\@\//,
  111. replacement: `${pathResolve('src')}/`
  112. }
  113. ]
  114. },
  115. esbuild: {
  116. pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
  117. drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined
  118. },
  119. build: {
  120. target: 'es2015',
  121. outDir: env.VITE_OUT_DIR || 'dist',
  122. sourcemap: env.VITE_SOURCEMAP === 'true',
  123. // brotliSize: false,
  124. rollupOptions: {
  125. plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
  126. // 拆包
  127. output: {
  128. manualChunks: {
  129. 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
  130. 'element-plus': ['element-plus'],
  131. },
  132. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  133. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  134. assetFileNames: (assetInfo) => {
  135. const { name } = assetInfo;
  136. const extType = name?.split('.').at(-1) || '';
  137. if (/png|jpe?g|gif|webp|svg/.test(extType)) {
  138. return 'img/[name]-[hash].[ext]';
  139. } else if (/css/.test(extType)) {
  140. return 'css/[name]-[hash].[ext]';
  141. } else {
  142. return 'assets/[name]-[hash].[ext]';
  143. }
  144. }
  145. }
  146. },
  147. cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false'),
  148. cssTarget: ['chrome31']
  149. },
  150. server: {
  151. proxy: {
  152. '/openapi': {
  153. target: 'http://112.124.9.106:8000',
  154. changeOrigin: true
  155. }
  156. },
  157. hmr: {
  158. overlay: false
  159. },
  160. host: '0.0.0.0'
  161. },
  162. optimizeDeps: {
  163. include: [
  164. 'vue',
  165. 'vue-router',
  166. 'vue-types',
  167. 'element-plus/es/locale/lang/zh-cn',
  168. 'element-plus/es/locale/lang/en',
  169. '@iconify/iconify',
  170. '@vueuse/core',
  171. 'axios',
  172. 'qs',
  173. 'vue-json-pretty',
  174. '@zxcvbn-ts/core',
  175. 'dayjs',
  176. 'cropperjs',
  177. ]
  178. }
  179. }
  180. }