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