Skip to content

Commit

Permalink
feat: Bump minimal version of Astro to 4.12 (#115)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Lefebvre <[email protected]>
  • Loading branch information
Fryuni and florian-lefebvre authored Jul 20, 2024
1 parent a9a1d56 commit e035a87
Show file tree
Hide file tree
Showing 14 changed files with 1,890 additions and 2,496 deletions.
26 changes: 26 additions & 0 deletions .changeset/twelve-coins-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"astro-integration-kit": minor
---

Bumps the minimal version of Astro to 4.12

- Removes the `AstroIntegration.ExtraHooks` workaround for extending hooks in favor of the native `Astro.IntegrationHooks` extendable interface:

```diff
-namespace AstroIntegrationKit {
- interface ExtraHooks {
+namespace Astro {
+ interface IntegrationHooks {
'myLib:myHook': (params: { foo: string }) => Promise<void>;
}
}
```

- Removes the `HookParameters` type that combined the native types and the workaround above. The native `HookParameters` should be used now:

```diff
- import type { HookParameters } from 'astro-integration-kit';
+ import type { HookParameters } from 'astro';
```

- Fixes the typing of `defineUtility` to not cause transitive dependency on non-portble Astro types when building libraries with type declarations.
6 changes: 3 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/starlight": "^0.22.4",
"@astrojs/starlight": "^0.25.1",
"@expressive-code/plugin-line-numbers": "^0.35.3",
"astro": "^4.8.6",
"astro": "^4.12.1",
"sharp": "^0.33.4"
},
"devDependencies": {
"@astrojs/check": "^0.7.0",
"@astrojs/check": "^0.8.2",
"typescript": "^5.4.5"
}
}
26 changes: 0 additions & 26 deletions docs/src/content/docs/core/define-integration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,6 @@ In setup, you'll want to add any logic that is shared between any hook, for exam

It needs to return Astro hooks.

## Using non-built-in Astro hooks

