vite.config.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. defineConfig
  3. } from 'vite'
  4. import vue from '@vitejs/plugin-vue'
  5. import viteCompression from 'vite-plugin-compression'
  6. import eslint from 'vite-plugin-eslint'
  7. import path from 'path'
  8. import fs from 'fs'
  9. const sizeOf = require('image-size')
  10. // 获取CAD图片大小
  11. const getImgSize = function () {
  12. try {
  13. const divs = path.resolve('./public/CAD_1')
  14. const fileName = fs.readdirSync(divs)
  15. if (!fileName || !fileName.length) return {}
  16. const max = Math.max(...fileName)
  17. const filePath = `${divs}/${max}`
  18. const filesPath = fs.readdirSync(filePath)
  19. const maxName = []
  20. for (let k = 0; k < filesPath.length; k++) {
  21. const el = filesPath[k]
  22. const id = el.indexOf('_')
  23. const num = el.substring(0, id)
  24. maxName.push({
  25. num,
  26. name: el
  27. })
  28. }
  29. maxName.sort((a, b) => +b.num - +a.num)
  30. const txt = `${divs}/${max}/${maxName[0].name}`
  31. const fileSize = sizeOf(txt)
  32. const obj = {
  33. W: fileSize.width * (+maxName[0].num+1),
  34. H: fileSize.height * max,
  35. S: fileSize.width,
  36. M: max
  37. }
  38. return obj
  39. } catch (error) {
  40. return {}
  41. }
  42. }
  43. export default ({
  44. mode
  45. }) => defineConfig({
  46. base: './',
  47. resolve: {
  48. alias: {
  49. /*
  50. 路径别名
  51. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  52. */
  53. '@': path.resolve(__dirname, './src')
  54. }
  55. },
  56. plugins: [ vue(), viteCompression({
  57. disable: true
  58. }), eslint({
  59. fix: true
  60. }) ],
  61. server: {
  62. host: 'localhost',
  63. port: 6547,
  64. open: true,
  65. strictPort: false,
  66. https: false
  67. // 反向代理
  68. // proxy: {
  69. // '/': {
  70. // target: 'http://172.16.1.215:5000',
  71. // changeOrigin: true,
  72. // rewrite: (path) => path.replace(/^\//, '')
  73. // },
  74. // }
  75. },
  76. build: {
  77. rollupOptions: {
  78. input:{
  79. index: path.resolve(__dirname, 'index.html')
  80. },
  81. output: { // 静态资源分类打包
  82. chunkFileNames: 'js/[name]-[hash].js',
  83. entryFileNames: 'js/[name]-[hash].js',
  84. assetFileNames: 'assets/[name]-[hash].[ext]'
  85. }
  86. },
  87. terserOptions: { // 去掉打印
  88. compress: {
  89. drop_console: true,
  90. drop_debugger: true
  91. }
  92. }
  93. },
  94. define: { // 环境变量配置
  95. 'process.env': {
  96. SERVERURL: mode === 'development' ? 'http://127.0.0.1:5000' : 'http://127.0.0.1:5000',
  97. imgSize: getImgSize()
  98. }
  99. }
  100. })