// vite.config.js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { resolve } from 'path' import { readdirSync, writeFileSync } from 'fs' function createHTMLFiles() { const viewsDir = './src/views/admin' const vueFiles = readdirSync(viewsDir).filter(file => file.endsWith('.vue')) const input = {} vueFiles.forEach(file => { const pageName = file.replace('.vue', '') const htmlFileName = `${pageName}.html` // 在项目根目录创建 HTML 文件 const htmlContent = ` ${pageName}
` writeFileSync(htmlFileName, htmlContent) input[pageName] = resolve(__dirname, htmlFileName) }) return input } export default defineConfig({ plugins: [vue()], // 设置基础路径为相对路径 base: './', build: { rollupOptions: { input: { // 添加 index.html 作为默认页面 index: resolve(__dirname, 'index.html'), ...createHTMLFiles() } }, // 资源文件放在根目录(空字符串) assetsDir: '', // 清理输出目录 emptyOutDir: true }, preview: { port: 4173, host: true, // 设置默认打开的页面 open: '/landingManagement.html' } })