vite.config.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import { loadEnv } from 'vite';
  3. import { resolve } from 'path';
  4. import { wrapperEnv } from './build/utils';
  5. import { createVitePlugins } from './build/vite/plugin';
  6. import { OUTPUT_DIR } from './build/constant';
  7. import { createProxy } from './build/vite/proxy';
  8. import pkg from './package.json';
  9. import { formatToDateTime } from './src/utils/dateUtil';
  10. const { dependencies, devDependencies, name, version } = pkg;
  11. const __APP_INFO__ = {
  12. pkg: { dependencies, devDependencies, name, version },
  13. lastBuildTime: formatToDateTime(new Date()),
  14. };
  15. function pathResolve(dir: string) {
  16. return resolve(process.cwd(), '.', dir);
  17. }
  18. export default ({ command, mode }: ConfigEnv): UserConfig => {
  19. const root = process.cwd();
  20. const env = loadEnv(mode, root);
  21. const viteEnv = wrapperEnv(env);
  22. const { VITE_PUBLIC_PATH, VITE_PORT, VITE_GLOB_PROD_MOCK, VITE_PROXY } = viteEnv;
  23. const prodMock = VITE_GLOB_PROD_MOCK;
  24. const isBuild = command === 'build';
  25. return {
  26. base: VITE_PUBLIC_PATH,
  27. esbuild: {},
  28. resolve: {
  29. alias: [
  30. {
  31. find: /\/#\//,
  32. replacement: pathResolve('types') + '/',
  33. },
  34. {
  35. find: '@',
  36. replacement: pathResolve('src') + '/',
  37. },
  38. ],
  39. dedupe: ['vue'],
  40. },
  41. plugins: createVitePlugins(viteEnv, isBuild, prodMock),
  42. define: {
  43. __APP_INFO__: JSON.stringify(__APP_INFO__),
  44. },
  45. css: {
  46. preprocessorOptions: {
  47. scss: {
  48. modifyVars: {},
  49. javascriptEnabled: true,
  50. additionalData: `@import "src/styles/var.scss";`,
  51. },
  52. },
  53. },
  54. server: {
  55. host: true,
  56. port: VITE_PORT,
  57. proxy: createProxy(VITE_PROXY),
  58. // proxy: {
  59. // '/api': {
  60. // target: '',
  61. // changeOrigin: true,
  62. // rewrite: (path) => path.replace(/^\/api/, '/api/v1')
  63. // }
  64. // }
  65. },
  66. optimizeDeps: {
  67. include: ['dayjs', '@vicons/ionicons5', '@vicons/antd', '@element-plus/icons-vue'],
  68. exclude: [],
  69. },
  70. build: {
  71. target: 'es2015',
  72. outDir: OUTPUT_DIR,
  73. // minify: 'terser',
  74. /**
  75. * 当 minify 为 minify 或 terser 打开注释
  76. */
  77. // terserOptions: {
  78. // compress: {
  79. // keep_infinity: true,
  80. // drop_console: VITE_DROP_CONSOLE,
  81. // },
  82. // },
  83. reportCompressedSize: false,
  84. chunkSizeWarningLimit: 2000,
  85. },
  86. };
  87. };