vite.config.ts 3.9 KB

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