Newer
Older
KaiFengPC / vite.config.js
@zhangdeliang zhangdeliang 25 days ago 4 KB update
  1. import { defineConfig, loadEnv } from 'vite';
  2. import path from 'path';
  3. import createVitePlugins from './vite/plugins';
  4. import postCssPxToRem from 'postcss-pxtorem';
  5. import autoprefixer from 'autoprefixer';
  6.  
  7. const Timestamp = new Date().getTime(); //随机时间戳
  8.  
  9. // const targetUrl = 'http://192.168.20.52:7300'; //杨辉
  10. const targetUrl = 'https://server2.wh-nf.cn:8082/prod-api'; //线上
  11.  
  12. // https://vitejs.dev/config/
  13. export default defineConfig(({ mode, command }) => {
  14. const env = loadEnv(mode, process.cwd());
  15. return {
  16. base: '/',
  17. // 是否开启eslint保存检测
  18. lintOnSave: false,
  19. plugins: createVitePlugins(env, command === 'build'),
  20. resolve: {
  21. alias: {
  22. // 设置路径
  23. '~': path.resolve(__dirname, './'),
  24. // 设置别名
  25. '@': path.resolve(__dirname, './src'),
  26. },
  27. // https://cn.vitejs.dev/config/#resolve-extensions
  28. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  29. },
  30. // vite 相关配置
  31. server: {
  32. port: 4319,
  33. host: true,
  34. hot: true, //自动保存
  35. cors: true,
  36. hmr: true, //vue3 vite配置热更新不用手动刷新 http://server1.wh-nf.cn:8023
  37. proxy: {
  38. '/business': {
  39. target: targetUrl,
  40. changeOrigin: true,
  41. rewrite: p => p.replace(/^\/business/, '/business/'),
  42. },
  43. '/publicService': {
  44. target: targetUrl,
  45. changeOrigin: true,
  46. rewrite: p => p.replace(/^\/publicService/, '/publicService'),
  47. },
  48. '/system': {
  49. target: targetUrl,
  50. changeOrigin: true,
  51. rewrite: p => p.replace(/^\/system/, '/system'),
  52. },
  53. '/auth': {
  54. target: targetUrl,
  55. changeOrigin: true,
  56. rewrite: p => p.replace(/^\/auth/, '/auth'),
  57. },
  58. '/code': {
  59. target: targetUrl,
  60. changeOrigin: true,
  61. rewrite: p => p.replace(/^\/code/, '/code'),
  62. },
  63. '/scada': {
  64. target: targetUrl,
  65. changeOrigin: true,
  66. rewrite: p => p.replace(/^\/scada/, '/scada'),
  67. },
  68. '/sponge': {
  69. target: targetUrl,
  70. changeOrigin: true,
  71. rewrite: path => path.replace(/^\/sponge/, '/sponge'),
  72. },
  73. '/ai': {
  74. target: targetUrl,
  75. changeOrigin: true,
  76. rewrite: p => p.replace(/^\/ai/, '/ai/'),
  77. },
  78. '/amap': {
  79. target: 'https://restapi.amap.com', //geocode服务
  80. changeOrigin: true,
  81. rewrite: p => p.replace(/^\/amap/, ''),
  82. },
  83. '/geoserver': {
  84. target: 'http://server1.wh-nf.cn:8023/geoserver', //geoserver服务
  85. changeOrigin: true,
  86. rewrite: path => path.replace(/^\/geoserver/, ''),
  87. },
  88. '/supermap1': {
  89. target: 'https://supermap1.wh-nf.cn:8090', //mapBox矢量地图
  90. changeOrigin: true,
  91. rewrite: path => path.replace(/^\/supermap1/, ''),
  92. },
  93. '/previewUrl': {
  94. target: 'https://server1.wh-nf.cn:8115', //文件预览地址
  95. changeOrigin: true,
  96. rewrite: path => path.replace(/^\/previewUrl/, ''),
  97. },
  98. },
  99. },
  100. css: {
  101. postcss: {
  102. plugins: [
  103. {
  104. postcssPlugin: 'internal:charset-removal',
  105. AtRule: {
  106. charset: atRule => {
  107. if (atRule.name === 'charset') {
  108. atRule.remove();
  109. }
  110. },
  111. },
  112. },
  113. postCssPxToRem({
  114. // 自适应,px>rem转换
  115. rootValue: 192, // 根据设计图尺寸写,设计图是 1920,就写 192
  116. propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
  117. selectorBlackList: ['-nopx'], // 过滤掉-nopx结尾的class,不进行rem转换
  118. }),
  119. autoprefixer({
  120. // 自动添加前缀
  121. overrideBrowserslist: [
  122. 'Android 4.1',
  123. 'iOS 7.1',
  124. 'Chrome > 31',
  125. 'ff > 31',
  126. 'ie >= 8',
  127. //'last 2 versions', // 所有主流浏览器最近2个版本
  128. ],
  129. grid: true,
  130. }),
  131. ],
  132. },
  133. },
  134. build: {
  135. sourcemap: false,
  136. outDir: 'KaiFengPC-dist',
  137. rollupOptions: {
  138. output: {
  139. chunkFileNames: `static/js/[name].[hash]${Timestamp}.js`,
  140. entryFileNames: `static/js/[name].[hash]${Timestamp}.js`,
  141. assetFileNames: `static/[ext]/[name].[hash]${Timestamp}.[ext]`,
  142. },
  143. brotliSize: true, // 不统计
  144. target: 'esnext',
  145. minify: 'esbuild', // 混淆器,terser构建后文件体积更小
  146. },
  147. },
  148. };
  149. });