vite.config.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: {
  22. include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en', 'element-plus/lib/locale/lang/zh-tw'],
  23. exclude: ['vue-demi'],
  24. },
  25. server: {
  26. host: '0.0.0.0',
  27. port: env.VITE_PORT as unknown as number,
  28. open: JSON.parse(env.VITE_OPEN),
  29. hmr: true,
  30. proxy: {
  31. '/gitee': {
  32. target: 'https://gitee.com',
  33. ws: true,
  34. changeOrigin: true,
  35. rewrite: (path) => path.replace(/^\/gitee/, ''),
  36. },
  37. },
  38. },
  39. build: {
  40. outDir: 'dist',
  41. chunkSizeWarningLimit: 1500,
  42. rollupOptions: {
  43. output: {
  44. chunkFileNames: 'assets/js/[name]-[hash].js',
  45. entryFileNames: 'assets/js/[name]-[hash].js',
  46. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
  47. manualChunks(id) {
  48. if (id.includes('node_modules')) {
  49. return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups.moduleName ?? 'vender';
  50. }
  51. },
  52. },
  53. ...(JSON.parse(env.VITE_OPEN_CDN) ? {external: buildConfig.external} : {}),
  54. },
  55. },
  56. css: { preprocessorOptions: { css: { charset: false } } },
  57. define: {
  58. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  59. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  60. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  61. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  62. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  63. },
  64. };
  65. });
  66. export default viteConfig;