vite.config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import vue from '@vitejs/plugin-vue';
  2. import {resolve} from 'path';
  3. import {defineConfig, loadEnv, ConfigEnv} from 'vite';
  4. import vueSetupExtend from 'vite-plugin-vue-setup-extend';
  5. import legacy from '@vitejs/plugin-legacy';
  6. // @ts-ignore
  7. import AutoImport from 'unplugin-auto-import/vite'
  8. // @ts-ignore
  9. import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
  10. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  11. const pathResolve = (dir: string) => {
  12. return resolve(__dirname, '.', dir);
  13. };
  14. const alias: Record<string, string> = {
  15. '/@': pathResolve('./src/'),
  16. '@': pathResolve('./src'),
  17. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  18. };
  19. // @ts-ignore
  20. import config from "./src/utils/config.js"
  21. import * as path from "path";
  22. let prefixKeys = Object.keys(config).filter(i => i.includes('Prefix'))
  23. let proxyObj = prefixKeys.reduce((r, i) => {
  24. let prefix = config[i]
  25. r[prefix] = {
  26. target: config.proxyLocation + prefix,
  27. ws: true,
  28. changeOrigin: true,
  29. rewrite: (path: string) => path.replace(new RegExp('^' + prefix), ''),
  30. timeout: 10000000,
  31. }
  32. return r
  33. }, {})
  34. // @ts-ignore
  35. const viteConfig = defineConfig((mode: ConfigEnv) => {
  36. const env = loadEnv(mode.mode, process.cwd());
  37. return {
  38. plugins: [
  39. vue(),
  40. legacy({
  41. targets: ['since 2015'],
  42. additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // regenerator-runtime/runtime @dian/polyfill
  43. renderLegacyChunks: true,
  44. polyfills: [
  45. 'es.symbol',
  46. 'es.array.filter',
  47. 'es.promise',
  48. 'es.promise.finally',
  49. 'es/map',
  50. 'es/set',
  51. 'es.array.for-each',
  52. 'es.object.define-properties',
  53. 'es.object.define-property',
  54. 'es.object.get-own-property-descriptor',
  55. 'es.object.get-own-property-descriptors',
  56. 'es.object.keys',
  57. 'es.object.to-string',
  58. 'web.dom-collections.for-each',
  59. 'esnext.global-this',
  60. 'esnext.string.match-all',
  61. 'es.object.entries',
  62. 'es.object.from-entries',
  63. 'esnext.object.iterate-entries',
  64. ],
  65. modernPolyfills: ['es.string.replace-all'],
  66. }),
  67. vueSetupExtend(),
  68. AutoImport({
  69. imports: ['vue', 'vue-router', 'pinia'],
  70. resolvers: [
  71. ElementPlusResolver({
  72. importStyle: 'sass'
  73. })
  74. ],
  75. dts: false
  76. }),
  77. createSvgIconsPlugin({
  78. iconDirs: [path.resolve(process.cwd(), "src/assets/svg-icons")],//svg文件存放的路径
  79. symbolId: "icon-[name]",
  80. }),
  81. ],
  82. root: process.cwd(),
  83. resolve: {alias, extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']},
  84. base: mode.mode === 'development' ? '/' : '/mcp',
  85. optimizeDeps: {
  86. include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en', 'element-plus/lib/locale/lang/zh-tw', 'form-data', 'follow-redirects'],
  87. },
  88. server: {
  89. host: 'mcp.9n1m.com', // 真机模拟,使用
  90. // host: '127.0.0.1', // 真机模拟,使用
  91. port: 80,
  92. open: env.VITE_OPEN,
  93. hmr: true,
  94. proxy: proxyObj
  95. },
  96. build: {
  97. minify: 'terser',
  98. terserOptions: {
  99. compress: {
  100. drop_console: true,
  101. drop_debugger: true,
  102. },
  103. },
  104. outDir: 'dist',
  105. chunkSizeWarningLimit: 1500,
  106. rollupOptions: {
  107. output: {
  108. entryFileNames: `assets/[name].[hash].js`,
  109. chunkFileNames: `assets/[name].[hash].js`,
  110. assetFileNames: `assets/[name].[hash].[ext]`,
  111. compact: true,
  112. manualChunks: {
  113. vue: ['vue', 'vue-router', 'pinia'],
  114. echarts: ['echarts'],
  115. },
  116. },
  117. },
  118. },
  119. css: {
  120. preprocessorOptions: {
  121. css: {charset: false},
  122. preprocessorOptions: {
  123. scss: {
  124. additionalData: `@use "@/assets/styles/element/index.scss" as *;`,
  125. },
  126. },
  127. }
  128. },
  129. define: {
  130. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  131. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  132. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  133. __VERSION__: JSON.stringify(process.env.npm_package_version),
  134. },
  135. };
  136. });
  137. export default viteConfig;