vite.config.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. };
  13. const viteConfig = defineConfig((mode: ConfigEnv) => {
  14. const env = loadEnv(mode.mode, process.cwd());
  15. return {
  16. plugins: [vue(), vueSetupExtend(), viteCompression(), JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null],
  17. root: process.cwd(),
  18. resolve: { alias },
  19. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  20. optimizeDeps: {
  21. exclude: ['vue-demi'],
  22. },
  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. },
  36. },
  37. build: {
  38. outDir: 'dist',
  39. chunkSizeWarningLimit: 1500,
  40. rollupOptions: {
  41. output: {
  42. chunkFileNames: 'assets/js/[name]-[hash].js',
  43. entryFileNames: 'assets/js/[name]-[hash].js',
  44. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
  45. manualChunks(id) {
  46. if (id.includes('node_modules')) {
  47. return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
  48. }
  49. },
  50. },
  51. ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
  52. },
  53. },
  54. css: { preprocessorOptions: { css: { charset: false } } },
  55. define: {
  56. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  57. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  58. },
  59. };
  60. });
  61. export default viteConfig;