vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import eslint from 'vite-plugin-eslint'
  5. import viteCompression from 'vite-plugin-compression'
  6. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  7. import { resolve } from 'path'
  8. import fs from 'fs'
  9. import sizeOf from 'image-size'
  10. // 获取CAD图片大小
  11. const getImgSize = () => {
  12. try {
  13. const divs = resolve('./public/CAD_1')
  14. const fileName = fs.readdirSync(divs) as any
  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. console.log('断面图基础配置',obj);
  39. return obj
  40. } catch (error) {
  41. return { W: 0, H: 0, S: 0, M: 0 }
  42. }
  43. }
  44. // https://vitejs.dev/config/
  45. export default ({ mode }) => {
  46. const env = loadEnv(mode, process.cwd())
  47. return defineConfig({
  48. base: './',
  49. resolve: {
  50. alias: {
  51. /*
  52. 路径别名
  53. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  54. */
  55. '@': resolve(__dirname, './src')
  56. }
  57. },
  58. plugins: [
  59. vue(), viteCompression(),
  60. eslint({ fix: true, include: ['**/*.ts', '**/*.vue'] }),
  61. createSvgIconsPlugin({
  62. iconDirs: [resolve(__dirname, './src/assets/icons')],
  63. // Specify symbolId format
  64. symbolId: 'icon-[dir]-[name]'
  65. })
  66. ],
  67. server: {
  68. host: '0.0.0.0',
  69. port: 6888,
  70. open: true,
  71. strictPort: false,
  72. // proxy: {
  73. // '/api': {
  74. // target: env.VITE_PROXY_URL,
  75. // changeOrigin: true,
  76. // rewrite: (path) => path.replace(/^\/api/, ''),
  77. // secure: false,
  78. // headers: {
  79. // Referer: 'https://example.com'
  80. // }
  81. // }
  82. // }
  83. },
  84. esbuild: {
  85. drop: ['debugger'] // build 移除打印
  86. },
  87. build: {
  88. rollupOptions: {
  89. input: {
  90. index: resolve(__dirname, 'index.html')
  91. },
  92. output: { // 静态资源分类打包
  93. chunkFileNames: 'js/[hash].js',
  94. entryFileNames: 'js/[hash].js',
  95. assetFileNames: 'assets/[ext]/[hash].[ext]',
  96. // 拆分node_modules包
  97. manualChunks: (id: any) => {
  98. if (id.includes('node_modules')) {
  99. return id.toString().split('node_modules/')[1].split('/')[0].toString()
  100. }
  101. return ''
  102. }
  103. }
  104. }
  105. },
  106. define: {
  107. 'process.env': {
  108. imgSize: getImgSize()
  109. },
  110. __VUE_OPTIONS_API__: false
  111. }
  112. })
  113. }