vite.config.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // @ts-ignore
  13. import postcssPxToViewport from 'postcss-px-to-viewport';
  14. const svg = createSvgIconsPlugin({
  15. // 要缓存的图标文件夹
  16. iconDirs: [path.resolve(__dirname, 'src/assets/svg')],
  17. // 执行 icon name 的格式
  18. symbolId: 'icon-[name]',
  19. });
  20. const { dependencies, devDependencies, name, version } = pkg;
  21. const __APP_INFO__ = {
  22. pkg: { dependencies, devDependencies, name, version },
  23. lastBuildTime: formatToDateTime(new Date()),
  24. };
  25. export default ({ command, mode }: ConfigEnv): UserConfig => {
  26. const root = process.cwd();
  27. const env = loadEnv(mode, root);
  28. const viteEnv = wrapperEnv(env);
  29. const { VITE_PORT, VITE_GLOB_PROD_MOCK } = viteEnv;
  30. const prodMock = VITE_GLOB_PROD_MOCK;
  31. const isBuild = command === 'build';
  32. return {
  33. base: './',
  34. publicDir: 'public',
  35. esbuild: {},
  36. resolve: {
  37. alias: {
  38. '@': resolve(__dirname, './src'),
  39. assets: resolve(__dirname, './src/assets'),
  40. views: resolve(__dirname, './src/views'),
  41. components: resolve(__dirname, './src/components'),
  42. },
  43. extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  44. },
  45. plugins: [
  46. createVitePlugins(viteEnv, isBuild, prodMock),
  47. svg,
  48. codeInspectorPlugin({
  49. bundler: 'vite',
  50. openIn: 'reuse',
  51. }),
  52. ],
  53. define: {
  54. __APP_INFO__: JSON.stringify(__APP_INFO__),
  55. },
  56. css: {
  57. devSourcemap: true,
  58. preprocessorOptions: {
  59. scss: {
  60. additionalData: `@use "@/styles/variables.scss" as *;`,
  61. },
  62. },
  63. postcss: {
  64. plugins: [
  65. postcssPxToViewport({
  66. unitToConvert: 'cpx', //自定义单位
  67. viewportWidth: 1920, //设计稿尺寸
  68. unitPrecision: 6, //单位转换后保留的精度
  69. propList: ['*'], //需要转换的属性列表
  70. viewportUnit: 'vw', //转换后的单位
  71. fontViewportUnit: 'vw', //字体转换后的单位
  72. selectorBlackList: ['ignore-'], //不需要转换的类名
  73. minPixelValue: 1, //最小转换的像素值
  74. mediaQuery: false, //是否在媒体查询中转换
  75. replace: true, //是否转换后直接更换属性值
  76. exclude: [/node_modules/], //忽略的文件
  77. }),
  78. ],
  79. },
  80. },
  81. server: {
  82. host: true,
  83. port: VITE_PORT,
  84. proxy: devProxy,
  85. },
  86. optimizeDeps: {
  87. include: ['dayjs', '@vicons/ionicons5', '@vicons/antd', '@element-plus/icons-vue'],
  88. exclude: [],
  89. },
  90. build: {
  91. target: 'es2015',
  92. outDir: OUTPUT_DIR,
  93. minify: 'terser',
  94. /**
  95. * 当 minify 为 minify 或 terser 打开注释
  96. */
  97. terserOptions: {
  98. compress: {
  99. keep_infinity: true,
  100. drop_console: true,
  101. drop_debugger: true,
  102. },
  103. },
  104. reportCompressedSize: false,
  105. chunkSizeWarningLimit: 2000,
  106. },
  107. };
  108. };