From 21e6e658db5d913585e28d5db84b24e7ad106622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gr=C3=B8ngaard?= Date: Tue, 21 May 2024 16:06:48 +0200 Subject: [PATCH] refactor: improve internal types (#163) * refactor: remove unreachable code * refactor: improve internal types I had a little bit of trouble understanding what options/configs were passed where and why, so now I've more clearly separated the client config options and the `studioBasePath` option. I've also adjusted the return types of the Vite Plugin factory functions to make TypeScript happy. --- packages/sanity-astro/src/index.ts | 32 ++++++++----------- .../src/vite-plugin-sanity-client.ts | 7 ++-- .../src/vite-plugin-sanity-studio.ts | 10 +++--- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/packages/sanity-astro/src/index.ts b/packages/sanity-astro/src/index.ts index 54f462ce..2d158418 100644 --- a/packages/sanity-astro/src/index.ts +++ b/packages/sanity-astro/src/index.ts @@ -7,41 +7,35 @@ type IntegrationOptions = ClientConfig & { studioBasePath?: string; }; -const defaultOptions: IntegrationOptions = { +const defaultClientConfig: ClientConfig = { apiVersion: "v2023-08-24", }; -export default function sanityIntegration( - options: IntegrationOptions, -): AstroIntegration { - const resolvedOptions = { - ...defaultOptions, - ...options, - }; +export default function sanityIntegration({ + studioBasePath, + ...clientConfig +}: IntegrationOptions): AstroIntegration { return { name: "@sanity/astro", hooks: { - "astro:config:setup": ({ - injectScript, - injectRoute, - updateConfig, - config, - logger, - }) => { + "astro:config:setup": ({ injectScript, injectRoute, updateConfig }) => { updateConfig({ vite: { plugins: [ - vitePluginSanityClient(resolvedOptions), - vitePluginSanityStudio(resolvedOptions, config), + vitePluginSanityClient({ + ...defaultClientConfig, + ...clientConfig, + }), + vitePluginSanityStudio({ studioBasePath }), ], }, }); // only load this route if `studioBasePath` is set - if (resolvedOptions.studioBasePath) { + if (studioBasePath) { injectRoute({ entryPoint: "@sanity/astro/studio/studio-route.astro", // Astro <= 3 entrypoint: "@sanity/astro/studio/studio-route.astro", // Astro > 3 - pattern: `/${resolvedOptions.studioBasePath}/[...params]`, + pattern: `/${studioBasePath}/[...params]`, prerender: false, }); } diff --git a/packages/sanity-astro/src/vite-plugin-sanity-client.ts b/packages/sanity-astro/src/vite-plugin-sanity-client.ts index 8a62418d..43a4bc6e 100644 --- a/packages/sanity-astro/src/vite-plugin-sanity-client.ts +++ b/packages/sanity-astro/src/vite-plugin-sanity-client.ts @@ -1,10 +1,11 @@ import type { ClientConfig } from "@sanity/client"; -import type { Plugin } from "vite"; +import type { DeepPartial } from "astro/dist/type-utils"; +import type { PluginOption } from "vite"; const virtualModuleId = "sanity:client"; const resolvedVirtualModuleId = "\0" + virtualModuleId; -export function vitePluginSanityClient(config: ClientConfig): Plugin { +export function vitePluginSanityClient(config: ClientConfig) { return { name: "sanity:client", resolveId(id: string) { @@ -22,5 +23,5 @@ export function vitePluginSanityClient(config: ClientConfig): Plugin { `; } }, - }; + } satisfies DeepPartial; } diff --git a/packages/sanity-astro/src/vite-plugin-sanity-studio.ts b/packages/sanity-astro/src/vite-plugin-sanity-studio.ts index fcbc8330..66d5296a 100644 --- a/packages/sanity-astro/src/vite-plugin-sanity-studio.ts +++ b/packages/sanity-astro/src/vite-plugin-sanity-studio.ts @@ -1,6 +1,9 @@ -import type { Plugin } from "vite"; +import type { DeepPartial } from "astro/dist/type-utils"; +import type { PluginOption } from "vite"; -export function vitePluginSanityStudio(resolvedOptions, { output }): Plugin { +export function vitePluginSanityStudio(resolvedOptions: { + studioBasePath?: string; +}) { const virtualModuleId = "sanity:studio"; const resolvedVirtualModuleId = virtualModuleId; @@ -19,7 +22,6 @@ export function vitePluginSanityStudio(resolvedOptions, { output }): Plugin { throw new Error( "[@sanity/astro]: Sanity Studio requires a `sanity.config.ts|js` file in your project root.", ); - return null; } if (!resolvedOptions.studioBasePath) { throw new Error( @@ -45,5 +47,5 @@ export function vitePluginSanityStudio(resolvedOptions, { output }): Plugin { } return null; }, - }; + } satisfies DeepPartial; }