vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. import { include, exclude } from './build/vite/optimize';
  12. function pathResolve(dir: string) {
  13. return resolve(process.cwd(), '.', dir);
  14. }
  15. const { dependencies, devDependencies, name, version } = pkg;
  16. const __APP_INFO__ = {
  17. pkg: { dependencies, devDependencies, name, version },
  18. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  19. };
  20. export default ({ command, mode }: ConfigEnv): UserConfig => {
  21. const root = process.cwd();
  22. const env = loadEnv(mode, root);
  23. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  24. const viteEnv = wrapperEnv(env);
  25. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
  26. const isBuild = command === 'build';
  27. return {
  28. base: VITE_PUBLIC_PATH,
  29. root,
  30. server: {
  31. https: false,
  32. // Listening on all local IPs
  33. host: true,
  34. port: VITE_PORT,
  35. // Load proxy configuration from .env
  36. proxy: createProxy(VITE_PROXY),
  37. },
  38. resolve: {
  39. alias: [
  40. {
  41. find: 'vue-i18n',
  42. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  43. },
  44. // /@/xxxx => src/xxxx
  45. {
  46. find: /\/@\//,
  47. replacement: pathResolve('src') + '/',
  48. },
  49. {
  50. find: /\/@bpmn\//,
  51. replacement: pathResolve('src') + '/views/workflow/design/bpmn/',
  52. },
  53. // /#/xxxx => types/xxxx
  54. {
  55. find: /\/#\//,
  56. replacement: pathResolve('types') + '/',
  57. },
  58. ],
  59. },
  60. esbuild: {
  61. drop: VITE_DROP_CONSOLE ? ['console', 'debugger'] : [],
  62. },
  63. build: {
  64. target: 'es2015',
  65. cssTarget: 'chrome80',
  66. outDir: OUTPUT_DIR,
  67. // minify: 'terser',
  68. /**
  69. * 当 minify=“minify:'terser'” 解开注释
  70. * Uncomment when minify="minify:'terser'"
  71. */
  72. // terserOptions: {
  73. // compress: {
  74. // keep_infinity: true,
  75. // drop_console: VITE_DROP_CONSOLE,
  76. // },
  77. // },
  78. // Turning off brotliSize display can slightly reduce packaging time
  79. reportCompressedSize: false,
  80. chunkSizeWarningLimit: 2000,
  81. },
  82. define: {
  83. __APP_INFO__: JSON.stringify(__APP_INFO__),
  84. },
  85. css: {
  86. preprocessorOptions: {
  87. less: {
  88. modifyVars: generateModifyVars(),
  89. javascriptEnabled: true,
  90. },
  91. },
  92. },
  93. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  94. plugins: createVitePlugins(viteEnv, isBuild),
  95. optimizeDeps: { include, exclude },
  96. };
  97. };