vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 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. viteCompression({
  44. verbose: true, // 是否在控制台中输出压缩结果
  45. disable: false, // 是否禁用压缩
  46. deleteOriginFile: false, // 压缩后是否删除源文件
  47. threshold: 5120, // 对大于 5KB 文件进行 gzip 压缩,单位Bytes
  48. algorithm: 'gzip', // 压缩算法,可选[‘gzip’,‘brotliCompress’,‘deflate’,‘deflateRaw’]
  49. ext: '.gz', // 生成的压缩包的后缀
  50. }),
  51. JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
  52. ],
  53. root: process.cwd(),
  54. resolve: { alias },
  55. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  56. optimizeDeps: { exclude: ['vue-demi'] },
  57. server: {
  58. host: '0.0.0.0',
  59. port: env.VITE_PORT as unknown as number,
  60. open: JSON.parse(env.VITE_OPEN),
  61. hmr: true,
  62. proxy: {
  63. '^/[Uu]pload': {
  64. target: env.VITE_API_URL,
  65. changeOrigin: true,
  66. },
  67. },
  68. },
  69. build: {
  70. outDir: 'dist',
  71. chunkSizeWarningLimit: 1500,
  72. assetsInlineLimit: 5000, // 小于此阈值的导入或引用资源将内联为 base64 编码
  73. sourcemap: false, // 构建后是否生成 source map 文件
  74. extractComments: false, // 移除注释
  75. minify: 'terser', // 启用后 terserOptions 配置才有效
  76. terserOptions: {
  77. compress: {
  78. drop_console: true, // 生产环境时移除console
  79. drop_debugger: true,
  80. },
  81. },
  82. rollupOptions: {
  83. output: {
  84. chunkFileNames: 'assets/js/[name]-[hash].js', // 引入文件名的名称
  85. entryFileNames: 'assets/js/[name]-[hash].js', // 包的入口文件名称
  86. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
  87. manualChunks(id) {
  88. if (id.includes('node_modules')) {
  89. return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
  90. }
  91. },
  92. },
  93. ...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
  94. },
  95. },
  96. css: { preprocessorOptions: { css: { charset: false } } },
  97. define: {
  98. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  99. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  100. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  101. __NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
  102. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  103. },
  104. };
  105. });
  106. export default viteConfig;