vite.config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. console.log('错误?',error);
  42. return { W: 0, H: 0, S: 0, M: 0 }
  43. }
  44. }
  45. // https://vitejs.dev/config/
  46. export default ({ mode }) => {
  47. const env = loadEnv(mode, process.cwd())
  48. return defineConfig({
  49. base: './',
  50. css: {
  51. preprocessorOptions: {
  52. scss: {
  53. api: 'modern-compiler'
  54. }
  55. }
  56. },
  57. resolve: {
  58. alias: {
  59. /*
  60. 路径别名
  61. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  62. */
  63. '@': resolve(__dirname, './src')
  64. }
  65. },
  66. plugins: [
  67. vue(), viteCompression(),
  68. eslint({ fix: true, include: ['**/*.ts', '**/*.vue'], cache: true }),
  69. createSvgIconsPlugin({
  70. iconDirs: [resolve(__dirname, './src/assets/icons')],
  71. symbolId: 'icon-[dir]-[name]'
  72. })
  73. ],
  74. server: {
  75. host: '0.0.0.0',
  76. port: 6888,
  77. open: true,
  78. strictPort: false,
  79. // proxy: {
  80. // '/api': {
  81. // target: env.VITE_PROXY_URL,
  82. // changeOrigin: true,
  83. // rewrite: (path) => path.replace(/^\/api/, ''),
  84. // secure: false,
  85. // headers: {
  86. // Referer: 'https://example.com'
  87. // }
  88. // }
  89. // }
  90. },
  91. esbuild: {
  92. drop: ['debugger'] // build 移除打印
  93. },
  94. build: {
  95. rollupOptions: {
  96. input: {
  97. index: resolve(__dirname, 'index.html')
  98. },
  99. output: { // 静态资源分类打包
  100. chunkFileNames: 'js/[hash].js',
  101. entryFileNames: 'js/[hash].js',
  102. assetFileNames: 'assets/[ext]/[hash].[ext]',
  103. // 拆分node_modules包
  104. manualChunks: (id: any) => {
  105. if (id.includes('node_modules')) {
  106. return id.toString().split('node_modules/')[1].split('/')[0].toString()
  107. }
  108. return ''
  109. }
  110. }
  111. }
  112. },
  113. define: {
  114. 'process.env': {
  115. imgSize: getImgSize()
  116. },
  117. __VUE_OPTIONS_API__: false
  118. }
  119. })
  120. }