import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import viteCompression from 'vite-plugin-compression' import eslint from 'vite-plugin-eslint' import path from 'path' import fs from 'fs' const sizeOf = require('image-size') // 获取CAD图片大小 const getImgSize = function () { try { const divs = path.resolve('./public/CAD_1') const fileName = fs.readdirSync(divs) if (!fileName || !fileName.length) return {} const max = Math.max(...fileName) const filePath = `${divs}/${max}` const filesPath = fs.readdirSync(filePath) const maxName = [] for (let k = 0; k < filesPath.length; k++) { const el = filesPath[k] const id = el.indexOf('_') const num = el.substring(0, id) maxName.push({ num, name: el }) } maxName.sort((a, b) => +b.num - +a.num) const txt = `${divs}/${max}/${maxName[0].name}` const fileSize = sizeOf(txt) const obj = { W: fileSize.width * (+maxName[0].num+1), H: fileSize.height * max, S: fileSize.width, M: max } return obj } catch (error) { return {} } } export default ({ mode }) => defineConfig({ base: './', resolve: { alias: { /* 路径别名 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径 */ '@': path.resolve(__dirname, './src') } }, plugins: [ vue(), viteCompression({ disable: true }), eslint({ fix: true }) ], server: { host: 'localhost', port: 6547, open: true, strictPort: false, https: false // 反向代理 // proxy: { // '/': { // target: 'http://172.16.1.215:5000', // changeOrigin: true, // rewrite: (path) => path.replace(/^\//, '') // }, // } }, build: { rollupOptions: { input:{ index: path.resolve(__dirname, 'index.html') }, output: { // 静态资源分类打包 chunkFileNames: 'js/[name]-[hash].js', entryFileNames: 'js/[name]-[hash].js', assetFileNames: 'assets/[name]-[hash].[ext]' } }, terserOptions: { // 去掉打印 compress: { drop_console: true, drop_debugger: true } } }, define: { // 环境变量配置 'process.env': { SERVERURL: mode === 'development' ? 'http://127.0.0.1:5000' : 'http://127.0.0.1:5000', imgSize: getImgSize() } } })