From 4947eb095862baa9a7da939e77cadd2b1233d5b5 Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Sat, 27 Apr 2024 19:46:36 -0300 Subject: [PATCH] fix: Simplify emitted declarations for dependent integrations --- .changeset/gorgeous-nails-cry.md | 46 ++++++++++++++++++++++++++ package/src/core/define-integration.ts | 3 +- package/src/core/with-plugins.ts | 11 ++---- 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 .changeset/gorgeous-nails-cry.md diff --git a/.changeset/gorgeous-nails-cry.md b/.changeset/gorgeous-nails-cry.md new file mode 100644 index 00000000..87380f45 --- /dev/null +++ b/.changeset/gorgeous-nails-cry.md @@ -0,0 +1,46 @@ +--- +"astro-integration-kit": patch +--- + +Simplify emitted declarations for dependent integrations. + +Previoysly an integration using AIK and publishing to NPM with their type declarations would generate a type like this: + +```ts +declare const _default: (options?: undefined) => astro.AstroIntegration & { + hooks: { + "astro:config:setup": (params: { + config: astro.AstroConfig; + command: "dev" | "build" | "preview"; + isRestart: boolean; + updateConfig: ( + newConfig: DeepPartial + ) => astro.AstroConfig; + addRenderer: (renderer: astro.AstroRenderer) => void; + addWatchFile: (path: string | URL) => void; + injectScript: (stage: astro.InjectedScriptStage, content: string) => void; + injectRoute: (injectRoute: astro.InjectedRoute) => void; + addClientDirective: (directive: astro.ClientDirectiveConfig) => void; + addDevOverlayPlugin: (entrypoint: string) => void; + addDevToolbarApp: (entrypoint: string | astro.DevToolbarAppEntry) => void; + addMiddleware: (mid: astro.AstroIntegrationMiddleware) => void; + logger: astro.AstroIntegrationLogger; + }) => void; + }; + addExtraPage: (page: string) => void; +}; +``` + +Such inlining of the Astro hook parameter types not only makes your published package include unnecessary code, but can also make it fail to compile if you use a hook that inlines a reference to a transitive dependency: + +``` +error TS2742: The inferred type of 'default' cannot be named without a reference to '.pnpm/vite@5.2.10_@types+node@20.12.7/node_modules/vite'. This is likely not portable. A type annotation is necessary. +``` + +Now, the same integration will emit the following simplified declaration, without inlining any of the AstroHooks types: + +```ts +declare const _default: (options?: undefined) => astro.AstroIntegration & { + addExtraPage: (page: string) => void; +}; +``` diff --git a/package/src/core/define-integration.ts b/package/src/core/define-integration.ts index e63ab943..92783432 100644 --- a/package/src/core/define-integration.ts +++ b/package/src/core/define-integration.ts @@ -2,6 +2,7 @@ import type { AstroIntegration } from "astro"; import { AstroError } from "astro/errors"; import { z } from "astro/zod"; import { errorMap } from "../internal/error-map.js"; +import type { Prettify } from "../internal/types.ts"; type AstroIntegrationSetupFn = (params: { name: string; @@ -46,7 +47,7 @@ export const defineIntegration = < : undefined extends z.input ? [options?: z.input] : [options: z.input] -) => AstroIntegration & ReturnType) => { +) => AstroIntegration & Prettify, keyof AstroIntegration>>) => { return (...args): AstroIntegration & ReturnType => { const parsedOptions = (optionsSchema ?? z.never().optional()).safeParse( args[0], diff --git a/package/src/core/with-plugins.ts b/package/src/core/with-plugins.ts index db256234..8c5681d9 100644 --- a/package/src/core/with-plugins.ts +++ b/package/src/core/with-plugins.ts @@ -1,5 +1,5 @@ import type { AstroIntegration } from "astro"; -import type { NonEmptyArray } from "../internal/types.js"; +import type { NonEmptyArray, Prettify } from "../internal/types.js"; import type { AddedParam, AnyPlugin, @@ -14,12 +14,7 @@ type WithPluginsParams> = { hooks: ExtendedHooks; }; -type WithPluginsReturn = Omit< - Extensions, - "name" | "plugins" | "hooks" -> & { - hooks: AstroIntegration["hooks"]; -}; +export type WithPluginsReturn = Extensions & Omit; /** * Allows to extend hooks with custom parameters. Only used for advanced use-cases. @@ -34,7 +29,7 @@ export const withPlugins = < Extensions extends Record, >( options: WithPluginsParams & Extensions, -): WithPluginsReturn => { +): WithPluginsReturn>>> => { const { name, plugins,