82a19101a8
- 创建 rui-frontend 前端仓库 - 迁移 admin-ui 管理后台 - 创建 cashier-mobile 和 customer-mobile 占位项目 - 配置 pnpm workspace
126 lines
3.1 KiB
TypeScript
126 lines
3.1 KiB
TypeScript
import type { Plugin, ViteDevServer } from 'vite'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
import type { BuildConfig } from '../src/types/system-config'
|
|
|
|
/**
|
|
* 模块路由映射表
|
|
*/
|
|
const moduleRouteMap: Record<string, string> = {
|
|
system: "import { systemRoutes } from '@/router/modules/system'",
|
|
user: "import { userRoutes } from '@/router/modules/user'",
|
|
order: "import { orderRoutes } from '@/router/modules/order'",
|
|
cms: "import { cmsRoutes } from '@/router/modules/cms'",
|
|
marketing: "import { marketingRoutes } from '@/router/modules/marketing'",
|
|
demo: "import { demoRoutes } from '@/router/modules/demo'",
|
|
cashier: "import { cashierRoutes } from '@/router/modules/cashier'",
|
|
}
|
|
|
|
/**
|
|
* 生成路由代码
|
|
*/
|
|
function generateRoutesCode(config: BuildConfig): string {
|
|
const imports = config.modules
|
|
.map((module) => moduleRouteMap[module])
|
|
.filter(Boolean)
|
|
.join('\n')
|
|
|
|
const routeArrays = config.modules
|
|
.map((module) => {
|
|
const varName = '...' + module + 'Routes'
|
|
return varName
|
|
})
|
|
.join(', ')
|
|
|
|
return `
|
|
import { coreRoutes } from '@/router/modules/core'
|
|
${imports}
|
|
|
|
const routes = [
|
|
...coreRoutes.slice(0, 1), // login route
|
|
{
|
|
path: '/',
|
|
component: () => import('@/layout/DefaultLayout.vue'),
|
|
redirect: '/dashboard',
|
|
children: [
|
|
...coreRoutes[1].children, // dashboard, profile, settings
|
|
${routeArrays},
|
|
],
|
|
},
|
|
]
|
|
|
|
export default routes
|
|
`
|
|
}
|
|
|
|
/**
|
|
* 生成系统配置代码
|
|
*/
|
|
function generateConfigCode(config: BuildConfig): string {
|
|
return `export default ${JSON.stringify(config, null, 2)}`
|
|
}
|
|
|
|
/**
|
|
* Vite 模块构建插件
|
|
*/
|
|
export default function moduleBuildPlugin(): Plugin {
|
|
let config: BuildConfig | null = null
|
|
let systemKey = 'default'
|
|
|
|
return {
|
|
name: 'vite-plugin-module-build',
|
|
enforce: 'pre',
|
|
|
|
config(_config, { command }) {
|
|
// 从命令行参数读取系统标识
|
|
const args = process.argv
|
|
const systemArg = args.find((arg) => arg.startsWith('--system='))
|
|
if (systemArg) {
|
|
systemKey = systemArg.replace('--system=', '')
|
|
}
|
|
|
|
// 读取配置文件
|
|
const configPath = path.resolve(process.cwd(), 'build-config', `${systemKey}.json`)
|
|
if (!fs.existsSync(configPath)) {
|
|
throw new Error(`System config not found: ${configPath}`)
|
|
}
|
|
|
|
config = JSON.parse(fs.readFileSync(configPath, 'utf-8'))
|
|
|
|
// 修改构建输出目录
|
|
return {
|
|
build: {
|
|
outDir: `dist/${config.key}`,
|
|
},
|
|
}
|
|
},
|
|
|
|
resolveId(id) {
|
|
if (id === 'virtual:generated-routes' || id === 'virtual:system-config') {
|
|
return id
|
|
}
|
|
return null
|
|
},
|
|
|
|
load(id) {
|
|
if (!config) return null
|
|
|
|
if (id === 'virtual:generated-routes') {
|
|
return generateRoutesCode(config)
|
|
}
|
|
|
|
if (id === 'virtual:system-config') {
|
|
return generateConfigCode(config)
|
|
}
|
|
|
|
return null
|
|
},
|
|
|
|
configureServer(server: ViteDevServer) {
|
|
// 开发模式下,当配置文件变更时重启服务
|
|
const configPath = path.resolve(server.config.root, 'build-config', `${systemKey}.json`)
|
|
server.watcher.add(configPath)
|
|
},
|
|
}
|
|
}
|