vite.config.ts 4.6 KB

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