|
| 1 | +import { fileURLToPath } from 'node:url' |
| 2 | +import path from 'node:path' |
| 3 | +import { existsSync, readdirSync } from 'node:fs' |
| 4 | +// @ts-expect-error: vite types may not be present at build time |
| 5 | +import type { Plugin } from 'vite' |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url) |
| 8 | +const __dirname = path.dirname(__filename) |
| 9 | +const root = path.resolve(__dirname, '..') |
| 10 | + |
| 11 | +// 自定义插件:处理 @opentiny/vue-theme 的子路径 CSS 导入 |
| 12 | +export function resolveOpentinyThemeCSS(): Plugin { |
| 13 | + return { |
| 14 | + name: 'resolve-opentiny-theme-css', |
| 15 | + resolveId(id) { |
| 16 | + // 处理 @opentiny/vue-theme/xxx/index.css 格式的导入 |
| 17 | + if (id.startsWith('@opentiny/vue-theme/') && id.endsWith('.css')) { |
| 18 | + // 解析到实际的 CSS 文件路径 |
| 19 | + const themePath = id.replace('@opentiny/vue-theme/', '') |
| 20 | + |
| 21 | + const directPath = path.resolve(root, 'node_modules/@opentiny/vue-theme', themePath) |
| 22 | + if (existsSync(directPath)) { |
| 23 | + return directPath |
| 24 | + } |
| 25 | + |
| 26 | + } |
| 27 | + return null |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// 处理静态资源路径解析,优先从子项目加载 |
| 33 | +export function resolveStaticAssets(): Plugin { |
| 34 | + const publicDirs = [ |
| 35 | + path.resolve(root, 'genui-sdk/sites/playground/web/public'), |
| 36 | + path.resolve(root, 'public'), |
| 37 | + ] |
| 38 | + |
| 39 | + return { |
| 40 | + name: 'resolve-static-assets', |
| 41 | + resolveId(id) { |
| 42 | + // 处理以 / 开头的绝对路径(静态资源) |
| 43 | + // 例如:/logo.svg, /images/xxx.png 等 |
| 44 | + if (id.startsWith('/') && !id.startsWith('//') && !id.startsWith('/@')) { |
| 45 | + // 移除开头的 /,获取相对路径 |
| 46 | + const relativePath = id.slice(1) |
| 47 | + |
| 48 | + // 在所有可能的 public 目录中查找(优先子项目) |
| 49 | + for (const publicDir of publicDirs) { |
| 50 | + if (existsSync(publicDir)) { |
| 51 | + const filePath = path.resolve(publicDir, relativePath) |
| 52 | + if (existsSync(filePath)) { |
| 53 | + // 返回实际文件路径,Vite 会处理它 |
| 54 | + return filePath |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return null |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
0 commit comments