vite.config.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-plus';
  5. import viteCompression from 'vite-plugin-compression';
  6. import { buildConfig } from './src/utils/build';
  7. import vueJsx from '@vitejs/plugin-vue-jsx'
  8. const pathResolve = (dir: string) => {
  9. return resolve(__dirname, '.', dir);
  10. };
  11. const alias: Record<string, string> = {
  12. '/@': pathResolve('./src/'),
  13. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  14. };
  15. const viteConfig = defineConfig((mode: ConfigEnv) => {
  16. const env = loadEnv(mode.mode, process.cwd());
  17. return {
  18. plugins: [vue(), vueJsx(), vueSetupExtend(), viteCompression(), JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null],
  19. root: process.cwd(),
  20. resolve: { alias },
  21. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  22. optimizeDeps: { exclude: ['vue-demi'] },
  23. server: {
  24. host: '0.0.0.0',
  25. port: env.VITE_PORT as unknown as number,
  26. open: JSON.parse(env.VITE_OPEN),
  27. hmr: true,
  28. proxy: {
  29. '/gitee': {
  30. target: 'https://gitee.com',
  31. ws: true,
  32. changeOrigin: true,
  33. rewrite: (path) => path.replace(/^\/gitee/, ''),
  34. },
  35. '^/[Uu]pload': {
  36. target: env.VITE_API_URL,
  37. changeOrigin: true,
  38. }
  39. },
  40. },
  41. build: {
  42. outDir: 'dist',
  43. chunkSizeWarningLimit: 1500,
  44. rollupOptions: {
  45. output: {
  46. chunkFileNames: 'assets/js/[name]-[hash].js',
  47. entryFileNames: 'assets/js/[name]-[hash].js',
  48. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
  49. manualChunks(id) {
  50. if (id.includes('node_modules')) {
  51. return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
  52. }
  53. },
  54. },
  55. ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
  56. },
  57. },
  58. css: { preprocessorOptions: { css: { charset: false } } },
  59. define: {
  60. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  61. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  62. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  63. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  64. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  65. },
  66. };
  67. });
  68. export default viteConfig;