vite.config.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import { loadEnv } from 'vite';
  3. import { resolve } from 'path';
  4. import { generateModifyVars } from './build/generate/generateModifyVars';
  5. import { createProxy } from './build/vite/proxy';
  6. import { wrapperEnv } from './build/utils';
  7. import { createVitePlugins } from './build/vite/plugin';
  8. import { OUTPUT_DIR } from './build/constant';
  9. import postcssPluginPx2rem from 'postcss-plugin-px2rem'; //引入插件
  10. import pkg from './package.json';
  11. import moment from 'moment';
  12. import autoprefixer from 'autoprefixer';
  13. import postCssPxToRem from 'postcss-pxtorem';
  14. function pathResolve(dir: string) {
  15. return resolve(process.cwd(), '.', dir);
  16. }
  17. const { dependencies, devDependencies, name, version } = pkg;
  18. const __APP_INFO__ = {
  19. pkg: { dependencies, devDependencies, name, version },
  20. lastBuildTime: moment().format('YYYY-MM-DD HH:mm:ss'),
  21. };
  22. //配置参数
  23. const px2remOptions = {
  24. rootValue: 54,
  25. // rootValue: 192, //换算基数, 默认100 ,也就是1440px ,这样的话把根标签的字体规定为1rem为50px,这样就可以从设计稿上量出多少个px直接在代码中写多少px了
  26. unitPrecision: 5, //允许REM单位增长到的十进制数字,其实就是精度控制
  27. // propWhiteList: [], // 默认值是一个空数组,这意味着禁用白名单并启用所有属性。
  28. propBlackList: ['headerLeft'], // 黑名单
  29. // exclude:false, //默认false,可以(reg)利用正则表达式排除某些文件夹的方法,例如/(node_module)/ 。如果想把前端UI框架内的px也转换成rem,请把此属性设为默认值
  30. // selectorBlackList: [], //要忽略并保留为px的选择器
  31. // ignoreIdentifier: false, //(boolean/string)忽略单个属性的方法,启用ignoreidentifier后,replace将自动设置为true。
  32. // replace: true, // (布尔值)替换包含REM的规则,而不是添加回退。
  33. mediaQuery: false, //(布尔值)允许在媒体查询中转换px
  34. minPixelValue: 0, //设置要替换的最小像素值(3px会被转rem)。 默认 0
  35. };
  36. export default ({ command, mode }: ConfigEnv): UserConfig => {
  37. const root = process.cwd();
  38. const env = loadEnv(mode, root);
  39. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  40. const viteEnv = wrapperEnv(env);
  41. const { VITE_PORT, VITE_GLOB_PUBLIC_PATH, VITE_PROXY, VITE_GLOB_DROP_CONSOLE } = viteEnv;
  42. const isBuild = command === 'build';
  43. return {
  44. base: VITE_GLOB_PUBLIC_PATH,
  45. root,
  46. resolve: {
  47. alias: [
  48. {
  49. find: 'vue-i18n',
  50. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  51. },
  52. // /@/xxxx => src/xxxx
  53. {
  54. find: /\/@\//,
  55. replacement: pathResolve('src') + '/',
  56. },
  57. // /#/xxxx => types/xxxx
  58. {
  59. find: /\/#\//,
  60. replacement: pathResolve('types') + '/',
  61. },
  62. // ['@vue/compiler-sfc', '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'],
  63. ],
  64. },
  65. // css: {
  66. // preprocessorOptions: {
  67. // },
  68. // },
  69. server: {
  70. // Listening on all local IPs
  71. host: true,
  72. port: VITE_PORT,
  73. // Load proxy configuration from .env
  74. proxy: createProxy(VITE_PROXY),
  75. },
  76. build: {
  77. target: 'es2020',
  78. outDir: OUTPUT_DIR,
  79. // minify: 'terser', // The minify mode is set to use terser to remove console
  80. terserOptions: {
  81. compress: {
  82. keep_infinity: true,
  83. // Used to delete console in production environment
  84. drop_console: VITE_GLOB_DROP_CONSOLE,
  85. },
  86. },
  87. // Turning off brotliSize display can slightly reduce packaging time
  88. brotliSize: false,
  89. chunkSizeWarningLimit: 2000,
  90. },
  91. define: {
  92. // setting vue-i18-next
  93. // Suppress warning
  94. __INTLIFY_PROD_DEVTOOLS__: false,
  95. __APP_INFO__: JSON.stringify(__APP_INFO__),
  96. 'process.env': {},
  97. },
  98. css: {
  99. preprocessorOptions: {
  100. sass: {
  101. additionalData: '@import "~@/styles/variables.scss";', // 导入Sass变量
  102. },
  103. less: {
  104. modifyVars: generateModifyVars(),
  105. javascriptEnabled: true,
  106. },
  107. },
  108. postcss: {
  109. // plugins: [
  110. // autoprefixer({
  111. // overrideBrowserslist: ['Android 4.1', 'iOS 7.1', 'Chrome > 31', 'ff > 31', 'ie >= 8'],
  112. // }),
  113. // postCssPxToRem({
  114. // // 自适应,px>rem转换
  115. // rootValue: 108, // 75表示750设计稿,37.5表示375设计稿
  116. // remUnit: 192,///设计图宽度/10
  117. // propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
  118. // selectorBlackList: ['norem'], // 过滤掉norem-开头的class,不进行rem转换
  119. // }),
  120. // ],
  121. plugins: [postcssPluginPx2rem(px2remOptions)],
  122. },
  123. },
  124. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  125. plugins: createVitePlugins(viteEnv, isBuild),
  126. optimizeDeps: {
  127. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  128. include: [
  129. '@iconify/iconify',
  130. 'ant-design-vue/es/locale/zh_CN',
  131. 'moment/dist/locale/zh-cn',
  132. 'ant-design-vue/es/locale/en_US',
  133. 'moment/dist/locale/eu',
  134. ],
  135. exclude: ['vue-demi', 'consolidate'],
  136. },
  137. }
  138. }