Files
Folo/apps/desktop/plugins/vite/specific-import.ts
T
Innei 048f082776 refactor(desktop): Consolidate platform-specific components for desktop and electron
- Rename `.desktop.tsx` files to `.electron.tsx` for consistent naming
- Update import paths in main component files to use `.electron.tsx`
- Remove redundant files and consolidate platform-specific implementations
- Adjust import resolver for platform-specific components

Signed-off-by: Innei <tukon479@gmail.com>
2025-03-04 18:34:53 +08:00

63 lines
1.5 KiB
TypeScript

import type { Plugin } from "vite"
type Platform = "electron" | "web"
export function createPlatformSpecificImportPlugin(platform: Platform): Plugin {
return {
name: "platform-specific-import",
enforce: "pre",
async resolveId(source, importer) {
if (!importer) {
return null
}
const allowExts = [".js", ".jsx", ".ts", ".tsx"]
const sharedExts = [".desktop.ts", ".desktop.tsx", ".desktop.js", ".desktop.jsx"]
if (!allowExts.some((ext) => importer.endsWith(ext))) return null
if (importer.includes("node_modules")) return null
const [path, query] = source.split("?")
if (path.startsWith(".") || path.startsWith("/")) {
let priorities: string[] = []
switch (platform) {
case "electron": {
priorities = [
".electron.ts",
".electron.tsx",
".electron.js",
".electron.jsx",
...sharedExts,
]
break
}
case "web": {
priorities = [".web.ts", ".web.tsx", ".web.js", ".web.jsx", ...sharedExts]
break
}
// No default
}
for (const ext of priorities) {
const resolvedPath = await this.resolve(
`${path}${ext}${query ? `?${query}` : ""}`,
importer,
{
skipSelf: true,
},
)
if (resolvedPath) {
return resolvedPath.id
}
}
}
return null
},
}
}