vite.config.ts 2.5 KB

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