vite.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 glob from 'glob'
  9. import fs, {
  10. fstat
  11. } from 'fs'
  12. const sizeOf = require('image-size')
  13. // 多页面入口配置
  14. const entryConfig = function () {
  15. const viteconfig = {}
  16. // 含有.js的文件
  17. const entryFiles = glob.sync('./src/pages/*/*.js')
  18. // 获取模板
  19. const temp = fs.readFileSync('./index.html')
  20. // 模板增加ts入口
  21. entryFiles.forEach((entry) => {
  22. const projectName = entry.split('/')[3]
  23. try {
  24. fs.accessSync(`./${projectName}.html`)
  25. } catch (error) {
  26. // 重构模板
  27. const html = temp.toString().replace(/\.\/src\/[^\s]*/g, `${entry}"></script>`)
  28. fs.writeFile(`./${projectName}.html`, html, (err) => {
  29. if (err) console.error(err)
  30. })
  31. }
  32. viteconfig[projectName] = path.resolve(__dirname, `./${projectName}.html`)
  33. })
  34. return viteconfig
  35. }
  36. // 获取CAD图片大小
  37. const getImgSize = function () {
  38. try {
  39. const divs = path.resolve('./public/CAD_1')
  40. const fileName = fs.readdirSync(divs)
  41. if (!fileName || !fileName.length) return {}
  42. const max = Math.max(...fileName)
  43. const filePath = `${divs}/${max}`
  44. const filesPath = fs.readdirSync(filePath)
  45. const maxName = []
  46. for (let k = 0; k < filesPath.length; k++) {
  47. const el = filesPath[k]
  48. const id = el.indexOf('_')
  49. const num = el.substring(0, id)
  50. maxName.push({
  51. num,
  52. name: el
  53. })
  54. }
  55. maxName.sort((a, b) => +b.num - +a.num)
  56. const txt = `${divs}/${max}/${maxName[0].name}`
  57. const fileSize = sizeOf(txt)
  58. const obj = {
  59. W: fileSize.width * (+maxName[0].num+1),
  60. H: fileSize.height * max,
  61. S: fileSize.width,
  62. M: max
  63. }
  64. return obj
  65. } catch (error) {
  66. return {}
  67. }
  68. }
  69. export default ({
  70. mode
  71. }) => defineConfig({
  72. base: './',
  73. resolve: {
  74. alias: {
  75. /*
  76. 路径别名
  77. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  78. */
  79. '@': path.resolve(__dirname, './src')
  80. }
  81. },
  82. plugins: [ vue(), viteCompression({
  83. disable: true
  84. }), eslint({
  85. fix: true
  86. }) ],
  87. server: {
  88. host: 'localhost',
  89. port: 6547,
  90. open: true,
  91. strictPort: false,
  92. https: false
  93. // 反向代理
  94. // proxy: {
  95. // '/': {
  96. // target: 'http://172.16.1.215:5000',
  97. // changeOrigin: true,
  98. // rewrite: (path) => path.replace(/^\//, '')
  99. // },
  100. // }
  101. },
  102. build: {
  103. rollupOptions: {
  104. input: entryConfig(),
  105. output: { // 静态资源分类打包
  106. chunkFileNames: 'js/[name]-[hash].js',
  107. entryFileNames: 'js/[name]-[hash].js',
  108. assetFileNames: 'assets/[name]-[hash].[ext]'
  109. }
  110. },
  111. terserOptions: { // 去掉打印
  112. compress: {
  113. drop_console: true,
  114. drop_debugger: true
  115. }
  116. }
  117. },
  118. define: { // 环境变量配置
  119. 'process.env': {
  120. SERVERURL: mode === 'development' ? 'http://127.0.0.1:5000' : 'http://127.0.0.1:5000',
  121. imgSize: getImgSize()
  122. }
  123. }
  124. })