vite.config.ts 2.2 KB

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