vite.config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/svg')],
  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: 'public',
  34. esbuild: {},
  35. resolve: {
  36. alias: {
  37. '@': resolve(__dirname, './src'),
  38. assets: resolve(__dirname, './src/assets'),
  39. views: resolve(__dirname, './src/views'),
  40. components: resolve(__dirname, './src/components'),
  41. },
  42. extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  43. },
  44. plugins: [
  45. createVitePlugins(viteEnv, isBuild, prodMock),
  46. svg,
  47. codeInspectorPlugin({
  48. bundler: 'vite',
  49. openIn: 'reuse',
  50. }),
  51. ],
  52. define: {
  53. __APP_INFO__: JSON.stringify(__APP_INFO__),
  54. },
  55. css: {
  56. devSourcemap: true,
  57. preprocessorOptions: {
  58. scss: {
  59. additionalData: `@use "@/styles/variables.scss" as *;`,
  60. },
  61. },
  62. postcss: {
  63. plugins: [
  64. postcssPxToViewport({
  65. unitToConvert: 'cpx', //自定义单位
  66. viewportWidth: 1920, //设计稿尺寸
  67. unitPrecision: 6, //单位转换后保留的精度
  68. propList: ['*'], //需要转换的属性列表
  69. viewportUnit: 'vw', //转换后的单位
  70. fontViewportUnit: 'vw', //字体转换后的单位
  71. selectorBlackList: ['ignore-'], //不需要转换的类名
  72. minPixelValue: 1, //最小转换的像素值
  73. mediaQuery: false, //是否在媒体查询中转换
  74. replace: true, //是否转换后直接更换属性值
  75. exclude: [/node_modules/], //忽略的文件
  76. }),
  77. ],
  78. },
  79. },
  80. server: {
  81. host: true,
  82. port: VITE_PORT,
  83. },
  84. optimizeDeps: {
  85. include: ['dayjs', '@vicons/ionicons5', '@vicons/antd', '@element-plus/icons-vue'],
  86. exclude: [],
  87. },
  88. build: {
  89. target: 'es2015',
  90. outDir: OUTPUT_DIR,
  91. minify: 'terser',
  92. /**
  93. * 当 minify 为 minify 或 terser 打开注释
  94. */
  95. terserOptions: {
  96. compress: {
  97. keep_infinity: true,
  98. drop_console: true,
  99. drop_debugger: true,
  100. },
  101. },
  102. reportCompressedSize: false,
  103. chunkSizeWarningLimit: 2000,
  104. },
  105. };
  106. };