vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 compression from 'vite-plugin-compression2';
  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. import { visualizer } from 'rollup-plugin-visualizer';
  11. import { webUpdateNotice } from '@plugin-web-update-notification/vite';
  12. const pathResolve = (dir: string) => {
  13. return resolve(__dirname, '.', dir);
  14. };
  15. const alias: Record<string, string> = {
  16. '/@': pathResolve('./src/'),
  17. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  18. };
  19. const viteConfig = defineConfig((mode: ConfigEnv) => {
  20. const env = loadEnv(mode.mode, process.cwd());
  21. fs.writeFileSync('./public/config.js', `window.__env__ = ${JSON.stringify(env, null, 2)} `);
  22. return {
  23. plugins: [
  24. visualizer({ open: false }), // 开启可视化分析页面
  25. CodeInspectorPlugin({
  26. bundler: 'vite',
  27. hotKeys: ['shiftKey'],
  28. }),
  29. vue(),
  30. vueJsx(),
  31. webUpdateNotice({
  32. notificationConfig: {
  33. placement: 'topLeft',
  34. },
  35. notificationProps: {
  36. title: '📢 系统更新',
  37. description: '系统更新啦,请刷新页面!',
  38. buttonText: '刷新',
  39. dismissButtonText: '忽略',
  40. },
  41. }),
  42. vueSetupExtend(),
  43. compression({
  44. deleteOriginalAssets: false, // 是否删除源文件
  45. threshold: 5120, // 对大于 5KB 文件进行 gzip 压缩,单位Bytes
  46. skipIfLargerOrEqual: true, // 如果压缩后的文件大小等于或大于原始文件,则跳过压缩
  47. // algorithm: 'gzip', // 压缩算法,可选[‘gzip’,‘brotliCompress’,‘deflate’,‘deflateRaw’]
  48. // exclude: [/\.(br)$/, /\.(gz)$/], // 排除指定文件
  49. }),
  50. JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
  51. ],
  52. root: process.cwd(),
  53. resolve: { alias },
  54. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  55. optimizeDeps: { exclude: ['vue-demi'] },
  56. server: {
  57. host: '0.0.0.0',
  58. port: env.VITE_PORT as unknown as number,
  59. open: JSON.parse(env.VITE_OPEN),
  60. hmr: true,
  61. proxy: {
  62. '^/[Uu]pload': {
  63. target: env.VITE_API_URL,
  64. changeOrigin: true,
  65. },
  66. },
  67. },
  68. build: {
  69. outDir: 'dist',
  70. chunkSizeWarningLimit: 1500,
  71. assetsInlineLimit: 5000, // 小于此阈值的导入或引用资源将内联为 base64 编码
  72. sourcemap: false, // 构建后是否生成 source map 文件
  73. extractComments: false, // 移除注释
  74. minify: 'terser', // 启用后 terserOptions 配置才有效
  75. terserOptions: {
  76. compress: {
  77. drop_console: true, // 生产环境时移除console
  78. drop_debugger: true,
  79. },
  80. },
  81. rollupOptions: {
  82. output: {
  83. chunkFileNames: 'assets/js/[name]-[hash].js', // 引入文件名的名称
  84. entryFileNames: 'assets/js/[name]-[hash].js', // 包的入口文件名称
  85. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
  86. manualChunks(id) {
  87. if (id.includes('node_modules')) {
  88. return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
  89. }
  90. },
  91. },
  92. ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
  93. },
  94. },
  95. css: { preprocessorOptions: { css: { charset: false } } },
  96. define: {
  97. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  98. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  99. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  100. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  101. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  102. },
  103. };
  104. });
  105. export default viteConfig;