Better logic for tauri module imports

This commit is contained in:
Nicola Spadari
2024-08-04 11:45:18 +02:00
parent 37ba553d72
commit 93c55cccee

View File

@@ -9,12 +9,13 @@ const capitalize = (name: string) => {
return name.charAt(0).toUpperCase() + name.slice(1); return name.charAt(0).toUpperCase() + name.slice(1);
}; };
const buildImport = (moduleName: string, moduleNamespace: Record<string, any>, prefix: string) => { const tauriModules = [
Object.keys(moduleNamespace).filter((name) => name !== "default").forEach((name) => { { module: tauriApp, prefix: "App", importPath: "@tauri-apps/api/app" },
const as = prefix ? prefix + capitalize(name) : name; { module: tauriShell, prefix: "Shell", importPath: "@tauri-apps/plugin-shell" },
addImports({ from: `@tauri-apps/${moduleName}/${name}`, name, as }); { module: tauriOs, prefix: "Os", importPath: "@tauri-apps/plugin-os" },
}); { module: tauriNotification, prefix: "Notification", importPath: "@tauri-apps/plugin-notification" },
}; { module: tauriFs, prefix: "Fs", importPath: "@tauri-apps/plugin-fs" }
];
export default defineNuxtModule<ModuleOptions>({ export default defineNuxtModule<ModuleOptions>({
meta: { meta: {
@@ -25,10 +26,12 @@ export default defineNuxtModule<ModuleOptions>({
prefix: "useTauri" prefix: "useTauri"
}, },
setup(options) { setup(options) {
buildImport("api/app", tauriApp, `${options.prefix}App`); tauriModules.forEach(({ module, prefix, importPath }) => {
buildImport("plugin-shell", tauriShell, `${options.prefix}Shell`); Object.keys(module).filter((name) => name !== "default").forEach((name) => {
buildImport("plugin-os", tauriOs, `${options.prefix}Os`); const prefixedName = `${options.prefix}${prefix}` || "";
buildImport("plugin-notification", tauriNotification, `${options.prefix}Notification`); const as = prefixedName ? prefixedName + capitalize(name) : name;
buildImport("plugin-fs", tauriFs, `${options.prefix}Fs`); addImports({ from: importPath, name, as });
});
});
} }
}); });