Files
Folo/apps/desktop/plugins/vite/specific-import.ts
T
DIYgodandDIYgod 8b07a9a573 chore: desktop dir (#2924)
* chore: desktop dir

* chore: desktop src and configs

* chore: auto-fix linting and formatting issues

* fix: path

* fix: path

* fix: path

* fix: path

* fix

* fix

* fix

* test

* test

* test

* test

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

---------

Co-authored-by: DIYgod <8266075+DIYgod@users.noreply.github.com>
2025-03-02 17:19:22 +08:00

42 lines
1.2 KiB
TypeScript

import type { Plugin } from "vite"
type Platform = "electron" | "web" | "rn"
export function createPlatformSpecificImportPlugin(platform: Platform): Plugin {
return {
name: "platform-specific-import",
enforce: "pre",
async resolveId(source, importer) {
const resolvedPath = await this.resolve(source, importer, {
skipSelf: true,
})
const allowExts = [".js", ".jsx", ".ts", ".tsx"]
if (
resolvedPath &&
!resolvedPath.id.includes("node_modules") &&
allowExts.some((ext) => importer?.endsWith(ext)) &&
allowExts.some((ext) => resolvedPath.id?.endsWith(ext))
) {
const lastDotIndex = resolvedPath.id.lastIndexOf(".")
const paths = [
`${resolvedPath.id.slice(0, lastDotIndex)}.${platform}${resolvedPath.id.slice(lastDotIndex)}`,
`${resolvedPath.id.slice(0, lastDotIndex)}.desktop${resolvedPath.id.slice(lastDotIndex)}`,
]
for (const path of paths) {
const resolvedPlatform = await this.resolve(path, importer, {
skipSelf: true,
})
if (resolvedPlatform) {
return resolvedPlatform.id
}
}
}
return null
},
}
}