vite.config.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(), vueJsx(), vueSetupExtend(), viteCompression(), JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null],
  27. root: process.cwd(),
  28. resolve: { alias },
  29. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  30. optimizeDeps: { exclude: ['vue-demi'] },
  31. server: {
  32. host: '0.0.0.0',
  33. port: env.VITE_PORT as unknown as number,
  34. open: JSON.parse(env.VITE_OPEN),
  35. hmr: true,
  36. proxy: {
  37. '/gitee': {
  38. target: 'https://gitee.com',
  39. ws: true,
  40. changeOrigin: true,
  41. rewrite: (path) => path.replace(/^\/gitee/, ''),
  42. },
  43. '^/[Uu]pload': {
  44. target: env.VITE_API_URL,
  45. changeOrigin: true,
  46. }
  47. },
  48. },
  49. build: {
  50. outDir: 'dist',
  51. chunkSizeWarningLimit: 1500,
  52. rollupOptions: {
  53. output: {
  54. chunkFileNames: 'assets/js/[name]-[hash].js',
  55. entryFileNames: 'assets/js/[name]-[hash].js',
  56. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
  57. manualChunks(id) {
  58. if (id.includes('node_modules')) {
  59. return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
  60. }
  61. },
  62. },
  63. ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
  64. },
  65. },
  66. css: { preprocessorOptions: { css: { charset: false } } },
  67. define: {
  68. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  69. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  70. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  71. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  72. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  73. },
  74. };
  75. });
  76. export default viteConfig;