Skip to content

Commit

Permalink
fix: Simplify emitted declarations for dependent integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Apr 27, 2024
1 parent 9cdbb97 commit 4947eb0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
46 changes: 46 additions & 0 deletions .changeset/gorgeous-nails-cry.md
Original file line number Diff line number Diff line change
@@ -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>
) => 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/[email protected]_@[email protected]/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;
};
```
3 changes: 2 additions & 1 deletion package/src/core/define-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Options extends z.ZodTypeAny> = (params: {
name: string;
Expand Down Expand Up @@ -46,7 +47,7 @@ export const defineIntegration = <
: undefined extends z.input<TOptionsSchema>
? [options?: z.input<TOptionsSchema>]
: [options: z.input<TOptionsSchema>]
) => AstroIntegration & ReturnType<TSetup>) => {
) => AstroIntegration & Prettify<Omit<ReturnType<TSetup>, keyof AstroIntegration>>) => {
return (...args): AstroIntegration & ReturnType<TSetup> => {
const parsedOptions = (optionsSchema ?? z.never().optional()).safeParse(
args[0],
Expand Down
11 changes: 3 additions & 8 deletions package/src/core/with-plugins.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,12 +14,7 @@ type WithPluginsParams<TPlugins extends NonEmptyArray<AnyPlugin>> = {
hooks: ExtendedHooks<TPlugins>;
};

type WithPluginsReturn<Extensions> = Omit<
Extensions,
"name" | "plugins" | "hooks"
> & {
hooks: AstroIntegration["hooks"];
};
export type WithPluginsReturn<Extensions> = Extensions & Omit<AstroIntegration, 'name'>;

/**
* Allows to extend hooks with custom parameters. Only used for advanced use-cases.
Expand All @@ -34,7 +29,7 @@ export const withPlugins = <
Extensions extends Record<any, unknown>,
>(
options: WithPluginsParams<TPlugins> & Extensions,
): WithPluginsReturn<Extensions> => {
): WithPluginsReturn<Prettify<Omit<Extensions, keyof WithPluginsParams<any>>>> => {
const {
name,
plugins,
Expand Down

0 comments on commit 4947eb0

Please sign in to comment.