vite.config.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import pkg from './package.json';
  3. import dayjs from 'dayjs';
  4. import { loadEnv } from 'vite';
  5. import { resolve } from 'path';
  6. import { generateModifyVars } from './build/generate/generateModifyVars';
  7. import { createProxy } from './build/vite/proxy';
  8. import { wrapperEnv } from './build/utils';
  9. import { createVitePlugins } from './build/vite/plugin';
  10. import { OUTPUT_DIR } from './build/constant';
  11. function pathResolve(dir: string) {
  12. return resolve(process.cwd(), '.', dir);
  13. }
  14. const { dependencies, devDependencies, name, version } = pkg;
  15. const __APP_INFO__ = {
  16. pkg: { dependencies, devDependencies, name, version },
  17. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  18. };
  19. export default async ({ command, mode }: ConfigEnv): Promise<UserConfig> => {
  20. const root = process.cwd();
  21. const env = loadEnv(mode, root);
  22. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  23. const viteEnv = wrapperEnv(env);
  24. const { VITE_PUBLIC_PATH, VITE_PROXY } = viteEnv;
  25. const isBuild = command === 'build';
  26. return {
  27. base: VITE_PUBLIC_PATH,
  28. root,
  29. resolve: {
  30. alias: [
  31. {
  32. find: 'vue-i18n',
  33. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  34. },
  35. // /@/xxxx => src/xxxx
  36. {
  37. find: /\/@\//,
  38. replacement: pathResolve('src') + '/',
  39. },
  40. // /#/xxxx => types/xxxx
  41. {
  42. find: /\/#\//,
  43. replacement: pathResolve('types') + '/',
  44. },
  45. ],
  46. },
  47. server: {
  48. host: true,
  49. // Load proxy configuration from .env
  50. proxy: createProxy(VITE_PROXY),
  51. },
  52. esbuild: {
  53. drop: ['console', 'debugger'],
  54. },
  55. build: {
  56. target: 'es2015',
  57. cssTarget: 'chrome80',
  58. outDir: OUTPUT_DIR,
  59. reportCompressedSize: false,
  60. chunkSizeWarningLimit: 2000,
  61. },
  62. define: {
  63. __APP_INFO__: JSON.stringify(__APP_INFO__),
  64. },
  65. css: {
  66. preprocessorOptions: {
  67. less: {
  68. modifyVars: generateModifyVars(),
  69. javascriptEnabled: true,
  70. },
  71. },
  72. },
  73. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  74. plugins: await createVitePlugins(viteEnv, isBuild),
  75. optimizeDeps: {
  76. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  77. include: [
  78. '@iconify/iconify',
  79. 'ant-design-vue/es/locale/zh_CN',
  80. 'ant-design-vue/es/locale/en_US',
  81. ],
  82. },
  83. };
  84. };