Some official Astro integrations, like [`@astrojs/db`](https://docs.astro.build/en/guides/integrations-guide/db/), define new integration hooks that are not built into Astro core. Astro Integration Kit does not integrate with those hooks by default as they are optional.

If you want to use those hooks on your integration or plugin, you can import that extra behavior.

### Astro DB (`@astrojs/db`)

```ts title="my-integration/index.ts" ins={2,9-11}
import { defineIntegration } from "astro-integration-kit";
import "astro-integration-kit/types/db";

export default defineIntegration({
// ...
setup() {
return {
hooks: {
"astro:db:setup": ({ extendDb }) => {
// ...
},
},
};
},
});
```

## Defining extra integration fields

Any extra property present on the return of `setup` will be present on the integration object returned by initializing the integration with the options.
Expand Down
34 changes: 34 additions & 0 deletions docs/src/content/docs/getting-started/upgrade-guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ import { Tabs, TabItem } from '@astrojs/starlight/components';

Features get added and removed, and breaking changes are introduced! This documents how to migrate.

## `0.15.0`

---

### Native hook types

With the new minimal version of Astro, there is no longer any need for the previous workaround to support non-native hooks, like those from `@astrojs/db`. The following changes are needed to replace the previous Astro Integration Kit types with the native types:

- Hook type extensions
```diff lang="ts"
- namespace AstroIntegrationKit {
- interface ExtraHooks {
+ namespace Astro {
+ interface IntegrationHooks {
'myLib:myHook': (params: { foo: string }) => Promise<void>;
}
}
```
- Hook parameters
```diff lang="ts"
- import type { HookParameters } from 'astro-integration-kit';
+ import type { HookParameters } from 'astro';
```
- `@astrojs/db` hooks:
```diff lang="ts"
- import "astro-integration-kit/types/db";
+ /// <reference types="@astrojs/db" />
```
If you are using `@astrojs/db` values and types in your project, you can just remove import. Importing anything from the library also loads the types.
```diff lang="ts"
- import "astro-integration-kit/types/db";
import {...} from "@astrojs/db";
```

## `0.13.0`

---
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "root",
"private": true,
"packageManager": "pnpm@9.1.1",
"packageManager": "pnpm@9.5.0+sha512.140036830124618d624a2187b50d04289d5a087f326c9edfc0ccd733d76c4f52c3a313d4fc148794a2a9d81553016004e6742e8cf850670268a7387fc220c903",
"engines": {
"node": ">=18.19.0"
},
Expand Down
12 changes: 1 addition & 11 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
"types": "./dist/plugins/index.d.ts",
"default": "./dist/plugins/index.js"
},
"./types/db": {
"types": "./types/db.d.ts",
"default": "./types/noop.js"
},
"./dev": {
"types": "./dist/dev/index.d.ts",
"default": "./dist/dev/index.js"
Expand All @@ -48,13 +44,7 @@
},
"type": "module",
"peerDependencies": {
"@astrojs/db": "^0.9 || ^0.10 || ^0.11",
"astro": "^4.4.1"
},
"peerDependenciesMeta": {
"@astrojs/db": {
"optional": true
}
"astro": "^4.12.0"
},
"devDependencies": {
"@types/node": "^20.12.12",
Expand Down
7 changes: 4 additions & 3 deletions package/src/core/define-utility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { HookParameters, Hooks } from "./types.js";
import type { HookParameters } from "astro";
import type { Hooks } from "./types.js";

/**
* Allows defining a type-safe function requiring all the params of a given hook.
Expand All @@ -21,7 +22,7 @@ export const defineUtility =
* The function itself
* @param {Function} fn;
*/
<TFn extends (params: HookParameters<THook>, ...args: Array<any>) => any>(
fn: TFn,
<TArgs extends Array<any>, T>(
fn: (params: HookParameters<THook>, ...args: TArgs) => T,
) =>
fn;
14 changes: 1 addition & 13 deletions package/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ export type Plugin<
// To avoid having to call this manually for every generic
export type AnyPlugin = Plugin<string, Record<string, any>>;

declare global {
namespace AstroIntegrationKit {
// biome-ignore lint/suspicious/noEmptyInterface: Requires for interface merging
export interface ExtraHooks {}
}
}

export type Hooks = Prettify<
Required<NonNullable<import("astro").AstroIntegration["hooks"]>> &
AstroIntegrationKit.ExtraHooks
>;

export type HookParameters<T extends keyof Hooks> = Parameters<Hooks[T]>[0];
export type Hooks = Required<Astro.IntegrationHooks>;

type AnyFunction = (...args: Array<any>) => any;

Expand Down
10 changes: 2 additions & 8 deletions package/src/core/with-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import type { AstroIntegration } from "astro";
import type { AstroIntegration, HookParameters } from "astro";
import type { NonEmptyArray, Prettify } from "../internal/types.js";
import type {
AddedParam,
AnyPlugin,
ExtendedHooks,
HookParameters,
Hooks,
} from "./types.js";
import type { AddedParam, AnyPlugin, ExtendedHooks, Hooks } from "./types.js";

type WithPluginsParams<TPlugins extends NonEmptyArray<AnyPlugin>> = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion package/tests/unit/watch-directory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import type { HookParameters } from "astro";
import { dirname, normalize } from "pathe";
import type { Plugin } from "vite";
import {
Expand All @@ -11,7 +12,6 @@ import {
vi,
} from "vitest";
import { createResolver } from "../../src/core/create-resolver.ts";
import type { HookParameters } from "../../src/core/types.ts";
import { watchDirectory } from "../../src/utilities/watch-directory.ts";

const tempFolderName = ".TMP_WATCHDIRECTORY";
Expand Down
10 changes: 0 additions & 10 deletions package/types/db.d.ts

This file was deleted.

18 changes: 0 additions & 18 deletions package/types/noop.js

This file was deleted.

4 changes: 2 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
},
"dependencies": {
"@astrojs/tailwind": "^5.1.0",
"astro": "^4.8.6",
"astro": "^4.12.1",
"astro-integration-kit": "workspace:*",
"tailwindcss": "^3.4.3"
},
"devDependencies": {
"@astrojs/check": "^0.7.0",
"@astrojs/check": "^0.8.2",
"@types/node": "^20.12.12",
"typescript": "^5.4.5",
"vite-plugin-pwa": "^0.19.8"
Expand Down
Loading

0 comments on commit e035a87

Please sign in to comment.