vite.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import eslint from 'vite-plugin-eslint'
  4. import viteCompression from 'vite-plugin-compression'
  5. import path from 'path'
  6. import fs from 'fs'
  7. import sizeOf from 'image-size'
  8. // 获取CAD图片大小
  9. const getImgSize = ()=> {
  10. try {
  11. const divs = path.resolve('./public/CAD_1')
  12. const fileName = fs.readdirSync(divs) as any
  13. if (!fileName || !fileName.length) return {}
  14. const max = Math.max(...fileName)
  15. const filePath = `${divs}/${max}`
  16. const filesPath = fs.readdirSync(filePath)
  17. const maxName = []
  18. for (let k = 0; k < filesPath.length; k++) {
  19. const el = filesPath[k]
  20. const id = el.indexOf('_')
  21. const num = el.substring(0, id)
  22. maxName.push({
  23. num,
  24. name: el
  25. })
  26. }
  27. maxName.sort((a, b) => +b.num - +a.num)
  28. const txt = `${divs}/${max}/${maxName[0].name}`
  29. const fileSize = sizeOf(txt)
  30. const obj = {
  31. W: fileSize.width * (+maxName[0].num + 1),
  32. H: fileSize.height * max,
  33. S: fileSize.width,
  34. M: max
  35. }
  36. return obj
  37. } catch (error) {
  38. return {}
  39. }
  40. }
  41. // https://vitejs.dev/config/
  42. export default defineConfig({
  43. base: './',
  44. resolve: {
  45. alias: {
  46. /*
  47. 路径别名
  48. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  49. */
  50. '@': path.resolve(__dirname, './src')
  51. }
  52. },
  53. plugins: [vue(), viteCompression(), eslint({ fix: true, include: ['**/*.ts', '**/*.vue'] })],
  54. server: {
  55. host: '0.0.0.0',
  56. port: 8585,
  57. open: true,
  58. strictPort: false,
  59. https: false
  60. },
  61. esbuild: {
  62. drop: ['console', 'debugger'] //build 移除打印
  63. },
  64. build: {
  65. rollupOptions: {
  66. input: {
  67. index: path.resolve(__dirname, 'index.html')
  68. },
  69. output: { // 静态资源分类打包
  70. chunkFileNames: 'js/[hash].js',
  71. entryFileNames: 'js/[hash].js',
  72. assetFileNames: 'assets/[ext]/[hash].[ext]',
  73. // 拆分node_modules包
  74. manualChunks: (id: any) => {
  75. if (id.includes("node_modules")) {
  76. return id.toString().split("node_modules/")[1].split("/")[0].toString()
  77. }
  78. }
  79. }
  80. }
  81. },
  82. define: {
  83. 'process.env': {
  84. imgSize: getImgSize()
  85. },
  86. __VUE_OPTIONS_API__: true
  87. }
  88. })