vite.config.ts 2.9 KB

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