| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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 glob from 'glob'
- import fs, { fstat } from 'fs'
- const sizeOf = require('image-size');
- // 多页面入口配置
- const entryConfig = function () {
- const viteconfig = {}
- // 含有.js的文件
- const entryFiles = glob.sync('./src/pages/*/*.js')
- // 获取模板
- const temp = fs.readFileSync('./index.html')
- // 模板增加ts入口
- entryFiles.forEach((entry) => {
- const projectName = entry.split('/')[3]
- try {
- fs.accessSync(`./${projectName}.html`)
- } catch (error) {
- // 重构模板
- const html = temp.toString().replace(/\.\/src\/[^\s]*/g, `${entry}"></script>`)
- fs.writeFile(`./${projectName}.html`, html, (err) => {
- if (err) console.error(err)
- })
- }
- viteconfig[projectName] = path.resolve(__dirname, `./${projectName}.html`)
- })
- return viteconfig
- }
- // 获取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)
- let 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: num, name: el })
- }
- maxName.sort((a, b) => {
- return +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: entryConfig(),
- 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()
- }
- }
- })
|