vite.config.ts 3.3 KB

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