vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. verbose: true, // 默认即可
  31. disable: false, // 开启压缩(不禁用),默认即可
  32. deleteOriginFile: false, // 删除源文件
  33. // 对所有大于 5KB 的文件进行 gzip 压缩
  34. threshold: 5120, // the unit is Bytes
  35. algorithm: 'gzip', // 压缩算法
  36. ext: '.gz', // 文件类型
  37. }),
  38. JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
  39. ],
  40. root: process.cwd(),
  41. resolve: { alias },
  42. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  43. optimizeDeps: { exclude: ['vue-demi'] },
  44. server: {
  45. host: '0.0.0.0',
  46. port: env.VITE_PORT as unknown as number,
  47. open: JSON.parse(env.VITE_OPEN),
  48. hmr: true,
  49. proxy: {
  50. '/gitee': {
  51. target: 'https://gitee.com',
  52. ws: true,
  53. changeOrigin: true,
  54. rewrite: (path) => path.replace(/^\/gitee/, ''),
  55. },
  56. '^/[Uu]pload': {
  57. target: env.VITE_API_URL,
  58. changeOrigin: true,
  59. },
  60. },
  61. },
  62. build: {
  63. outDir: 'dist',
  64. chunkSizeWarningLimit: 1500,
  65. assetsInlineLimit: 5000, // 小于此阈值的导入或引用资源将内联为 base64 编码
  66. sourcemap: false, // 构建后是否生成 source map 文件
  67. terserOptions: {
  68. compress: {
  69. //生产环境时移除console
  70. drop_console: true,
  71. drop_debugger: true,
  72. },
  73. },
  74. rollupOptions: {
  75. output: {
  76. chunkFileNames: 'assets/js/[name]-[hash].js',
  77. entryFileNames: 'assets/js/[name]-[hash].js',
  78. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
  79. manualChunks(id) {
  80. if (id.includes('node_modules')) {
  81. let newId = id.toString().replace('/.', '/');
  82. return newId.match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
  83. }
  84. },
  85. },
  86. ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
  87. },
  88. },
  89. css: { preprocessorOptions: { css: { charset: false } } },
  90. define: {
  91. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  92. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  93. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  94. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  95. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  96. },
  97. };
  98. });
  99. export default viteConfig;