vite.config.js 3.1 KB

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